Sorting Chapter 4 Sorting Sorting is one of

  • Slides: 22
Download presentation
Sorting Chapter 4

Sorting Chapter 4

Sorting • Sorting is one of the most fundamental problem in computer science. •

Sorting • Sorting is one of the most fundamental problem in computer science. • It is a useful preprocessing to make short work of other problems.

Applications of Sorting

Applications of Sorting

Applications of Sorting

Applications of Sorting

Sorting Algorithms • Selection Sort – very little storage; data movement – O(n 2)

Sorting Algorithms • Selection Sort – very little storage; data movement – O(n 2) comparidons. • Heapsort – selection sort with right data structure • Insertion Sort – very little storage; optimal comparisons – O(n 2) data movement

Sorting agorithms • Quicksort – This algorithm reduces the job of sorting one big

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.

Quicksort

Quicksort

Quicksort

Quicksort

Multicriteria Sorting • How can we break ties in sorting using multiple criteria?

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

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++

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

Vito’s Family

Vito’s Family

Vito’s Family

Vito’s Family

Vito’s Family

Vito’s Family • What is the right version of average to solve Vito’s problem:

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.

Stacks of Flapjacks

Stacks of Flapjacks

Stacks of Flapjacks

Stacks of Flapjacks

Stacks of Flapjacks

Stacks of Flapjacks

Stacks of Flapjacks

Stacks of Flapjacks

Bridge

Bridge

Bridge

Bridge

Bridge

Bridge