“插入排序”,用于由文本文件制成的数组

问题描述 投票:0回答:1

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

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

100
12345
Jane
Doe
12359
John
Doe
98765
James
Doe

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

[我所坚持的是如何设置'InsertionSort'以从重新格式化的'客户'列表中提取在第一行中创建的代码的数据。

我觉得插入排序将是我拥有的客户列表的最佳选择。以下是我可以找到的最佳示例,它是最适合该代码的示例。我一直在试图获取此公式以将信息拉到“客户列表”并进行排序而一直陷入困境。

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()
                {

                }
            }
        }
    }
}

以下代码是'Customer'类。

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}";
        }
    }  
}
c# arrays binary-search insertion-sort selection-sort
1个回答
0
投票
//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"); } }
© www.soinside.com 2019 - 2024. All rights reserved.