1.17k likes | 1.51k Views
C++ Plus Data Structures. Nell Dale Chapter 10 Sorting and Searching Algorithms. EFFICIENCY. Compare the efficiency of the sorting algorithms, in terms of Big-O and space requirements.
E N D
C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms expanded by J. Goetz
EFFICIENCY • Compare the efficiency of the sorting algorithms, in terms of Big-O and space requirements. • The usual time versus space tradeoff applies to sorts – more space often means less time, and vice versa. • We relate the number of comparisonsor swapsto the number of elements in the list (N) as a rough measure of the efficiency. expanded by J. Goetz
Sorting means . . . • The values stored in an array have keys of a type ItemType for which the relational operators are defined. (We also assume unique keys.) • Sortingrearranges the elements into either ascending or descending order within the array. (We’ll use ascending order.) expanded by J. Goetz
Straight Selection Sort values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] • Divides the array into two parts: already sorted, and not yet sorted. • On each pass, • finds the smallest of the unsorted elements, and • swaps it into its correct place, • thereby increasing the number of sorted elements by one. 36 24 10 6 12 expanded by J. Goetz
Selection Sort: Pass One values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] 36 24 10 6 12 U N S O R T E D expanded by J. Goetz
SORTED Selection Sort: End Pass One values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] 6 24 10 36 12 U N S O R T E D expanded by J. Goetz
SORTED Selection Sort: Pass Two values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] 6 24 10 36 12 U N S O R T E D expanded by J. Goetz
SORTED Selection Sort: End Pass Two values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] 6 10 24 36 12 U N S O R T E D expanded by J. Goetz
SORTED Selection Sort: Pass Three values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] 6 10 24 36 12 U N S O R T E D expanded by J. Goetz
UNSORTED Selection Sort: End Pass Three values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] S O R T E D 6 10 12 36 24 expanded by J. Goetz
UNSORTED Selection Sort: Pass Four values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] S O R T E D 6 10 12 36 24 expanded by J. Goetz
Selection Sort: End Pass Four values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] 6 10 12 24 36 S O R T E D expanded by J. Goetz
Selection Sort: How many comparisons? values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] 6 10 12 24 36 4 compares for values[0] 3 compares for values[1] 2 compares for values[2] 1 compare for values[3] = 4 + 3 + 2 + 1 expanded by J. Goetz
For selection sort in general • The number of comparisons when the array contains N elements is Sum = (N-1) + (N-2) + . . . + 2 + 1 expanded by J. Goetz
Notice that . . . Sum = (N-1) + (N-2) + . . . + 2 + 1 + Sum = 1 + 2 + . . . + (N-2) + (N-1) 2* Sum = N + N + . . . + N + N 2 * Sum = N * (N-1) Sum = N * (N-1) 2 expanded by J. Goetz
For selection sort in general • The number of comparisons when the array contains N elements is Sum = (N-1) + (N-2) + . . . + 2 + 1 Sum = N * (N-1) /2 Sum = .5 N2 - .5 N The term .5 N2 increases fastest relative to .5 N Sum = O(N2) expanded by J. Goetz
// feature: 1 . on each pass through the loop, one element is put into the proper place // 2. each iteration finds the smallest unsorted element and puts it into its correct place template <class ItemType > void SelectionSort ( ItemType values [ ] , int numValues ) // Post: Sorts array values[0 . . numValues-1 ] into ascending // order by key { int endIndex = numValues - 1 ; for ( int current = 0 ; current < endIndex ; current++ ) // swap the smallest unsorted element with the element at index current Swap ( values [ current ] , values [ MinIndex ( values, current, endIndex ) ] ) ; } // maximum one swap per iteration 17 expanded by J. Goetz
template <class ItemType > int MinIndex ( ItemType values [ ] , int start , int end ) // Post: Function value = index of the smallest value in // values [start] . . values [end]. { int indexOfMin = start ; for ( int index = start + 1 ; index <= end ; index++ ) if ( values [ index ] < values [ indexOfMin ] ) indexOfMin = index ; return indexOfMin; } 18 expanded by J. Goetz
template<class ItemType> inline void Swap(ItemType& item1, ItemType& item2) // Post: Contents of item1 and item2 have been swapped. { ItemType tempItem; tempItem = item1; item1 = item2; item2 = tempItem; } expanded by J. Goetz
Bubble Sort values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] • Compares neighboring pairs of array elements, starting with the last array element, and • swaps neighbors whenever they are not in correct order. • On each pass, this causes the smallest element to “bubble up” to its correct place in the array and changes the location of the other elements in the array. 36 24 10 6 12 expanded by J. Goetz
Snapshot of BubbleSort Divides the array into two parts: already sorted, and not yet sorted. expanded by J. Goetz
template <class ItemType > void BubbleSort ( ItemType values [ ] , int numValues ) // Post: Sorts array values[0 ]. .. numValues-1 ] into ascending // order by key { int current = 0 ; // set the current index while ( current < numValues - 1 ) // “bubble up” the smallest item in the unsorted part // and change the location of the other elements BubbleUp ( values , current , numValues - 1 ) ; current++ ; // shrink the unsorted part of the array } 22 expanded by J. Goetz
template <class ItemType > void BubbleUp ( ItemType values [ ] , int start , int end ) // Post: Neighboring elements that were out of order have been // swapped between values [start] and values [end], // beginning at values [end]. { for ( int index = end ; index > start ; index-- ) if (values [ index ] < values [ index - 1 ] ) Swap ( values [ index ], values [ index - 1 ] ) ; } 23 expanded by J. Goetz
For bubble sort in general • The number of comparisons (in BubbleUp() is called N-1 first time and so on) when the array contains N elements is Sum = (N-1) + (N-2) + . . . + 2 + 1 Sum = N * (N-1) /2 Sum = .5 N2 - .5 N Sum = O(N2) • bubble sort in general requires the same amount of work as selection sort but BubbleSort might get the array in order before N-1 calls to BubbleUp() because it may has more than one swap per iteration. expanded by J. Goetz
Observations on BubbleSort This algorithm is always O(N2). There can be a large number of intermediate swaps. Can this algorithm be improved? expanded by J. Goetz
ANALYSIS • If no elements have been swapped, we know that the array is already in order. • So quit before the maximum number of iterations if BubbleUp returns a Boolean flag, sorted, to tell us when the array is sorted. expanded by J. Goetz
template<class ItemType> void BubbleUp2(ItemType values[], int startIndex, int endIndex, bool& sorted) // Post: Adjacent pairs that are out of order have // been switched between // values[startIndex]..values[endIndex] beginning at // values[endIndex]. // sorted is false if a swap was made; otherwise, true. { sorted = true; for (int index = endIndex; index > startIndex; index--) if (values[index] < (values[index-1])) { Swap(values[index], values[index-1]); sorted = false; } } // Note:ShortBubble is a good choice if you know is // almost in order !!! It is only algorithm that recognize // when the array is already sorted and stops. // The best case scenario is O(N) when // the array is initially sorted in ascending order template<class ItemType> void ShortBubble(ItemType values[], int numValues) // Post: The elements in the array values are // sorted into ascendingby key. // The process stops as soon as values is sorted. int current = 0; bool sorted = false; while (current < numValues - 1 && !sorted) { BubbleUp2(values, current, numValues-1, sorted); current++; } } // The worst case scenario is O(N2) // when the array is initially sorted in descending order + many extra swaps + setting and resseting of the sorted flag expanded by J. Goetz
Insertion Sort The principle of the insertion sort: One by one, each as yet unsorted array element is inserted into its properplace with respect to the already sorted elements. On each pass, this causes the number of already sorted elements to increase by one. values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] 36 24 10 6 12 expanded by J. Goetz
24 10 6 Insertion Sort Works like someone who “inserts” one more card at a time into a hand of cards that are already sorted. To insert 12, we need to make room for it by moving first 36 and then 24. 36 12 expanded by J. Goetz
24 10 6 Insertion Sort Works like someone who “inserts” one more card at a time into a hand of cards that are already sorted. To insert 12, we need to make room for it by moving first 36 and then 24. 36 12 expanded by J. Goetz
24 36 Insertion Sort Works like someone who “inserts” one more card at a time into a hand of cards that are already sorted. To insert 12, we need to make room for it by moving first 36 and then 24. 10 6 12 expanded by J. Goetz
24 36 Insertion Sort Works like someone who “inserts” one more card at a time into a hand of cards that are already sorted. To insert 12, we need to make room for it by moving first 36 and then 24. 12 10 6 expanded by J. Goetz
A Snapshot of the Insertion Sort Algorithm Divides the array into two parts: already sorted, and not yet sorted. expanded by J. Goetz
template <class ItemType > void InsertionSort ( ItemType values [ ] , int numValues ) // Post: Sorts array values[0 . . numValues-1 ] into ascending // order by key { for ( int count = 0 ; count < numValues ; count++ ) // each succesive element in the array to be sorted is inserted // into the proper place with respect to other, already sorted elements InsertItem ( values , 0 , count ) ; } 34 expanded by J. Goetz
Template <class ItemType > Void InsertItem ( ItemType values [ ] , int start , int end ) // Post: elements between values [start] and values [end] // Have been sorted into ascending order by key. { bool finished = false ; int current = end ; bool moreToSearch = ( current != start ) ; while ( moreToSearch && !Finished ) // stop: swapped into the 1st place or they are in order { // compare the item at values [ current ] to the one before, // swap them if necessary if (values [ current ] < values [ current - 1 ] ) { Swap ( values [ current ], values [ current - 1 ] ) ; current-- ; moreToSearch = ( current != start ); } else finished = true ; } } 35 expanded by J. Goetz
Analizing Sorting Algorithms Simple Sorts (the simplicity) The princple: an array is divided into a sorted and an unsorted part during execution If we know nothing about the original order they are all O(N2) (they are too time consuming for sorting large arrays): • Straight Selection Sort • Bubble Sort • Insertion Sort A best case is O(N) if the data are sorted in ascending order A worst case is O(N2) if the data are sorted in reverse order More Complex Sorts • Quick Sort • Merge Sort • Heap Sort O(N2) O(N*log N) 36 expanded by J. Goetz
Recall that . . . A heap is a binary tree that satisfies these special SHAPEand ORDER properties: • Its shape must be a complete binary tree. • Order property: for each node in the heap, the value stored in that node is greater thanor equal to the value in each of its children. expanded by J. Goetz
A complete binary tree A complete binary tree is a binary tree that is either full or full through the next-to-last level, with the leaves on the last level as far to the left as possible. SHAPE OF A COMPLETE BINARY TREE expanded by J. Goetz
70 The largest element in a heap is always found in the root node root 12 60 30 10 8 40 expanded by J. Goetz
70 0 30 4 10 6 The heap can be stored in an array values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] root 70 60 12 40 30 8 10 12 2 60 1 40 3 8 5 expanded by J. Goetz
I. Heap Sort Approach • make the unsorted array into a heap by satisfying the order property (by taking up consecutive positions in the array). • Then repeat the steps 2. and 3. below until there are no more unsorted elements. 2. Take the root (maximum) element off the heapby swapping it into its correct place in the array at the end of the unsorted elements. 3. Reheap the remaining unsorted elements. (This puts the next-largest element into the root position). (The shape property guarantees a minimum high, so O(log2 N)comparisons) expanded by J. Goetz
70 0 30 4 10 6 1. After creating the original heap values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 70 60 12 40 30 6 10 12 2 60 1 40 3 6 5 expanded by J. Goetz
70 0 30 4 10 6 2. Swap root element into last place in unsorted array values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 70 60 12 40 30 6 10 12 2 60 1 40 3 6 5 expanded by J. Goetz
10 0 30 4 3. After swapping root element into its place values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 10 60 12 40 30 6 70 12 2 60 1 40 3 6 5 70 6 NO NEED TO CONSIDER AGAIN expanded by J. Goetz
60 0 30 4 4 .After reheaping remaining unsorted elements values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 60 40 12 10 30 6 70 12 2 40 1 10 3 6 5 70 6 expanded by J. Goetz
60 0 30 4 2. Swap root element into last place in unsorted array values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 60 40 12 10 30 6 70 12 2 40 1 10 3 6 5 70 6 expanded by J. Goetz
6 0 30 4 NO NEED TO CONSIDER AGAIN 3. After swapping root element into its place values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 6 40 12 10 30 60 70 12 2 40 1 10 3 60 5 70 6 expanded by J. Goetz
40 0 6 4 4. After reheaping remaining unsorted elements values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 40 30 12 10 6 60 70 30 1 12 2 10 3 60 5 70 6 expanded by J. Goetz
40 0 6 4 2. Swap root element into last place in unsorted array values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 40 30 12 10 6 60 70 30 1 12 2 10 3 60 5 70 6 expanded by J. Goetz
6 0 NO NEED TO CONSIDER AGAIN 3. After swapping root element into its place values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 6 30 12 10 40 60 70 30 1 12 2 10 3 60 5 70 6 40 4 expanded by J. Goetz