排序
比较

分类

源码
C版
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #include <math.h> |
| #include <stdio.h> |
| #include <stdlib.h> |
| |
| int cmp(const void* a, const void* b) { return *(int*)(a) - *(int*)b; } |
| |
| void swap(int* a, int* b) { |
| int temp = *a; |
| *a = *b; |
| *b = temp; |
| } |
| |
| |
| void swap2(int* a, int* b) { |
| *a = *a ^ *b; |
| *b = *a ^ *b; |
| *a = *a ^ *b; |
| } |
| |
| void display(int* ary, int size) { |
| for (int i = 0; i < size; i++) { |
| printf("%d ", ary[i]); |
| } |
| puts(""); |
| } |
| |
| |
| void bubbleSort(int* array, int size) { |
| |
| for (int i = 0; i < size - 1; i++) { |
| |
| bool isSorted = true; |
| |
| for (int j = 0; j < size - 1 - i; j++) { |
| |
| if (array[j] > array[j + 1]) { |
| |
| isSorted = false; |
| swap(&array[j], &array[j + 1]); |
| } |
| } |
| if (isSorted == true) break; |
| } |
| display(array, size); |
| } |
| |
| void quickSortRecursive(int* array, int left, int right) { |
| if (left >= right) return; |
| int i = left; |
| int j = right; |
| |
| int key = array[left]; |
| |
| while (i < j) { |
| |
| while (i < j && array[j] >= key) { |
| j--; |
| } |
| |
| if (i < j) { |
| array[i] = array[j]; |
| |
| i++; |
| } |
| |
| while (i < j && array[i] <= key) { |
| i++; |
| } |
| |
| if (i < j) { |
| array[j] = array[i]; |
| j--; |
| } |
| } |
| |
| array[i] = key; |
| quickSortRecursive(array, left, i - 1); |
| quickSortRecursive(array, i + 1, right); |
| } |
| |
| |
| void quickSort(int* array, int size) { |
| quickSortRecursive(array, 0, size - 1); |
| display(array, size); |
| } |
| |
| |
| void selectionSort(int* array, int size) { |
| |
| for (int i = 0; i < size - 1; i++) { |
| int minIndex = i; |
| |
| for (int j = i + 1; j < size; j++) { |
| if (array[j] < array[minIndex]) { |
| minIndex = j; |
| } |
| } |
| |
| if (minIndex != i) { |
| swap(&array[i], &array[minIndex]); |
| } |
| } |
| display(array, size); |
| } |
| |
| |
| void adjustHeap(int* array, int currentIndex, int size) { |
| int temp = array[currentIndex]; |
| int leftChildIndex = 2 * currentIndex + 1; |
| |
| while (leftChildIndex <= (size - 1)) { |
| |
| if (leftChildIndex < (size - 1) && |
| array[leftChildIndex] < array[leftChildIndex + 1]) { |
| leftChildIndex++; |
| } |
| |
| if (array[leftChildIndex] <= temp) break; |
| |
| array[currentIndex] = array[leftChildIndex]; |
| |
| currentIndex = leftChildIndex; |
| leftChildIndex = 2 * currentIndex + 1; |
| } |
| array[currentIndex] = temp; |
| } |
| |
| |
| void heapSort(int* array, int size) { |
| |
| for (int i = (size - 2) / 2; i >= 0; i--) { |
| adjustHeap(array, i, size); |
| } |
| printf("大顶堆:"); |
| display(array, size); |
| |
| for (int i = 1; i < size; i++) { |
| swap(&array[0], &array[size - i]); |
| |
| adjustHeap(array, 0, size - i); |
| } |
| display(array, size); |
| } |
| |
| |
| void insertionSort(int* array, int size) { |
| |
| |
| for (int i = 1; i < size; i++) { |
| |
| int temp = array[i]; |
| |
| |
| int j = i - 1; |
| while (j >= 0 && (array[j] > temp)) { |
| |
| array[j + 1] = array[j]; |
| j--; |
| } |
| array[j + 1] = temp; |
| } |
| |
| display(array, size); |
| } |
| |
| |
| void binaryInsertionSort(int* array, int size) { |
| for (int i = 1; i < size; i++) { |
| int temp = array[i]; |
| |
| int left = 0; |
| int right = i - 1; |
| int mid; |
| while (left <= right) { |
| mid = left + (right - left) / 2; |
| if (array[mid] >= temp) { |
| right = mid - 1; |
| } else { |
| left = mid + 1; |
| } |
| } |
| |
| |
| for (int j = i - 1; j >= left; j--) { |
| array[j + 1] = array[j]; |
| } |
| array[left] = temp; |
| } |
| display(array, size); |
| } |
| |
| |
| void shellSort(int* array, int size) { |
| |
| int gap = size / 2; |
| while (gap > 0) { |
| |
| |
| for (int i = gap; i < size; i++) { |
| int temp = array[i]; |
| int j = i - gap; |
| while (j >= 0 && array[j] > temp) { |
| array[j + gap] = array[j]; |
| j -= gap; |
| } |
| array[j + gap] = temp; |
| } |
| printf("gap:%d\n", gap); |
| display(array, size); |
| gap = gap / 2; |
| } |
| } |
| |
| |
| void mergeSort_conquer(int* array, int left, int mid, int right, int* temp) { |
| |
| int i = left; |
| int j = mid + 1; |
| int index = 0; |
| while (i <= mid && j <= right) { |
| if (array[i] < array[j]) { |
| temp[index++] = array[i++]; |
| } else { |
| temp[index++] = array[j++]; |
| } |
| } |
| |
| while (i <= mid) { |
| temp[index++] = array[i++]; |
| } |
| while (j <= right) { |
| temp[index++] = array[j++]; |
| } |
| |
| index = 0; |
| while (left <= right) { |
| array[left++] = temp[index++]; |
| } |
| } |
| |
| |
| void mergeSort_divide(int* array, int left, int right, int* temp) { |
| if (left >= right) return; |
| int mid = left + (right - left) / 2; |
| |
| mergeSort_divide(array, left, mid, temp); |
| |
| mergeSort_divide(array, mid + 1, right, temp); |
| |
| mergeSort_conquer(array, left, mid, right, temp); |
| } |
| |
| |
| void mergeSort(int* array, int size) { |
| int* temp = (int*)malloc(sizeof(int) * size); |
| mergeSort_divide(array, 0, size - 1, temp); |
| display(array, size); |
| } |
| |
| |
| |
| |
| void countingSort(int* array, int size) { |
| int* frequency = (int*)calloc(11, sizeof(int)); |
| |
| for (int i = 0; i < size; i++) { |
| frequency[array[i]]++; |
| } |
| display(frequency, 11); |
| |
| for (int i = 1; i < 11; i++) { |
| frequency[i] += frequency[i - 1]; |
| } |
| display(frequency, 11); |
| |
| int* sorted = (int*)calloc(size, sizeof(int)); |
| |
| for (int i = size - 1; i >= 0; i--) { |
| |
| |
| |
| sorted[--frequency[array[i]]] = array[i]; |
| printf("frequency:\t"); |
| display(frequency, 11); |
| printf("sorted:\t\t"); |
| display(sorted, size); |
| } |
| } |
| |
| typedef struct { |
| int** bucket; |
| int row; |
| int column; |
| int* index; |
| } Bucket; |
| |
| |
| |
| void bucketSort(int* array, int size) { |
| Bucket* b = (Bucket*)malloc(sizeof(Bucket)); |
| b->row = 5; |
| b->column = 3; |
| b->index = (int*)calloc(b->row, sizeof(int)); |
| b->bucket = (int**)malloc(sizeof(int) * b->row); |
| for (int i = 0; i < b->row; i++) { |
| b->bucket[i] = (int*)malloc(sizeof(int) * b->column); |
| } |
| |
| for (int i = 0; i < size; i++) { |
| int index = array[i] / 10; |
| b->bucket[index][b->index[index]++] = array[i]; |
| } |
| size = 0; |
| |
| for (int i = 0; i < b->row; i++) { |
| qsort(b->bucket[i], b->column, sizeof(int), cmp); |
| for (int j = 0; j < b->column; j++) { |
| array[size++] = b->bucket[i][j]; |
| } |
| } |
| display(array, size); |
| } |
| |
| |
| void radixSort(int* array, int size) { |
| Bucket* b = (Bucket*)malloc(sizeof(Bucket)); |
| b->row = 10; |
| b->column = 10; |
| b->index = (int*)calloc(b->row, sizeof(int)); |
| |
| b->bucket = (int**)malloc(sizeof(int) * b->row); |
| for (int i = 0; i < b->row; i++) { |
| b->bucket[i] = (int*)malloc(sizeof(int) * b->column); |
| } |
| |
| |
| for (int i = 0; i < 3; i++) { |
| |
| for (int j = 0; j < size; j++) { |
| int index = (array[j] / (int)pow(10, i)) % 10; |
| b->bucket[index][b->index[index]++] = array[j]; |
| } |
| |
| int returnSize = 0; |
| for (int j = 0; j < b->row; j++) { |
| for (int k = 0; k < b->index[j]; k++) { |
| array[returnSize++] = b->bucket[j][k]; |
| } |
| |
| b->index[j] = 0; |
| } |
| } |
| |
| display(array, size); |
| } |
| |
| void testSort() { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| int c[] = {53, 3, 542, 748, 14, 77, 214, 154, 63, 616}; |
| radixSort(c, 10); |
| } |
| void daulPivotQuickSortRecursive(int* array, int left, int right) { |
| if (left >= right) return; |
| int i = left; |
| int j = right; |
| int k = i + 1; |
| |
| |
| |
| |
| |
| |
| if (array[left] > array[right]) { |
| swap(&array[left], &array[right]); |
| } |
| int pivot1 = array[left]; |
| int pivot2 = array[right]; |
| |
| while (k < j) { |
| if (array[k] > pivot1 && array[k] < pivot2) { |
| k++; |
| } else if (array[k] <= pivot1) { |
| |
| swap(&array[++i], &array[k++]); |
| } else if (array[k] >= pivot2) { |
| |
| while (k < j && array[j] >= pivot2) { |
| j--; |
| } |
| |
| swap(&array[k], &array[j]); |
| } |
| } |
| |
| |
| swap(&array[left], &array[i]); |
| swap(&array[right], &array[j]); |
| |
| |
| daulPivotQuickSortRecursive(array, left, i - 1); |
| daulPivotQuickSortRecursive(array, i + 1, j - 1); |
| daulPivotQuickSortRecursive(array, j + 1, right); |
| } |
| |
| |
| void daulPivotQuickSort(int* array, int size) { |
| daulPivotQuickSortRecursive(array, 0, size - 1); |
| display(array, size); |
| } |
Java版
| package sort; |
| |
| import java.util.ArrayList; |
| import java.util.Arrays; |
| import java.util.Collections; |
| import java.util.List; |
| |
| public class Sort { |
| |
| public static boolean check(int count) { |
| for (int i = 0; i < count; i++) { |
| if (!isArrayEqual()) |
| return false; |
| } |
| return true; |
| } |
| |
| public static boolean isArrayEqual() { |
| int size = (int) (Math.random() * 100); |
| int[] array = generateRandomArray(size); |
| int[] temp = Arrays.copyOf(array, array.length); |
| Arrays.sort(array); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| radixSort(temp); |
| |
| return Arrays.equals(array, temp); |
| } |
| |
| |
| public static int[] generateRandomArray(int size) { |
| int[] res = new int[size]; |
| for (int i = 0; i < size; i++) { |
| res[i] = (int) (Math.random() * 1000); |
| } |
| return res; |
| } |
| |
| public static void swap(int[] array, int a, int b) { |
| int temp = array[a]; |
| array[a] = array[b]; |
| array[b] = temp; |
| } |
| |
| public static void bubbleSort(int[] array) { |
| for (int i = 0; i < array.length - 1; i++) { |
| boolean isSorted = true; |
| for (int j = 1; j < array.length - i; j++) { |
| if (array[j - 1] > array[j]) { |
| swap(array, j - 1, j); |
| isSorted = false; |
| } |
| } |
| if (isSorted) |
| break; |
| } |
| } |
| |
| public static void quickSort(int[] array) { |
| quickSortRecursive(array, 0, array.length - 1); |
| } |
| |
| public static void quickSortRecursive(int[] array, int left, int right) { |
| if (left >= right) |
| return; |
| int i = left; |
| int j = right; |
| int pivot = array[left]; |
| while (i < j) { |
| while (i < j && array[j] >= pivot) { |
| j--; |
| } |
| if (i < j) { |
| array[i] = array[j]; |
| i++; |
| } |
| while (i < j && array[i] <= pivot) { |
| i++; |
| } |
| if (i < j) { |
| array[j] = array[i]; |
| j--; |
| } |
| } |
| array[i] = pivot; |
| quickSortRecursive(array, left, i - 1); |
| quickSortRecursive(array, i + 1, right); |
| } |
| |
| |
| public static void daulPivotQuickSort(int[] array) { |
| daulPivotQuickSortRecursive(array, 0, array.length - 1); |
| } |
| |
| public static void daulPivotQuickSortRecursive(int[] array, int left, int right) { |
| if (left >= right) |
| return; |
| int i = left; |
| int j = right; |
| int k = left + 1; |
| |
| |
| |
| |
| if (array[left] > array[right]) { |
| swap(array, left, right); |
| } |
| int pivot1 = array[left]; |
| int pivot2 = array[right]; |
| |
| while (k < j) { |
| int temp = array[k]; |
| if (temp > pivot1 && temp < pivot2) { |
| k++; |
| } else if (temp <= pivot1) { |
| swap(array, ++i, k++); |
| } else if (temp >= pivot2) { |
| while (k < j && array[j] >= pivot2) { |
| j--; |
| } |
| if (k < j) { |
| swap(array, k, j); |
| } |
| } |
| } |
| |
| swap(array, left, i); |
| swap(array, j, right); |
| daulPivotQuickSortRecursive(array, left, i - 1); |
| daulPivotQuickSortRecursive(array, i + 1, j - 1); |
| daulPivotQuickSortRecursive(array, j + 1, right); |
| } |
| |
| public static void selectionSort(int[] array) { |
| for (int i = 0; i < array.length - 1; i++) { |
| int minIndex = i; |
| for (int j = i + 1; j < array.length; j++) { |
| if (array[minIndex] > array[j]) { |
| minIndex = j; |
| } |
| } |
| if (minIndex != i) { |
| swap(array, i, minIndex); |
| } |
| } |
| } |
| |
| public static void heapSort(int[] array) { |
| for (int i = (array.length - 2) / 2; i >= 0; i--) { |
| adjustHeap(array, i, array.length); |
| } |
| for (int i = 0; i < array.length - 1; i++) { |
| swap(array, 0, array.length - 1 - i); |
| adjustHeap(array, 0, array.length - 1 - i); |
| } |
| } |
| |
| public static void adjustHeap(int[] array, int currentIndex, int len) { |
| int temp = array[currentIndex]; |
| int leftChildIndex = 2 * currentIndex + 1; |
| while (leftChildIndex <= (len - 1)) { |
| if (leftChildIndex < (len - 1) && array[leftChildIndex] < array[leftChildIndex + 1]) { |
| leftChildIndex++; |
| } |
| if (array[leftChildIndex] <= temp) |
| break; |
| array[currentIndex] = array[leftChildIndex]; |
| currentIndex = leftChildIndex; |
| leftChildIndex = 2 * currentIndex + 1; |
| } |
| array[currentIndex] = temp; |
| } |
| |
| public static void insertionSort(int[] array) { |
| for (int i = 1; i < array.length; i++) { |
| int temp = array[i]; |
| int j = i - 1; |
| while (j >= 0 && array[j] > temp) { |
| array[j + 1] = array[j]; |
| j--; |
| } |
| array[j + 1] = temp; |
| } |
| } |
| |
| public static void binaryInsertionSrot(int[] array) { |
| for (int i = 1; i < array.length; i++) { |
| int temp = array[i]; |
| int left = 0; |
| int right = i - 1; |
| int mid; |
| while (left <= right) { |
| mid = left + (right - left) / 2; |
| if (array[mid] >= temp) { |
| right = mid - 1; |
| } else { |
| left = mid + 1; |
| } |
| } |
| for (int j = i - 1; j >= left; j--) { |
| array[j + 1] = array[j]; |
| } |
| array[left] = temp; |
| } |
| } |
| |
| public static void shellSort(int[] array) { |
| int gap = array.length / 2; |
| while (gap > 0) { |
| for (int i = gap; i < array.length; i += gap) { |
| int temp = array[i]; |
| int j = i - gap; |
| while (j >= 0 && array[j] > temp) { |
| array[j + gap] = array[j]; |
| j -= gap; |
| } |
| array[j + gap] = temp; |
| } |
| gap /= 2; |
| } |
| } |
| |
| public static void mergeSort(int[] array) { |
| int[] temp = new int[array.length]; |
| mergeSort_divide(array, 0, array.length - 1, temp); |
| } |
| |
| public static void mergeSort_divide(int[] array, int left, int right, int[] temp) { |
| if (left >= right) |
| return; |
| int mid = left + (right - left) / 2; |
| mergeSort_divide(array, left, mid, temp); |
| mergeSort_divide(array, mid + 1, right, temp); |
| mergeSort_conquer(array, left, mid, right, temp); |
| } |
| |
| public static void mergeSort_conquer(int[] array, int left, int mid, int right, int[] temp) { |
| if (left >= right) |
| return; |
| int i = left; |
| int j = mid + 1; |
| int index = 0; |
| while ((i <= mid && j <= right)) { |
| if (array[i] <= array[j]) { |
| temp[index++] = array[i++]; |
| } else { |
| temp[index++] = array[j++]; |
| } |
| } |
| while (i <= mid) { |
| temp[index++] = array[i++]; |
| } |
| while (j <= right) { |
| temp[index++] = array[j++]; |
| } |
| index = 0; |
| while (left <= right) { |
| array[left++] = temp[index++]; |
| } |
| } |
| |
| public static void countingSort(int[] array) { |
| int[] frequency = new int[1000]; |
| for (int j : array) { |
| frequency[j]++; |
| } |
| for (int i = 1; i < 1000; i++) { |
| frequency[i] += frequency[i - 1]; |
| } |
| int[] sorted = new int[array.length]; |
| for (int j : array) { |
| sorted[--frequency[j]] = j; |
| } |
| System.arraycopy(sorted, 0, array, 0, array.length); |
| } |
| |
| public static void bucketSort(int[] array) { |
| |
| |
| List<List<Integer>> buckets = new ArrayList<>(10); |
| for (int i = 0; i < 10; i++) { |
| buckets.add(new ArrayList<>()); |
| } |
| for (int i : array) { |
| int index = i / 100; |
| buckets.get(index).add(i); |
| } |
| |
| int index = 0; |
| for (List<Integer> bucket : buckets) { |
| Collections.sort(bucket); |
| for (Integer integer : bucket) { |
| array[index++] = integer; |
| } |
| } |
| } |
| |
| public static void radixSort(int[] array) { |
| |
| |
| List<List<Integer>> buckets = new ArrayList<>(10); |
| for (int i = 0; i < 10; i++) { |
| buckets.add(new ArrayList<>()); |
| } |
| |
| for (int i = 0; i < 3; i++) { |
| for (int j : array) { |
| int index = (j / (int) Math.pow(10, i)) % 10; |
| buckets.get(index).add(j); |
| } |
| |
| int index = 0; |
| for (List<Integer> bucket : buckets) { |
| for (Integer integer : bucket) { |
| array[index++] = integer; |
| } |
| bucket.clear(); |
| } |
| } |
| } |
| |
| public static void main(String[] args) { |
| int[] a = {1, 0, 7, 2, 10, 5, 2, 8, 6, 0}; |
| System.out.println(Arrays.toString(a)); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| radixSort(a); |
| |
| System.out.println(Arrays.toString(a)); |
| System.out.println(check(10000)); |
| } |
| } |
C++版
| void quickSort(vector<int> &array, int left, int right) { |
| if (left >= right) return; |
| int i = left; |
| int j = right; |
| int pivot = array[left]; |
| |
| while (i < j) { |
| while (i < j && pivot <= array[j]) { |
| j--; |
| } |
| if (i < j) { |
| array[i] = array[j]; |
| i++; |
| } |
| while (i < j && pivot >= array[i]) { |
| i++; |
| } |
| if (i < j) { |
| array[j] = array[i]; |
| j--; |
| } |
| } |
| array[i] = pivot; |
| quickSort(array, left, i - 1); |
| quickSort(array, i + 1, right); |
| } |
| |
| void adjustHeap(vector<int> &array, int currentIndex, int len) { |
| |
| int temp = array[currentIndex]; |
| |
| int leftChildIndex = 2 * currentIndex + 1; |
| |
| |
| while (leftChildIndex <= (len - 1)) { |
| |
| if (leftChildIndex < (len - 1) |
| && (array[leftChildIndex] < array[leftChildIndex + 1])) |
| leftChildIndex++; |
| |
| |
| if (array[leftChildIndex] <= temp) break; |
| |
| array[currentIndex] = array[leftChildIndex]; |
| currentIndex = leftChildIndex; |
| leftChildIndex = 2 * currentIndex + 1; |
| } |
| |
| array[currentIndex] = temp; |
| } |
| |
| |
| void headSort(vector<int> &array) { |
| |
| |
| for (int i = array.size() / 2 - 1; i >= 0; i--) { |
| adjustHeap(array, i, array.size()); |
| } |
| |
| |
| |
| for (int len = array.size() - 1; len >= 1; len--) { |
| |
| swap(array[0], array[len]); |
| |
| adjustHeap(array, 0, len); |
| } |
| } |
| #include <vector> |
| #include <algorithm> |
| |
| using namespace std; |
| |
| class Solution { |
| public: |
| |
| static const int BASE = 10; |
| static const int MAXN = 50001; |
| static int help[MAXN]; |
| static int cnts[BASE]; |
| |
| |
| vector<int> sortArray(vector<int> &arr) { |
| if (arr.size() <= 1) return arr; |
| int n = arr.size(); |
| int _min = arr[0]; |
| |
| for (int i = 1; i < n; i++) |
| _min = min(_min, arr[i]); |
| int _max = 0; |
| |
| for (int i = 0; i < n; i++) { |
| arr[i] -= _min; |
| _max = max(_max, arr[i]); |
| } |
| |
| radixSort(arr, n, bits(_max)); |
| |
| for (int i = 0; i < n; i++) |
| arr[i] += _min; |
| return arr; |
| } |
| |
| |
| int bits(int number) { |
| int ans = 0; |
| while (number > 0) { |
| ans++; |
| number /= BASE; |
| } |
| return ans; |
| } |
| |
| |
| void radixSort(vector<int> &arr, int n, int bits) { |
| for (int offset = 1; bits > 0; offset *= BASE, bits--) { |
| fill(cnts, cnts + BASE, 0); |
| for (int i = 0; i < n; i++) |
| cnts[(arr[i] / offset) % BASE]++; |
| |
| for (int i = 1; i < BASE; i++) |
| cnts[i] += cnts[i - 1]; |
| |
| for (int i = n - 1; i >= 0; i--) |
| |
| |
| help[--cnts[(arr[i] / offset) % BASE]] = arr[i]; |
| for (int i = 0; i < n; i++) |
| arr[i] = help[i]; |
| } |
| } |
| }; |
| |
| int Solution::help[MAXN] = {0}; |
| int Solution::cnts[Solution::BASE] = {0}; |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步