Is it possible to make a ranking at the same time as quick sort?

Asked 2 years ago, Updated 2 years ago, 46 views

A, 10,50,32
B, 30, 60, 21
C,15,45,61

I would like to read the csv file and output the dat and csv file that meets the following conditions without changing the order of A to C.The reason I don't want to go out of order is to allocate unique values (arrangement subscripts) depending on the order of A, B, and C.

  • Dat file with only the fourth column of data sorted in descending order
    Expected Output:

    61
    32
    21
    
  • Ranking ranking when sorted in descending order by data in column 5 and column 4
    Expected Output:

    A, 10, 50, 32, 2
    B, 30, 60, 21, 3
    C, 15, 45, 61, 1
    

Dat file with data only in column 4 sorted in descending order
Expected Output:

61
32
21

Ranking ranking when sorted in descending order by data in column 5 and column 4
Expected Output:

A, 10, 50, 32, 2
B, 30, 60, 21, 3
C, 15, 45, 61, 1

Also, I would like to use a quick sort with a faster average calculation time.

At the moment I was thinking about how to implement it, I was thinking of putting only the third column in the array and performing quick sort, and using a separate for statement to rank it.

However, it seems inefficient to write the syntax separately and repeatedly for ranking even though the data in descending order is almost equal to the ranking in the quick sort, so I'm wondering if there's any way.

Is it possible to perform quick sort and ranking at the same time?Or is it possible to change the sorting algorithm?

Please let me know about the above points.

algorithm

2022-09-30 19:22

1 Answers

It seems inefficient to write the syntax separately and repeatedly for ranking purposes.

I don't think ranking separately is inefficient.

The average quick sort calculation is O(nlog n), worst of all, O(n^2), but then the ranking is O(n).
Also, it is difficult to determine the ranking during sorting.Even if implemented, the IF statement for determining the ranking will be passed through every time, and it will become inefficient.If you want efficiency, you should pay more attention to the number of conditional branches than to the number of iterations.
If you really want to sort and rank in the same way, I think you can easily implement bubble sort, but I don't recommend it because it will definitely slow down.
In addition, it is not recommended that multiple functions be processed into one process.Considering the ease of maintenance, I think it would be easier to understand if the processing was organized by function.

For your information, if I were to implement it with C#, the code would be as follows.We are creating a new list by sorting the list itself with the values in the fourth column as the key.

public class data
{
    public string name {get;set;}
    public int Value 1 { get; set; }
    public int Value2 {get;set;}
    public int Value3 {get;set;}
    Public int Rank {get;set;} // Rank is not determined when input CVS is loaded
}

public void sample (List<Data>source)
{
    // Create a list sorted by Value3.
    // Source and ranking only have different order of arrangement, and the data of the elements refer to the same.
    List<Data>ranking=source.OrderByDescending(e=>e.Value3).ToList();

    // Ranking. Action not implemented if Value 3 is equivalent
    intrank=1;
    foreach (var data in ranking)
    {
        data.Rank=rank;
        rank++;
    }

    // output:Value 3
    foreach (var data in ranking)
    {
        Console.WriteLine($"{data.Value3}");
    }

    // output:ranked data
    foreach (var data in source)
    {
        Console.WriteLine($"{data.Name}, {data.Value1}, {data.Value2}, {data.Value3}, {data.Rank}";
    }
}


2022-09-30 19:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.