Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Multiple-Criteria Sorting algorithm to use?

Lets' say we have list of items, each item has (unknown)number of attributes. Sorting by single attribute is a simple sort algorithm. The question is: how to sort the same list ordering by all attributes? Each attribute has a weight, so we might sort by least important attribute first and then by more important attribute using stable sort algorithm and so on, but this is clearly not efficient.

Thanks.

like image 907
Dima Avatar asked Sep 15 '11 13:09

Dima


People also ask

Which sorting technique is best for sorting?

Quicksort. Quicksort is one of the most efficient sorting algorithms, and this makes of it one of the most used as well. The first thing to do is to select a pivot number, this number will separate the data, on its left are the numbers smaller than it and the greater numbers on the right.

Which sorting algorithm gives best performance?

Which is the best sorting algorithm? If you've observed, the time complexity of Quicksort is O(n logn) in the best and average case scenarios and O(n^2) in the worst case. But since it has the upper hand in the average cases for most inputs, Quicksort is generally considered the “fastest” sorting algorithm.

Which sorting algorithm is best if it is already sorted & Why?

Insertion Sort If the data is nearly sorted or when the list is small as it has a complexity of O(N2) and if the list is sorted a minimum number of elements will slide over to insert the element at its correct location. This algorithm is stable and it has fast running case when the list is nearly sorted.

Are there multiple algorithms for sorting data?

Wikipedia lists 43 different sorting algorithms. Quick sort, Merge Sort, Shell sort, Bubble Sort, Pigeonhole sort, Spreadsort, Bead sort, Stooge sort, and many more. Why so many? Because different sorting algorithms are useful in different circumstances.


1 Answers

SORT BY A,B,C

Your comparison inside the sort will: A,B,C are in highest to lowest prioerity

  • Compare Element 1's A with Element 2's A
    • If greater or less return result
    • Else Compare B
      • If greater or less return result
      • Else Compare C return result

This can be extrapolated to A..n criteria with a simple loop.

  • For Each Criteria in list of Criteria
    • Compare Element 1's Criteria with Element 2's
      • If greater or less return result
      • Else continue // for clarity
  • Return equal

The above both assume your Comparison function is Compare ( Element1, Element2 )

like image 130
Louis Ricci Avatar answered Oct 21 '22 08:10

Louis Ricci