Sorting Algorithms
October 18, 2017
CMPE 250 Sorting Algorithms October 18, 2017 1 / 74
Sorting Algorithms October 18, 2017 CMPE 250 Sorting Algorithms - - PowerPoint PPT Presentation
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
CMPE 250 Sorting Algorithms October 18, 2017 1 / 74
CMPE 250 Sorting Algorithms October 18, 2017 2 / 74
CMPE 250 Sorting Algorithms October 18, 2017 3 / 74
CMPE 250 Sorting Algorithms October 18, 2017 4 / 74
CMPE 250 Sorting Algorithms October 18, 2017 5 / 74
CMPE 250 Sorting Algorithms October 18, 2017 6 / 74
CMPE 250 Sorting Algorithms October 18, 2017 7 / 74
CMPE 250 Sorting Algorithms October 18, 2017 8 / 74
CMPE 250 Sorting Algorithms October 18, 2017 9 / 74
CMPE 250 Sorting Algorithms October 18, 2017 10 / 74
CMPE 250 Sorting Algorithms October 18, 2017 11 / 74
CMPE 250 Sorting Algorithms October 18, 2017 12 / 74
CMPE 250 Sorting Algorithms October 18, 2017 13 / 74
CMPE 250 Sorting Algorithms October 18, 2017 14 / 74
CMPE 250 Sorting Algorithms October 18, 2017 15 / 74
CMPE 250 Sorting Algorithms October 18, 2017 16 / 74
/** * Internal method that merges two sorted halves of a 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
CMPE 250 Sorting Algorithms October 18, 2017 18 / 74
CMPE 250 Sorting Algorithms October 18, 2017 19 / 74
CMPE 250 Sorting Algorithms October 18, 2017 20 / 74
CMPE 250 Sorting Algorithms October 18, 2017 21 / 74
CMPE 250 Sorting Algorithms October 18, 2017 22 / 74
CMPE 250 Sorting Algorithms October 18, 2017 23 / 74
CMPE 250 Sorting Algorithms October 18, 2017 24 / 74
CMPE 250 Sorting Algorithms October 18, 2017 25 / 74
1
2
3
4
CMPE 250 Sorting Algorithms October 18, 2017 26 / 74
#include <iostream> using namespace std; // Link list node typedef struct Node* listpointer; struct Node { int data; listpointer next; }; // function prototypes listpointer SortedMerge(listpointer a, listpointer b); void FrontBackSplit(listpointer source, listpointer* frontRef, listpointer* backRef); // sorts the linked list by changing next pointers (not data) void MergeSort(listpointer* headRef) { listpointer head = *headRef; listpointer a; listpointer b; //Base case -- length 0 or 1 if ((head == NULL) || (head->next == NULL)) { return; } // Split head into ’a’ and ’b’ sublists FrontBackSplit(head, &a, &b); // Recursively sort the sublists MergeSort(&a); MergeSort(&b); // answer = merge the two sorted lists together *headRef = SortedMerge(a, b); }
CMPE 250 Sorting Algorithms October 18, 2017 27 / 74
CMPE 250 Sorting Algorithms October 18, 2017 28 / 74
// Split the nodes of the given list into front and back halves, // and return the two lists using the reference parameters. // If the length is odd, the extra node should go in the front list. // Uses the fast/slow pointer strategy. void FrontBackSplit(listpointer source, listpointer* frontRef, listpointer* backRef) { listpointer fast; listpointer slow; if (source==NULL || source->next==NULL) { // length < 2 cases *frontRef = source; *backRef = NULL; } else { slow = source; fast = source->next; // Advance ’fast’ two nodes, and advance ’slow’ one node while (fast != NULL) { fast = fast->next; if (fast != NULL) { slow = slow->next; fast = fast->next; } } // ’slow’ is before the midpoint in the list, so split it in two //at that point. *frontRef = source; *backRef = slow->next; slow->next = NULL; } }
CMPE 250 Sorting Algorithms October 18, 2017 29 / 74
// Function to print nodes in a given linked list void printList(listpointer node) { while(node!=NULL) { cout<< node->data<<" "; node = node->next; } } // Function to insert a node at the beginning of the linked list void push(listpointer* head_ref, int new_data) { // allocate node listpointer new_node = new Node; // put in the data new_node->data = new_data; // link the old list off the new node new_node->next = (*head_ref); // move the head to point to the new node (*head_ref) = new_node; }
CMPE 250 Sorting Algorithms October 18, 2017 30 / 74
// Driver program to test above functions int main() { // Start with the empty list listpointer a = NULL; int n,num; // Let us create an unsorted linked list to test the functions cout<<endl<<"Enter the number of data elements to be sorted: "; cin>>n; // Create linked list. for(int i = 0; i < n; i++) { cout<<"Enter element "<<i+1<<": "; cin>>num; push(&a,num); } // Sort the above created Linked List MergeSort(&a); cout<< endl << "Sorted Linked List is: "<<endl; printList(a); return 0; }
CMPE 250 Sorting Algorithms October 18, 2017 31 / 74
1
2
3
CMPE 250 Sorting Algorithms October 18, 2017 32 / 74
CMPE 250 Sorting Algorithms October 18, 2017 33 / 74
CMPE 250 Sorting Algorithms October 18, 2017 34 / 74
CMPE 250 Sorting Algorithms October 18, 2017 35 / 74
CMPE 250 Sorting Algorithms October 18, 2017 36 / 74
CMPE 250 Sorting Algorithms October 18, 2017 37 / 74
CMPE 250 Sorting Algorithms October 18, 2017 38 / 74
CMPE 250 Sorting Algorithms October 18, 2017 39 / 74
CMPE 250 Sorting Algorithms October 18, 2017 40 / 74
CMPE 250 Sorting Algorithms October 18, 2017 41 / 74
CMPE 250 Sorting Algorithms October 18, 2017 42 / 74
CMPE 250 Sorting Algorithms October 18, 2017 43 / 74
CMPE 250 Sorting Algorithms October 18, 2017 44 / 74
// Uses median-of-three partitioning and a cutoff of 20. // a is an array of Comparable items. // left is the left-most index of the subarray. // right is the right-most index of the subarray. template <typename Comparable> void quicksort( vector<Comparable> & a, int left, int right ) { if( left + 20 <= right ) { const Comparable & pivot = median3( a, left, right ); // Begin partitioning int i = left, j = right - 1; for( ; ; ) { while( a[ ++i ] < pivot ) { } while( pivot < a[ --j ] ) { } if( i < j ) std::swap( a[ i ], a[ j ] ); else break; } std::swap( a[ i ], a[ right - 1 ] ); // Restore pivot quicksort( a, left, i - 1 ); // Sort small elements quicksort( a, i + 1, right ); // Sort large elements } else // Do an insertion sort on the subarray insertionSort( a, left, right ); } CMPE 250 Sorting Algorithms October 18, 2017 45 / 74
CMPE 250 Sorting Algorithms October 18, 2017 46 / 74
CMPE 250 Sorting Algorithms October 18, 2017 47 / 74
CMPE 250 Sorting Algorithms October 18, 2017 48 / 74
CMPE 250 Sorting Algorithms October 18, 2017 49 / 74
CMPE 250 Sorting Algorithms October 18, 2017 50 / 74
CMPE 250 Sorting Algorithms October 18, 2017 51 / 74
CMPE 250 Sorting Algorithms October 18, 2017 52 / 74
CMPE 250 Sorting Algorithms October 18, 2017 53 / 74
CMPE 250 Sorting Algorithms October 18, 2017 54 / 74
CMPE 250 Sorting Algorithms October 18, 2017 55 / 74
// Internal method for heapsort. // i is the index of an item in the heap. // Returns the index of the left child. inline int leftChild( int i ) { return 2 * i + 1; } // Internal method for heapsort that is used in // deleteMax and buildHeap. // i is the position from which to percolate down. // n is the logical size of the binary heap. template <typename Comparable> void percDown( vector<Comparable> & a, int i, int n ) { int child; Comparable tmp; for( tmp = std::move( a[ i ] ); leftChild( i ) < n; i = child ) { child = leftChild( i ); if( child != n - 1 && a[ child ] < a[ child + 1 ] ) ++child; if( tmp < a[ child ] ) a[ i ] = std::move( a[ child ] ); else break; } a[ i ] = std::move( tmp ); } CMPE 250 Sorting Algorithms October 18, 2017 56 / 74
CMPE 250 Sorting Algorithms October 18, 2017 57 / 74
CMPE 250 Sorting Algorithms October 18, 2017 58 / 74
CMPE 250 Sorting Algorithms October 18, 2017 59 / 74
1
2
3
4
5
6
CMPE 250 Sorting Algorithms October 18, 2017 60 / 74
CMPE 250 Sorting Algorithms October 18, 2017 61 / 74
CMPE 250 Sorting Algorithms October 18, 2017 62 / 74
CMPE 250 Sorting Algorithms October 18, 2017 63 / 74
CMPE 250 Sorting Algorithms October 18, 2017 64 / 74
CMPE 250 Sorting Algorithms October 18, 2017 65 / 74
CMPE 250 Sorting Algorithms October 18, 2017 66 / 74
CMPE 250 Sorting Algorithms October 18, 2017 67 / 74
CMPE 250 Sorting Algorithms October 18, 2017 68 / 74
CMPE 250 Sorting Algorithms October 18, 2017 69 / 74
CMPE 250 Sorting Algorithms October 18, 2017 70 / 74
CMPE 250 Sorting Algorithms October 18, 2017 71 / 74
CMPE 250 Sorting Algorithms October 18, 2017 72 / 74
CMPE 250 Sorting Algorithms October 18, 2017 73 / 74
CMPE 250 Sorting Algorithms October 18, 2017 74 / 74