sort和qsort Posted on 2021-04-10 Edited on 2022-04-06 sort1234bool cmp(const int &a, const int &b) { return a > b;//从大到小}sort(a + 1, a + 1 + n, cmp); qsort1234int cmp2(const void* a, const void* b) { return *(int*)b - *(int*)a;//从大到小}qsort(a+1, n, sizeof(a[0]),cmp2);
排序算法 Posted on 2021-04-09 Edited on 2022-04-06 快排12345678910111213141516171819202122232425262728void mysort(int l, int r) { if (l == r) return; int mid = (l + r) / 2; mysort(l, mid); mysort(mid + 1, r); int i = l; int j = mid + 1; int k = l; while (i <= mid && j <= r) { if (a[i] <= a[j]) { b[k++] = a[i++]; } else { b[k++] = a[j++]; } } while (i <= mid) { b[k++] = a[i++]; } while (j <= r) { b[k++] = a[j++]; } for (int t = l; t <= r; t++) { a[t] = b[t]; }} 归并排序123456789101112131415161718192021void mysort2(int l, int r) { if (l >= r) return; int mid = (l + r) / 2; int i = l; int j = r; int flag = a[i]; while (i<j) { while (i < j && flag <= a[j]) { j--; } a[i] = a[j]; while (i < j && a[i] <= flag) { i++; } a[j] = a[i]; } a[i] = flag; mysort2(l, i); mysort2(i + 1, r);}
github图片看不了 Posted on 2021-04-08 改host1234567891011121314151617181920212223242526140.82.113.3 github.com140.82.114.20 gist.github.com151.101.184.133 assets-cdn.github.com151.101.184.133 raw.githubusercontent.com151.101.184.133 gist.githubusercontent.com151.101.184.133 cloud.githubusercontent.com151.101.184.133 camo.githubusercontent.com151.101.184.133 avatars0.githubusercontent.com199.232.68.133 avatars0.githubusercontent.com199.232.28.133 avatars1.githubusercontent.com151.101.184.133 avatars1.githubusercontent.com151.101.184.133 avatars2.githubusercontent.com199.232.28.133 avatars2.githubusercontent.com151.101.184.133 avatars3.githubusercontent.com199.232.68.133 avatars3.githubusercontent.com151.101.184.133 avatars4.githubusercontent.com199.232.68.133 avatars4.githubusercontent.com151.101.184.133 avatars5.githubusercontent.com199.232.68.133 avatars5.githubusercontent.com151.101.184.133 avatars6.githubusercontent.com199.232.68.133 avatars6.githubusercontent.com151.101.184.133 avatars7.githubusercontent.com199.232.68.133 avatars7.githubusercontent.com151.101.184.133 avatars8.githubusercontent.com199.232.68.133 avatars8.githubusercontent.com 改DNS12233.5.5.5233.6.6.6 结果都没用
快排 Posted on 2021-04-08 12345678910111213141516171819202122void mysort(vector<int>& nums, int l, int r){ if(l>=r) return; int tl = l; int tr = r; int flag = nums[l]; while(l<r){ while(l<r&&flag<=nums[r]){ r--; } nums[l] = nums[r]; while(l<r&&nums[l]<=flag){ l ++; } nums[r] = nums[l]; } nums[l] = flag; mysort(nums, tl, l-1); mysort(nums, l+1, tr);}