【问题标题】:'Insertion Sort' for an Array made from a text file由文本文件组成的数组的“插入排序”
【发布时间】:2020-06-11 22:26:40
【问题描述】:

我正在尝试对由文本文件组成的数组进行“插入排序”。

程序的第一部分读取文本文件,将其上下文分配给字符串作为名称,将整数分配给数字。因为文本文件的格式如下:

100
12345
Jane
Doe
12359
John
Doe
98765
James
Doe

因此,文本文件的第一行说明了文件中的客户总数。接下来的三行是客户的 ID、名字和姓氏。

我坚持的地方是我将如何设置“插入排序”以从重新格式化的“客户”列表中提取数据,该列表是第一行中创建的代码。

我觉得插入排序将是我拥有的客户列表的最佳选择。 以下是我能找到的最适合代码的最佳示例。我一直在试图让这个公式将信息拉到“客户列表”并运行排序时遇到困难。

void insertionSort()
{
    int j, temp;
    for (int i = 1; i <= arr.Length - 1; i++)
    {
        temp = arr[i];
        j = i - 1;
        while (j >= 0 && arr[j] > temp)
        {
            arr[j + 1] = arr[j];
            j--;
        }
        arr[j + 1] = temp;
    }
}

对此的任何帮助将不胜感激。

同时,我将处理二进制搜索方法,看看上面有什么。

以下代码是“程序”。

using System;
using System.Collections.Generic;
using System.IO;

namespace ProgramTest
{
    public class Program
    {
        public static void Main(string [] args)
        {
            //Creates Customers list
            List<Customer> customers = new List<Customer>();
            string fName, lName;
            int id;
            //Opens text file
            using (StreamReader sr = File.OpenText("customers.txt"))
            {
                 //ignores first line
                 string data = sr.ReadLine();
                 //reads data and assigns objects
                 while ((data = sr.ReadLine()) != null)
                 {
                     fName = data;
                     lName = sr.ReadLine();
                     id = int.Parse(sr.ReadLine());
                     //creats customer class list 

                     customers.Add(new Customer() { customerId = id, fName = fName, lName = lName });
                 int choice;
            }
            do
            {
                Console.WriteLine("Main Menu\n");
                Console.WriteLine("1. List all Customers");
                Console.WriteLine("2. Sort by Customer ID");
                Console.WriteLine("3. Search by Customer ID");
                Console.WriteLine("4. Exit");
                Console.WriteLine();
                Console.Write("Enter Choice:");
                choice = Convert.ToInt32(Console.ReadLine());

                switch (choice)
                {
                    case 1:
                        listAll();
                        break;
                    case 2:
                        insertionSort();
                        break;
                    case 3:
                        binarySearch();
                        break;
                    case 0:
                        break;
                    default:
                        Console.WriteLine("Dont Recognize Input.");
                        break;
                } while (choice != 4);

                //display the list as is from text file
                void listAll()
                {
                    foreach (var customer in customers)
                        Console.WriteLine(customer);
                }

                //sorts list from based on Customer ID lowest to highest
                void insertionSort()
                {

                }

                //can search by customer ID
                void binarySearch()
                {

                }
            }
        }
    }
}

以下代码是“客户”类。

using System;

namespace ProgramTest{
{
    public class Customer
    {
        /// First Name String
        public string fName { get; set; }

        /// Last Mame String
        public string lName { get; set; }

        /// Customer ID
        public int customerId { get; set; }

        /// Return String
        public override string ToString()
        {
            //return $"{fName}, {lName}, {customerId.ToString().PadLeft(5, '0')}";
            return $"{customerId}: {fName} {lName}";
        }
    }  
}

【问题讨论】:

  • 为什么不:customers = customers.OrderBy(c =&gt; c.Id).ToList();
  • @RufusL for the customers line 这将如何改变输出?
  • 该行按Id 属性对客户进行升序排序。
  • 除非这是学习排序算法的练习,否则我同意@Ru​​fus 仅使用内置的 LINQ 排序和搜索功能。
  • 还有Array.Sort 用于数组,List&lt;T&gt; 也有Sort 方法。

标签: c# arrays binary-search insertion-sort selection-sort


【解决方案1】:

使用 LINQ 可以很容易地以您的代码为例:

//sorts list from based on Customer ID lowest to highest
void insertionSort()
{
    customers = customers.OrderBy(x => x.customerId).ToList();
}

//can search by customer ID
void binarySearch()
{
    Console.Write("Customer ID to search:");
    var id = Convert.ToInt32(Console.ReadLine());
    var customer = customers.FirstOrDefault(x => x.customerId == id);
    if (customer != null)
    {
        Console.WriteLine(customer);
    }
    else
    {
        Console.WriteLine("Customer not found");
    }

}

【讨论】:

    猜你喜欢
    • 2022-10-15
    • 1970-01-01
    • 2013-03-03
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多