Sorting Algorithms
Total Page:16
File Type:pdf, Size:1020Kb
Sorting Algorithms October 18, 2017 CMPE 250 Sorting Algorithms October 18, 2017 1 / 74 Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. An internal sort requires that the collection of data fit entirely in the computer’s main memory. We can use an external sort when the collection of data cannot fit in the computer’s main memory all at once but must reside in secondary storage such as on a disk (or tape). We will analyze only internal sorting algorithms. CMPE 250 Sorting Algorithms October 18, 2017 2 / 74 Why Sorting? Any significant amount of computer output is generally arranged in some sorted order so that it can be interpreted. Sorting also has indirect uses. An initial sort of the data can significantly enhance the performance of an algorithm. Majority of programming projects use a sort somewhere, and in many cases, the sorting cost determines the running time. A comparison-based sorting algorithm makes ordering decisions only on the basis of comparisons. CMPE 250 Sorting Algorithms October 18, 2017 3 / 74 Sorting Algorithms There are many sorting algorithms, such as: Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort Heap Sort Shell Sort The first three are the foundations for faster and more efficient algorithms. CMPE 250 Sorting Algorithms October 18, 2017 4 / 74 Insertion Sort Insertion sort is a simple sorting algorithm that is appropriate for small inputs. The most common sorting technique used by card players. The list is divided into two parts: sorted and unsorted. In each pass, the first element of the unsorted part is picked up, transferred to the sorted sublist, and inserted at the appropriate place. A list of n elements will take at most n − 1 passes to sort the data. CMPE 250 Sorting Algorithms October 18, 2017 5 / 74 Insertion Sort Example CMPE 250 Sorting Algorithms October 18, 2017 6 / 74 Insertion Sort Algorithm // Simple insertion sort. template <typename Comparable> void insertionSort( vector<Comparable> &a) { for( int p = 1;p<a.size( ); ++p) { Comparable tmp= std::move(a[p]); int j; for(j=p;j > 0 && tmp<a[j - 1 ]; --j) a[j]= std::move(a[j - 1 ] ); a[j]= std::move( tmp); } } CMPE 250 Sorting Algorithms October 18, 2017 7 / 74 Insertion Sort – Analysis Running time depends on not only the size of the array but also the contents of the array. Best-case: ! O(n) Array is already sorted in ascending order. Inner loop will not be executed. The number of moves: 2 × (n − 1) ! O(n) The number of key comparisons: (n − 1) ! O(n) Worst-case: ! O(n2) Array is in reverse order: Inner loop is executed i − 1 times, for i = 2; 3;:::; n The number of moves: 2 × (n − 1) + (1 + 2 + ··· + n − 1) = 2 × (n − 1) + n × (n − 1)=2 ! O(n2) The number of key comparisons: (1 + 2 + ··· + n − 1) = n × (n − 1)=2 ! O(n2) Average-case: ! O(n2) We have to look at all possible initial data organizations. So, Insertion Sort is O(n2) CMPE 250 Sorting Algorithms October 18, 2017 8 / 74 Analysis of insertion sort Which running time will be used to characterize this algorithm? Best, worst or average? Worst: Longest running time (this is the upper limit for the algorithm) It is guaranteed that the algorithm will not be worse than this. Sometimes we are interested in the average case. But there are some problems with the average case. It is difficult to figure out the average case. i.e. what is average input? Are we going to assume all possible inputs are equally likely? In fact for most algorithms the average case is the same as the worst case. CMPE 250 Sorting Algorithms October 18, 2017 9 / 74 A lower bound for simple sorting algorithms An inversion : an ordered pair (Ai ; Aj ) such that i < j but Ai > Aj Example: 10, 6, 7, 15, 3,1 Inversions are: (10,6), (10,7), (10,3), (10,1), (6,3), (6,1) (7,3), (7,1) (15,3), (15,1), (3,1) CMPE 250 Sorting Algorithms October 18, 2017 10 / 74 Swapping Swapping adjacent elements that are out of order removes one inversion. A sorted array has no inversions. Sorting an array that contains i inversions requires at least i swaps of adjacent elements. CMPE 250 Sorting Algorithms October 18, 2017 11 / 74 Theorems Theorem 1: The average number of inversions in an array of N distinct elements is N(N − 1)=4 Theorem 2: Any algorithm that sorts by exchanging adjacent elements requires Ω(N2) time on average. For a sorting algorithm to run in less than quadratic time it must do something other than swap adjacent elements. CMPE 250 Sorting Algorithms October 18, 2017 12 / 74 Mergesort Mergesort algorithm is one of the two important divide-and-conquer sorting algorithms (the other one is quicksort). It is a recursive algorithm. Divides the list into halves, Sorts each half separately, and Then merges the sorted halves into one sorted array. CMPE 250 Sorting Algorithms October 18, 2017 13 / 74 Merge Sort Example CMPE 250 Sorting Algorithms October 18, 2017 14 / 74 Mergesort /** * Mergesort algorithm(driver). */ template <typename Comparable> void mergeSort( vector<Comparable> &a) { vector<Comparable> tmpArray(a.size( ) ); mergeSort(a, tmpArray, 0,a.size( ) - 1 ); } CMPE 250 Sorting Algorithms October 18, 2017 15 / 74 Mergesort (Cont.) /** * Internal method that makes recursive calls. * a is an array of Comparable items. * tmpArray is an array to place the merged result. * left is the left-most index of the subarray. * right is the right-most index of the subarray. */ template<typename Comparable> void mergeSort(vector<Comparable> &a, vector<Comparable> & tmpArray, int left, int right){ if (left< right){ int center=(left+ right) / 2; mergeSort(a, tmpArray, left, center); mergeSort(a, tmpArray, center + 1, right); merge(a, tmpArray, left, center + 1, right); } } CMPE 250 Sorting Algorithms October 18, 2017 16 / 74 Merge /** * Internal method that merges two sorted halves ofa subarray. * a is an array of Comparable items. * tmpArray is an array to place the merged result. * leftPos is the left-most index of the subarray. * rightPos is the index of the start of the second half. * rightEnd is the right-most index of the subarray. */ template <typename Comparable> void merge( vector<Comparable> &a, vector<Comparable> & tmpArray, int leftPos, int rightPos, int rightEnd) { int leftEnd= rightPos - 1; int tmpPos= leftPos; int numElements= rightEnd- leftPos + 1; // Main loop while( leftPos <= leftEnd&& rightPos <= rightEnd) if(a[ leftPos ] <=a[ rightPos]) tmpArray[ tmpPos++ ] = std::move(a[ leftPos++ ] ); else tmpArray[ tmpPos++ ] = std::move(a[ rightPos++ ] ); while( leftPos <= leftEnd) // Copy rest of first half tmpArray[ tmpPos++ ] = std::move(a[ leftPos++ ] ); while( rightPos <= rightEnd) // Copy rest of right half tmpArray[ tmpPos++ ] = std::move(a[ rightPos++ ] ); // Copy tmpArray back for( int i = 0;i< numElements; ++i, --rightEnd) a[ rightEnd]= std::move( tmpArray[ rightEnd]); } CMPE 250 Sorting Algorithms October 18, 2017 17 / 74 Merge Sort Example CMPE 250 Sorting Algorithms October 18, 2017 18 / 74 Merge Sort Example CMPE 250 Sorting Algorithms October 18, 2017 19 / 74 Mergesort – Analysis of Merge A worst-case instance of the merge step in mergesort CMPE 250 Sorting Algorithms October 18, 2017 20 / 74 Mergesort – Analysis of Merge (cont.) Merging two sorted arrays of size k Best-case: All the elements in the first array are smaller (or larger) than all the elements in the second array. The number of moves: 2k + 2k The number of key comparisons: k Worst-case: The number of moves: 2k + 2k The number of key comparisons: 2k − 1 CMPE 250 Sorting Algorithms October 18, 2017 21 / 74 Mergesort - Analysis Levels of recursive calls to mergesort, given an array of eight items CMPE 250 Sorting Algorithms October 18, 2017 22 / 74 Mergesort - Analysis CMPE 250 Sorting Algorithms October 18, 2017 23 / 74 Mergesort - Analysis Worst-case – The number of key comparisons: = 20 × (2 × 2m−1 − 1) + 21 × (2 × 2m−2 − 1) + ::: + 2m−1 × (2 × 20 − 1) = (2m − 1) + (2m − 2) + ::: + (2m − 2m−1) ( m terms ) m Pm−1 i = m × 2 − i=0 2 = m × 2m − 2m − 1 Using m = logn = n × log2n − n − 1 ! O(n × log2n) CMPE 250 Sorting Algorithms October 18, 2017 24 / 74 Mergesort - Analysis Mergesort is an extremely efficient algorithm with respect to time. Both worst case and average cases are O(n × log2n) But, mergesort requires an extra array whose size equals to the size of the original array. If we use a linked list, we do not need an extra array But, we need space for the links And, it will be difficult to divide the list into half ( O(n) ) CMPE 250 Sorting Algorithms October 18, 2017 25 / 74 Mergesort for Linked Lists Merge sort is often preferred for sorting a linked list. The slow random-access performance of a linked list makes some other algorithms (such as quicksort) perform poorly, and others (such as heapsort) completely impossible. MergeSort 1 If head is NULL or there is only one element in the Linked List then return. 2 Else divide the linked list into two halves. 3 Sort the two halves a and b. MergeSort(&first); MergeSort(&second); 4 Merge the two parts of the list into a sorted one. *head = Merge(first, second); CMPE 250 Sorting Algorithms October 18, 2017 26 / 74 Mergesort for linked lists #include <iostream> using namespace std; // Link list node typedef struct Node* listpointer; struct Node { int data; listpointer next; }; // function prototypes listpointer SortedMerge(listpointera, listpointerb); void FrontBackSplit(listpointer source, listpointer* frontRef, listpointer* backRef); // sorts the linked list by changing next pointers(not data) void MergeSort(listpointer* headRef) { listpointer