【问题标题】:Using bubble-sort to sort a text file by rows使用冒泡排序按行对文本文件进行排序
【发布时间】:2016-03-29 19:20:15
【问题描述】:

我正在创建 50 行的文本文件,每一行都有关于文件的信息。行中的单词用';'分隔。我需要按文件大小使用冒泡排序算法对文件进行排序,它是第三个单词,并从在控制台中排序的文件中写入行。这意味着第一行将具有最大的文件大小,第二、第三等...

到目前为止,我有这个:

Random rnd = new Random();
if (!File.Exists(@"C:\Users\ariak_000\Desktop\Řadící algoritmy\database\alpha.txt")) //C:\Users\bartbo13it\Downloads\alpha.txt 
{                
    string[] arrayx = new string[50]; 
    string[] arrayy = arrayx.Select(ggg => String.Join(";", new string[] { @"C:\user\directory", "file" + rnd.Next(100, 999), rnd.Next(0, 999999).ToString(), "txt", rnd.Next(0, 1).ToString() })).ToArray();
    File.WriteAllLines(@"C:\Users\ariak_000\Desktop\Řadící algoritmy\database\alpha.txtt", arrayy);
}
string[] lines = System.IO.File.ReadAllLines(@"C:\Users\ariak_000\Desktop\Řadící algoritmy\database\alpha.txt");                        
int pocet_radku = File.ReadAllLines(@"C:\Users\ariak_000\Desktop\Řadící algoritmy\database\alpha.txt").Length;

List<int> velikostsouboru = new List<int>();
using (StreamReader sr = new StreamReader(@"C:\Users\ariak_000\Desktop\Řadící algoritmy\database\alpha.txt"))
{
    Console.WriteLine("Pocet radku " + pocet_radku);
    for (int i = 0; i < pocet_radku; i++)
    {
        string radek = sr.ReadLine();
        string[] rozdeleni = radek.Split(';');
        int ParsePokus = Int32.Parse(rozdeleni[2]);
        velikostsouboru.Add(ParsePokus);
    }
}
int[] array = velikostsouboru.ToArray();
for (int i = 0; i < array.Length - 1; i++)
{
    for (int j = 0; j < array.Length - i - 1; j++)
    {
        if (array[j + 1] > array[j])
        {
            int temp = array[j];
            array[j] = array[j + 1];
            array[j + 1] = temp;
        }
    }
}
for (int a = 0; a < array.Length; a++)
{
    Console.WriteLine(array[a] + " ");
}           
Console.ReadKey();

它只是使用冒泡排序算法对文件大小进行排序,并在控制台中按从大到小的排序写入文件大小。仍然需要编写整行排序,而不仅仅是文件大小。

我将不胜感激。

【问题讨论】:

  • 为什么你有2个for循环只是为了打印value2
  • 既然你已经编辑了......为什么你有1个for循环只是为了打印value2
  • 它们是不必要的,谢谢。代码已编辑。
  • for (int a = 0; a &lt; 1; a++) - wat...
  • 使用Simple bubble sort c#作为参考。

标签: c# file bubble-sort


【解决方案1】:
// There are better ways to create a file, but I decided to keep it in the original.
const string filePath = @"C:\Users\ariak_000\Desktop\Řadící algoritmy\database\alpha.txt";

var rnd = new Random();
if (!File.Exists(filePath))
{
    var array = new string[50].Select(x => String.Join(";", new[]
    {
        @"C:\user\directory", 
        "file" + rnd.Next(100, 999), 
        rnd.Next(0, 999999).ToString(CultureInfo.InvariantCulture), 
        "txt", 
        rnd.Next(0, 1).ToString(CultureInfo.InvariantCulture)
    })).ToArray();

    File.WriteAllLines(filePath, array);
}

// It will read the file and fill a variable with the data
// Convert the value to int is necessary in the order process
// Otherside, it will consider 9 higher than 10.
var data = File.ReadAllLines(filePath)
    .Select(value => value.Split(';'))
    .Select(x => new
    {
        Folder = x[0],
        FileName = x[1],
        Size = Convert.ToInt32(x[2]),
        Extension = x[3],
        IsActive = x[4]
    })
    .ToList<dynamic>();

// You can change it using the bubble sort as you wish
// I preffer the linq way.
var orderedData = data.OrderByDescending(x => x.Size);

// Writes the header in console (du~)
Console.WriteLine("Folder" + "\t\t\t"
                + "FileName" + "\t"
                + "Size" + "\t"
                + "Extension" + "\t"
                + "IsActive");

foreach (var result in orderedData)
{
    // If you want to write the result in a file, you do it here.
    Console.WriteLine(result.Folder + "\t"
                    + result.FileName + "\t\t"
                    + result.Size + "\t"
                    + result.Extension + "\t\t"
                    + result.IsActive);
}

Console.ReadKey();

【讨论】:

    猜你喜欢
    • 2022-01-19
    • 2015-09-13
    • 2019-05-10
    • 2017-09-27
    • 2011-04-24
    • 2011-04-22
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    相关资源
    最近更新 更多