defselection_sort(nums): n = len(nums) for i inrange(n-1): k = i for j inrange(i + 1, n): if nums[j] < nums[k]: k = j # 记录最小元素的索引 nums[i], nums[k] = nums[k], nums[i]
1 2 3 4 5 6 7 8 9 10 11 12
voidselectionSort(vector<int> &nums){ int n = nums.size(); for(int i = 0; i < n - 1; i++){ int k = i; for(int j = i + 1; j < n; j++){ if(nums[j] < nums[k]){ k = j; } } swap(nums[i], nums[k]); } }
1 2 3 4 5 6 7 8 9 10 11 12 13
voidselectionSort(int nums[], int n){ for(int i = 0; i < n - 1; i++){ int k = i; for (int j = i + 1; j < n; j++){ if(nums[j] < nums[k]){ k = j } } int temp = nums[i]; nums[i] = nums[k]; nums[k] = temp; } }
defbubble_sort_with_flag(nums): n = len(nums) for i inrange(n - 1, 0, -1): flag = False for j inrange(i): if nums[j] > nums[j+1]: nums[j], nums[j+1] = nums[j+1], nums[j] flag = True ifnot flag: break
1 2 3 4 5 6 7 8 9 10 11 12 13 14
voidbubbleSortWithFlag(vector<int>& nums){ for(int i = nums.size() - 1; i > 0; i--){ bool flag = false; for(int j = 0; j < i; j++){ if(nums[j] > nums[j+1]){ swap(nums[j], nums[j+1]); flag = true; } } if(!flag){ break; } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
voidbubbleSortWithFlag(int nums[], int size){ for(int i = size() - 1; i > 0; i--){ bool flag = false; for(int j = 0; j < i; j++){ if(nums[j] > nums[j+1]){ int temp = nums[j]; nums[j] = nums[j+1]; nums[j+1] = temp; flag = true; } } if(!flag){ break; } } }
defpartition(self, nums, left, right): i, j = left, right while i < j: while i < j and nums[j] >= nums[left]: j -= 1# 从右向左找首个小于基准数的元素 while i < j and nums[i] <= nums[left]: i += 1# 从左向右找首个大于基准数的元素 nums[i], nums[j] = nums[j], nums[i] nums[i], nums[left] = nums[left], nums[i] return i
voidswap(int nums[], int i, int j) { int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp; }
intpartition(int nums[], int left, int right) { int i = left, j = right; while (i < j) { while (i < j && nums[j] >= nums[left]) { j--; } while (i < j && nums[i] <= nums[left]) { i++; } swap(nums, i, j); } swap(nums, i, left); return i; }
defmedian_three(self, nums, left, mid, right): """选取三个元素中的中位数""" if (nums[left] < nums[mid]) ^ (nums[left] < nums[right]): return left elif (nums[mid] < nums[left]) ^ (nums[mid] < nums[right]): return mid return right
defpartition(self, nums, left, right): med = self.median_three(nums, left, (left + right) // 2, right) nums[left], nums[med] = nums[med], nums[left] i, j = left, right while i < j: while i < j and nums[j] >= nums[left]: j -= 1 while i < j and nums[i] <= nums[left]: i += 1 nums[i], nums[j] = nums[j], nums[i] nums[i], nums[left] = nums[left], nums[i] return i
intmedianThree(vector<int> &nums, int left, int mid, int right){ if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right])) return left; elseif ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right])) return mid; else return right; }
intpartition(vector<int> &nums, int left, int right){ int med = medianThree(nums, left, (left + right) / 2, right); swap(nums, left, med); int i = left, j = right; while (i < j) { while (i < j && nums[j] >= nums[left]) j--; while (i < j && nums[i] <= nums[left]) i++; swap(nums, i, j); } swap(nums, i, left); return i; }
intmedianThree(int nums[], int left, int mid, int right) { if ((nums[left] < nums[mid]) ^ (nums[left] < nums[right])) return left; elseif ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right])) return mid; else return right; }
intpartition(int nums[], int left, int right) { int med = medianThree(nums, left, (left + right) / 2, right); swap(nums, left, med); int i = left, j = right; while (i < j) { while (i < j && nums[j] >= nums[left]) j--; while (i < j && nums[i] <= nums[left]) i++; swap(nums, i, j); } swap(nums, i, left); return i; }
defsift_down(nums, n, i): """堆的长度为n,从节点i开始,从顶至底堆化""" whileTrue: l = 2 * i + 1 r = 2 * i + 2 ma = i if l < n and nums[l] > nums[ma]: ma = l if r < n and nums[r] > nums[ma]: ma = r if ma == i: break nums[i], nums[ma] = nums[ma], nums[i] i = ma
defheap_sort(nums): # 建堆操作 for i inrange(len(nums) // 2 - 1, -1, -1): sift_down(nums, len(nums), i) for i inrange(len(nums) - 1, 0, -1): nums[0], nums[i] = nums[i], nums[0] sift_down(nums, i, 0)
voidsiftDown(vector<int>& nums, int n, int i){ while(true){ int l = 2 * i + 1; int r = 2 * i + 2; int ma = i if (l < n && nums[l] > nums[ma]){ ma = l; } if (r < n && nums[r] > nums[ma]){ ma = r; } if (ma == i){ break; } swap(nums[i], nums[ma]); i = ma; } }
voidheapSort(vector<int>& nums){ for (int i = nums.size() / 2 - 1; i >= 0; --i){ siftDown(nums, nums.size(), i); } for (int i = nums.size() - 1; i > 0; --i){ swap(nums[0], nums[i]); siftDown(nums, i, 0); } }
voidsiftDown(int nums[], int n, int i){ while(1){ int l = 2 * i + 1; int r = 2 * i + 2; int ma = i; if (l < n && nums[l] > nums[ma]){ ma = l; } if (r < n && nums[r] > nums[ma]){ ma = r; } if (ma == i){ break; } int temp = nums[i]; nums[i] = nums[ma]; nums[ma] = temp; i = ma; } }
voidheapSOrt(int nums[], int size){ for(int i = n / 2 - 1; i >= 0; --i){ siftDown(nums, n, i); } for(int i = size - 1; i > 0; --i){ int temp = nums[0]; nums[0] = nums[i]; nums[i] = temp; siftDown(nums, i, 0); } }
defbucket_sort(nums): k = len(nums) // 2 buckets = [[] for _ inrange(k)] # 1. 将数组元素分配到各个桶中 for num in nums: # 输入数据范围为[0, 1),使用num * k 映射到范围[0, k-1] i = int(num * k) buckets[i].append(num) # 2. 对各个桶执行排序 for bucket in buckets: buckets.sort() # 3. 遍历桶合并结果 i = 0 for bucket in buckets: for num in bucket: nums[i] = num i += 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
voidbucketSort(vector<float>& nums){ int k = nums.size() / 2; vector<vector<float>> buckets(k); for (float num : nums){ int i = num * k; buckets[i].push_back(num); } for(vector<float>& bucket : buckets){ sort(bucket.begin(), bucket.end()); }
int i = 0; for (vector<float>& bucket : buckets){ for (float num : bucket){ nums[i++] = num; } } }
voidbucketSort(float nums[], int size){ int k = size / 2; float **buckets = calloc(k, sizeof(float*)); for (int i = 0; i < k; i++){ buckets[i] = calloc(ARRAY_SIZE, sizeof(float)); }
for (int i = 0; i < size; i++){ int bucket_idx = nums[i] * k; int j = 0; while(buckets[bucket_idx][j] > 0 && buckets[bucket_idx][j] < nums[i]){ j++; } float temp = nums[i]; while(j < ARRAY_SIZE && buckets[bucket_idx][j] > 0){ swap(&temp, &buckets[bucket_idx][j]); } buckets[bucket_idx][j] = temp; }
for (int i = 0; i < k; i++){ qsort(buckets[i], ARRAY_SIZE); }
for(int i = 0, j = 0; j < k; j++){ for(int l = 0; i < ARRAY_SIZE; l++){ if(buckets[j][l] > 0){ nums[i++] = buckets[j][l]; } } }
for (int i = 0; i < k; i++){ free(buckets[i]); } free(buckets); }
defcounting_sort_naive(nums): m = 0 for num in nums: m = max(m, num) counter = [0] * (m + 1) for num in nums: counter[num] += 1 i = 0 for num inrange(m + 1): for _ inrange(counter[num]): nums[i] = num i += 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
voidcountingSortNaive(vecctor<int>& nums){ int m = 0; for (int num : nums){ m = max(m, num); } vector<int> counter(m + 1, 0); for (int num : nums){ counter[num]++; } int i = 0; for (int num = 0; num < m + 1; i++){ for (int j = 0; j < counter[num]; j++){ nums[i++] = num; } } }
defcounting_sort(nums): m = max(nums) counter = [0] * (m + 1) for num in nums: counter[num] += 1 # 计算前缀和 for i inrange(m): counter[i + 1] += counter[i] n = len(nums) res = [0] * n for i inrange(n - 1, -1, -1): num = nums[i] res[counter[num] - 1] = num counter[num] -= 1 # 覆盖原数组 for i inrange(n): nums[i] = res[i]
voidcountingSort(vector<int>& nums){ int m = 0; for (int num : nums){ m = max(num, m); }
vector<int> counter(m + 1, 0); for (int num : nums){ counter[num]++; }
for (int i = 0; i < m; i++){ counter[i+1] += counter[i]; }
int n = nums.size(); vector<int> res(n); for (int i = n - 1; i >= 0; --i){ int num = nums[i]; res[counter[num] - 1] = num; counter[num]--; } nums = res; }
defcounting_sort_digit(nums: list[int], exp): """计数排序(根据nums第k位排序)""" counter = [0] * 10 n = len(nums) for i inrange(n): d = digit(nums[i], exp) counter[d] += 1 for i inrange(1, 10): counter[i] += counter[i - 1] res = [0] * n for i inrange(n - 1, -1, -1): d = digit(nums[i], exp) j = counter[d] - 1 res[j] = nums[i] counter[d] -= 1 for i inrange(n): nums[i] = res[i]
defradix_sort(nums): m = max(nums) exp = 1 while exp <= m: counting_sort_digit(nums, exp) exp *= 10
voidcountingSortDigit(vector<int>& nums, int exp){ vector<int> counter(10, 0); int n = nums.size(); for (int i = 0; i < size; i++){ int d = digit(nums[i], exp); counter[d]++; }
for (int i = 0; i < 9; i++){ counter[i + 1] = counter[i]; }
vector<int> res(n, 0); for (int i = n - 1; i >= 0; i--){ int d = digit(nums[i], exp); int j = counter[d]; res[j] = nums[i]; counter[d]--; }
nums = res; }
voidradixCounting(vector<int>& nums){ int m = *max_element(nums.begin(), nums.end());
intdigit(int num intexp){ return (num / exp) % 10; }
voidcountingSortDigit(int nums[], int size, intexp){ int *counter = (int*)calloc(10, sizeof(10)); for (int i = 0; i < size; i++){ int d = digit(nums[i], exp); counter[d]++; }
for (int i = 0; i < 9; i++){ counter[i + 1] = counter[i]; }
int* res = (int*)calloc(size, 0); for (int i = size - 1; i >= 0; i--){ int d = digit(nums[i], exp); int j = counter[d]; res[j] = nums[i]; counter[d]--; }
memcpy(nums, res, size * sizeof(int));
free(res); }
voidradixSort(int nums[], int size){ int max = INT32_MIN; for (size_t i = 0; i < size; i++){ if(nums[i] > max){ max = nums[i]; } }