void Util::getArray23(int *arr, int len) { srand(time(NULL)); for (int i = 0; i < len; i++) { arr[i] = rand(); } } void Util::printArray24(int *arr, int len) { for (int i = 0; i < len; i++) { cout << arr[i] << "t"; } cout << endl << endl; } void Util::selectionSort(int *arr, int len) { for (int i = 0; i < len; i++) { int minIndex = i; for (int j = i + 1; j < len; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } if (i != minIndex) { swap(&arr[i], &arr[minIndex]); } } } void Util::swap(int *left, int *right) { int temp = *left; *left = *right; *right = temp; } void Util::array26(int len) { int *arr = new int[len]; getArray23(arr, len); cout << "Before selection sort:" << endl; printArray24(arr, len); cout << "After selection sort:" << endl; selectionSort(arr, len); printArray24(arr, len); delete[] arr; cout << "Finished in void Util::array26(int len) and now is " << getTimeNow() << endl; }
Quick sort
int Util::partition28(int *arr,int low,int high) { int pivot=arr[high]; int i=low-1; for(int j=low;j<high;j++) { if(arr[j]<pivot) { i=i+1; swap(&arr[i],&arr[j]); } } swap(&arr[i+1],&arr[high]); return i+1; } void Util::quickSort27(int *arr,int low,int high) { if(low<high) { int pivot=partition28(arr,low,high); quickSort27(arr,low,pivot-1); quickSort27(arr,pivot+1,high); } }
void Util::array29(int len) { int *arr = new int[len]; getArray23(arr, len); cout << "Before quick sort:" << endl; printArray24(arr, len); cout << "After quick sort:" << endl; quickSort27(arr, 0, len - 1); printArray24(arr, len); delete[] arr; cout << "Finished in void Util::array29(int len) and now is " << getTimeNow() << endl; }
原文链接: https://www.cnblogs.com/Fred1987/p/16027100.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;
也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬
![C++ selection sort.quick sort](https://www.ccppcoding.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/401572
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!