Server IP : 172.16.15.8 / Your IP : 13.59.112.169 Web Server : Apache System : Linux zeus.vwu.edu 4.18.0-553.27.1.el8_10.x86_64 #1 SMP Wed Nov 6 14:29:02 UTC 2024 x86_64 User : apache ( 48) PHP Version : 7.2.24 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON Directory (0705) : /home/rnlink/ |
[ Home ] | [ C0mmand ] | [ Upload File ] |
---|
//---------------------------------------------------------------------------------- // Sorting routines covered in class //---------------------------------------------------------------------------------- #include <iostream> using namespace std; const int N = 16; // for array size //---------------------------------------------------------------------------------- // BubbleSort //---------------------------------------------------------------------------------- void BubbleSort (int A[]) { int i, j; // for loop indices for (i = 0; i < N-1; i++) // for each position in A, starting at the top ... for (j = N-1; j > i; j--) // ... bubble up the next smallest value to this if (A[j] < A[j-1]) // position by swapping swap (A[j], A[j-1]); } //---------------------------------------------------------------------------------- // InsertionSort //---------------------------------------------------------------------------------- void InsertionSort(int A[]) { int i, j, // for loop indices value; // next value to be put in place for (i = 1; i < N; i++) // for each value in A ... { value = A[i]; j = i-1; while (j >= 0 && A[j] > value) // ... shift to make space for it { A[j+1] = A[j]; j--; } A[j+1] = value; // then put the value in the position vacated } } //---------------------------------------------------------------------------------- // SelectionSort //---------------------------------------------------------------------------------- void SelectionSort (int A[]) { int i, j, // for loop indices minindex; // index of next smallest value for (i = 0; i < N-1; i++) // for each position in A, starting at the top ... { minindex = i; // ... find position of next smallest value for (j = (i + 1); j < N; j++) if (A[j] < A[minindex]) minindex = j; swap (A[i], A[minindex]); // and swap smallest value here } } //---------------------------------------------------------------------------------- // Merge is an auxiliary routine for MergeSort //---------------------------------------------------------------------------------- void Merge (int A[], int leftfirst, int leftlast, int rightfirst, int rightlast) { int tmpA [N]; // temp array to hold merged partitions int index = leftfirst; // index tracking position in temp array int i = leftfirst; // index tracking position in left partition int j = rightfirst; // index tracking position in right partition while (i <= leftlast && j <= rightlast) // while at least one partition not used up { if (A[i] <= A[j]) // compare next number in each partition { // and move smallest to temp array tmpA[index] = A[i]; i++; } else { tmpA[index] = A[j]; j++; } index++; } while (i <= leftlast) // right partition used up first { // so copy remainder of left partition tmpA[index] = A[i]; i++; index++; } while (j <= rightlast) // left partition used up first { // so copy remainder of right partition tmpA[index] = A[j]; j++; index++; } for (index = leftfirst; index <= rightlast; index++) // copy temp array back to A A[index] = tmpA[index]; } //---------------------------------------------------------------------------------- // MergeSort //---------------------------------------------------------------------------------- void MergeSort (int A[], int first, int last) { if (first < last) // don't bother if only 1 element in partition { int mid = (first + last) / 2; // calculate midpoint of partition MergeSort (A, first, mid); // recursively sort first half of partition MergeSort (A, mid+1, last); // recursively sort second half of partition Merge (A, first, mid, mid+1, last); // merge sorted partitions together } } //---------------------------------------------------------------------------------- // Split is an auxiliary routine for QuickSort //---------------------------------------------------------------------------------- void Split (int A[], int first, int last, int& splitpoint) { int splitval = A[first]; // choose pivot value as first element int i = first + 1; // beginning of left partition int j = last; // end of right partition do { while (i <= j && A[i] < splitval) // find value not belonging on left i++; while (i <= j && A[j] > splitval) // find value not belonging on right j--; if (i < j) // swap them if indices have not crossed { swap (A[i], A[j]); i++; // update left index for next round j--; // update right index for next round } } while (i <= j); // go around again if indices have not crossed splitpoint = j; // splitpoint is where j ended up swap(A[first], A[splitpoint]); // so swap first value with value here } //---------------------------------------------------------------------------------- // QuickSort //---------------------------------------------------------------------------------- void QuickSort (int A[], int first, int last) { if (first < last) { int splitpoint; Split (A, first, last, splitpoint); QuickSort (A, first, splitpoint-1); QuickSort (A, splitpoint+1, last); } } //---------------------------------------------------------------------------------- // ReHeapDown is an auxiliary routine for HeapSort //---------------------------------------------------------------------------------- void ReHeapDown(int A[], int root, int bottom) { int maxchild; // position of maximum child int leftchild = root*2 + 1; // position of left child int rightchild = root*2 + 2; // position of right child if (leftchild <= bottom) // if left child exists ... { maxchild = leftchild; // ... assume left child is largest if (rightchild <= bottom) // if right child also exists ... if (A[rightchild] > A[maxchild]) // ... see if it's larger than left maxchild = rightchild; if (A[root] < A[maxchild]) // if root smaller than maxchild ... { swap (A[root], A[maxchild]); // ... swap them to repair heap ... ReHeapDown (A, maxchild, bottom); // ... and move down heap } } } //---------------------------------------------------------------------------------- // HeapSort //---------------------------------------------------------------------------------- void HeapSort (int A[]) { int i; // Stage 1: Build the heap for (i = N/2 - 1; i >= 0; i--) ReHeapDown (A, i, N-1); // Stage 2: Swap one large value into place at a time for (i = N-1; i >= 1; i--) { swap (A[0], A[i]); // put root (largest value) into place ReHeapDown (A, 0, i-1); // repair remaining heap } } //---------------------------------------------------------------------------------- // To initialize A to the same sequence for each test //---------------------------------------------------------------------------------- void InitA (int A[]) { A[0] = 3; A[1] = 7; A[2] = 5; A[3] = 14; A[4] = 1; A[5] = 9; A[6] = 12; A[7] = 15; A[8] = 6; A[9] = 13; A[10] = 2; A[11] = 4; A[12] = 16; A[13] = 8; A[14] = 11; A[15] = 10; } //---------------------------------------------------------------------------------- // To display A both before and after sorting //---------------------------------------------------------------------------------- void DisplayA (int A[]) { for (int i = 0; i < N; i++) cout << A[i] << " "; cout << endl; } //---------------------------------------------------------------------------------- int main () { int A[N]; // Array of numbers to sort // Try BubbleSort InitA(A); cout << "\nArray before Sorting: "; DisplayA (A); BubbleSort (A); cout << "After BubbleSort: "; DisplayA (A); // Try InsertionSort InitA(A); cout << "\nArray before Sorting: "; DisplayA (A); InsertionSort (A); cout << "After InsertionSort: "; DisplayA (A); // Try SelectionSort InitA(A); cout << "\nArray before Sorting: "; DisplayA (A); SelectionSort (A); cout << "After SelectionSort: "; DisplayA (A); // Try MergeSort InitA(A); cout << "\nArray before Sorting: "; DisplayA (A); MergeSort (A, 0, N-1); cout << "After MergeSort: "; DisplayA (A); // Try QuickSort InitA(A); cout << "\nArray before Sorting: "; DisplayA (A); QuickSort (A, 0, N-1); cout << "After QuickSort: "; DisplayA (A); // Try HeapSort InitA(A); cout << "\nArray before Sorting: "; DisplayA (A); HeapSort (A); cout << "After HeapSort: "; DisplayA (A); }