首页 > 代码库 > 排序算法-冒泡、插入、归并、希尔、快速、选择--代码总结
排序算法-冒泡、插入、归并、希尔、快速、选择--代码总结
冒泡排序代码:
#include <iostream> #include <string> using namespace std; template<class ItemType> void bubbleSort(ItemType theArray[], int n) { bool sorted = false; // False when swaps occur int pass = 1; while (!sorted && (pass < n)) { // At this point, theArray[n+1-pass..n-1] is sorted // and all of its entries are > the entries in theArray[0..n-pass] sorted = true; // Assume sorted for (int index = 0; index < n - pass; index++) { // At this point, all entries in theArray[0..index-1] // are <= theArray[index] int nextIndex = index + 1; if (theArray[index] > theArray[nextIndex]) { // Exchange entries std::swap(theArray[index], theArray[nextIndex]); sorted = false; // Signal exchange } // end if } // end for // Assertion: theArray[0..n-pass-1] < theArray[n-pass] pass++; } // end while } // end bubbleSort int main() { string a[6] = {"Z", "X", "R", "K", "F", "B"}; bubbleSort(a, 6); for (int i = 0; i < 6; i++) cout << a[i] << " "; cout << endl; } // end main
插入排序代码:
#include <iostream> #include <string> using namespace std; template<class ItemType> void insertionSort(ItemType theArray[], int n) { for (int unsorted = 1; unsorted < n; unsorted++) { ItemType nextItem = theArray[unsorted]; int loc = unsorted; while ((loc > 0) && (theArray[loc - 1] > nextItem)) { // Shift theArray[loc - 1] to the right theArray[loc] = theArray[loc - 1]; loc--; } // end while // At this point, theArray[loc] is where nextItem belongs theArray[loc] = nextItem; // Insert nextItem into sorted region } // end for } // end insertionSort int main() { string a[6] = {"Z", "X", "R", "K", "F", "B"}; insertionSort(a, 6); for (int i = 0; i < 6; i++) cout << a[i] << " "; cout << endl; } // end main
归并排序代码:
#include <iostream> #include <string> using namespace std; const int MAX_SIZE = 50; template<class ItemType> void merge(ItemType theArray[], int first, int mid, int last) { ItemType tempArray[MAX_SIZE]; // Temporary array int first1 = first; // Beginning of first subarray int last1 = mid; // End of first subarray int first2 = mid + 1; // Beginning of second subarray int last2 = last; // End of second subarray int index = first1; // Next available location in tempArray while ((first1 <= last1) && (first2 <= last2)) { // At this point, tempArray[first..index-1] is in order if (theArray[first1] <= theArray[first2]) { tempArray[index] = theArray[first1]; first1++; } else { tempArray[index] = theArray[first2]; first2++; } // end if index++; } // end while // Finish off the first subarray, if necessary while (first1 <= last1) { // At this point, tempArray[first..index-1] is in order tempArray[index] = theArray[first1]; first1++; index++; } // end while // Finish off the second subarray, if necessary while (first2 <= last2) { // At this point, tempArray[first..index-1] is in order tempArray[index] = theArray[first2]; first2++; index++; } // end for // Copy the result back into the original array for (index = first; index <= last; index++) theArray[index] = tempArray[index]; } // end merge void mergeSort(ItemType theArray[], int first, int last) { if (first < last) { // Sort each half int mid = first + (last - first) / 2; // Index of midpoint // Sort left half theArray[first..mid] mergeSort(theArray, first, mid); // Sort right half theArray[mid+1..last] mergeSort(theArray, mid + 1, last); // Merge the two halves merge(theArray, first, mid, last); } // end if } // end mergeSort int main() { string a[6] = {"Z", "X", "R", "K", "F", "B"}; mergeSort(a, 0, 5); for (int i = 0; i < 6; i++) cout << a[i] << " "; cout << endl; } // end main
希尔排序代码:
void shellSort(ItemType theArray[], int n) { for (int h = n / 2; h > 0; h = h / 2) { for (int unsorted = h; unsorted < n; unsorted++) { ItemType nextItem = theArray[unsorted]; int loc = unsorted; while ( (loc >= h) && (theArray[loc - h] > nextItem) ) { theArray[loc] = theArray[loc - h]; loc = loc - h; } // end while theArray[loc] = nextItem; } // end for } // end for } // end shellSort
快速排序代码:
int partition(int *arr,int low,int high) { int pivot=arr[high]; int i=low-1; int j; for(j=low;j<high;++j) if(arr[j]<pivot) swap(arr[++i],arr[j]); swap(arr[i+1],arr[high]); return i+1; } void quickSort(int theArray[], int first, int last) { // Create the partition: S1 | Pivot | S2 if(first < last) { int pivotIndex = partition(theArray, first, last); // Sort subarrays S1 and S2 quickSort(theArray, first, pivotIndex - 1); quickSort(theArray, pivotIndex + 1, last); } // end if }
选择排序代码:
void selectionSort(ItemType theArray[], int n) { for (int last = n - 1; last >= 1; last--) { int largest = findIndexofLargest(theArray, last+1); std::swap(theArray[largest], theArray[last]); } // end for } // end selectionSort int findIndexofLargest(const ItemType theArray[], int size) { int indexSoFar = 0; // Index of largest entry found so far for (int currentIndex = 1; currentIndex < size; currentIndex++) { if (theArray[currentIndex] > theArray[indexSoFar]) indexSoFar = currentIndex; } // end for return indexSoFar; // Index of largest entry } // end findIndexofLargest
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。