Data Structures Algorithms Classic Sorting Richard Newman Classic

  • Slides: 8
Download presentation
Data Structures & Algorithms • Classic Sorting • Richard Newman

Data Structures & Algorithms • Classic Sorting • Richard Newman

 • Classic Sorting Algorithms • Merge. Sort • Bottom-up Merge. Sort • Quick.

• Classic Sorting Algorithms • Merge. Sort • Bottom-up Merge. Sort • Quick. Sort • Sorting Complexity • Divide-and-Conquer

 • Significance • Critical components of today's computational infrastructure l Understanding their properties

• Significance • Critical components of today's computational infrastructure l Understanding their properties has enabled us to develop them into practical system sorts l Quick. Sort honored as one of top 10 algorithms of the 20 th century in science and engineering

 • Merge. Sort • Sort data by l 1. Dividing array into 2

• Merge. Sort • Sort data by l 1. Dividing array into 2 halves l 2. Recursively sort each half l 3. Merge the two halves l Key step: Merge

Merge. Sort • Copy arrays to be sorted into auxiliary array aux[] l Merge

Merge. Sort • Copy arrays to be sorted into auxiliary array aux[] l Merge two halves back into original array – how? l Compare first items of each list l If get to the end of one list, just copy the remaining items of the other list

Merge. Sort • Copy original array into aux[] for(int k = lo; k <=

Merge. Sort • Copy original array into aux[] for(int k = lo; k <= hi; ++k) aux[k] = a[k]; l Perform merge int i = lo, j = mid+1; for (int k = lo; k <= hi; ++k){ if (i > mid) a[k] = aux[j++]; else if (j > hi) a[k] = aux[i++]; else if (aux[j] < aux[i]) a[k] = aux[j++]; else a[k] = aux[i++]; }

Merge. Sort A L G O R T H M S hi mid lo

Merge. Sort A L G O R T H M S hi mid lo aux[] I A G L O R H I M S T j i k a[] A G H I L M O R S T

 • Selection Sort • Variant – exchange smaller item with item following sorted

• Selection Sort • Variant – exchange smaller item with item following sorted list as smaller item is found • What is effect on performance? • Is Selection Sort stable? • Selection Sort animation: http: //www. sorting-algorithms. com/selection-sort