-
排序
- void SelectSort(int *array, int size)
- {
- for (int idx = 0; idx < size; ++idx)
- {
- int min = idx ;
- int tmp = idx + 1;
- while (tmp < size)
- {
- if (array[tmp] < array[min])
- {
- min = tmp;
- }
- tmp++;
- }
- if (idx != min)
- {
- std::swap(array[min], array[idx]);
- }
- }
- }
-
- void BubbleSort(int* array, int size)
- {
- int flag = 1;
- int count = 0;
- for (int idx = 0; idx < size - 1; ++idx)
- {
- for (int jx = 0; jx < size - idx - 1; ++jx)
- {
- if (array[jx]>array[jx + 1])
- {
- std::swap(array[jx], array[jx + 1]);
- flag = 0;
- }
- count++;
- }
- if (flag == 1)
- {
- break;
- }
- }
- }
-
- void Merge(int *array, int *temp,int left, int mid, int right)
- {
- int begin1 = left;
- int end1 = mid;
- int begin2 = mid+1;
- int end2 = right;
- int index = left;
- while (begin1 <= end1 && begin2 <= end2)
- {
- if (array[begin1] < array[begin2])
- {
- temp[index++] = array[begin1++];
- }
- else
- {
- temp[index++] = array[begin2++];
- }
- }
- while (begin1 <= end1)
- {
- temp[index++] = array[begin1++];
- }
- while (begin2 <= end2)
- {
- temp[index++] = array[begin2++];
- }
- }
- void _MergeSort(int *array,int *temp, int left, int right)
- {
- if (left < right)
- {
- int mid = left + ((right - left) >> 1);
- _MergeSort(array, temp, left, mid);
- _MergeSort(array,temp, mid + 1, right);
- Merge(array, temp,left, mid, right);
- memcpy(array + left, temp + left, sizeof(array[0])*(right - left + 1));
- }
- }
- void MergeSort(int *array, int size)
- {
- int *temp = new int [size];
- if (NULL != temp)
- {
- _MergeSort(array, temp, 0, size - 1);
- delete[]temp;
- }
- }
-
- void MergeSortNor(int *array, int size)
- {
- int* temp = new int[size];
- int left = 0;
- int right = size - 1;
- int gap = 1;
- while (gap < size)
- {
- for (size_t idx = 0; idx < size; idx += 2 * gap)
- {
- left = idx;
- int mid = idx + gap - 1;
- right = mid + gap;
- if (mid >= size)
- {
- mid = size - 1;
- }
- if (right >= size)
- {
- right = size - 1;
- }
- Merge(array, temp, left, mid, right);
- }
- memcpy(array, temp, size*sizeof(int));
- gap++;
- }
- delete[]temp;
- }
-
- void printfSort(int arr[], int count)
- {
- for (int idx = 0; idx < count; ++idx)
- {
- cout << arr[idx] << " ";
- }
- cout << endl;
- }
- void FuntTest()
- {
-
- int array[] = { 21, 25, 49, 25, 0, 16 };
-
-
- int count = sizeof(array) / sizeof(array[0]);
- cout << count << endl;
- printfSort(array, count);
-
-
-
-
-
- printfSort(array,count);
- }
- int main()
- {
- FuntTest();
- system("pause");
- return 0;
- }
-
相关阅读:
微信小程序实现运动步数排行(可删除)
一个文艺的在线生成漂亮的二维码工具网站
微信小程序常见的UI框架/组件库总结
小程序踩坑记- tabBar.list[3].selectedIconPath 大小超过 40kb
推荐一款便捷的在线图片处理工具
如何在本地运行查看github上的开源项目
微信小程序实现运动步数排行(可删除)
从零开始学ios开发(三):第一个有交互的app
从零开始学ios开发(二):Hello World!来啦!
从零开始学ios开发(一):准备起航
-
原文地址:https://www.cnblogs.com/lyugeyi1030/p/8228137.html
Copyright © 2020-2023
润新知