220 likes | 242 Views
Sorting is fundamental in computer science, facilitating efficient problem-solving. Explore various sorting algorithms like Selection Sort, Heapsort, Quicksort, and Multicriteria Sorting. Learn about Stable Sorting and Library Routines. Delve into intriguing problems like Vito's Family and Stacks of Flapjacks. Discover how sorting techniques can enhance your problem-solving skills.
E N D
Sorting Chapter 4
Sorting • Sorting is one of the most fundamental problem in computer science. • It is a useful preprocessing to make short work of other problems.
Sorting Algorithms • Selection Sort • very little storage; data movement • O(n2) comparidons. • Heapsort • selection sort with right data structure • Insertion Sort • very little storage; optimal comparisons • O(n2) data movement
Sorting agorithms • Quicksort • This algorithm reduces the job of sorting one big array into the job of sorting two smaller arrays by performing a partition step. • The partition separates the array into those elements that are less than the pivot/divider element, and • those which are strictly greater than this pivot/divider element.
Multicriteria Sorting • How can we break ties in sorting using multiple criteria?
Stable Sorting • Identical keys are kept in the same relative order they were in before the sorting. • we could sort by the first name, • then do stable sort by the last name • We know that the final results are sorted by both major and minor keys.
Library Routines • C • stdlib.h contains library functions • qsort • C++ • C++STL • sort • stable_sort • Java • java.util.Arrays • sort(Object[] a) • sort(Object[] a, Comparator c)
Vito’s Family • What is the right version of average to solve Vito’s problem: mean, median, or something else? • mean is easy to compute • median: the middle element in the sorted list. • This can be determined easily after sorting the items. • This element can be determined without sorting the elements using a quicksort like algorithm called quickselect.