C++ selection sort.quick sort
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; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
2020-03-19 TripleDESCryptoServiceProvider CryptoStream