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.
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 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.
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.
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.
SORT BY A,B,C
Your comparison inside the sort will: A,B,C are in highest to lowest prioerity
This can be extrapolated to A..n criteria with a simple loop.
The above both assume your Comparison function is Compare ( Element1, Element2 )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With