You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.4 KiB
63 lines
1.4 KiB
#include <stdio.h>
|
|
|
|
//直接插入排序
|
|
void insertSort(int arr[], int n) {
|
|
int i, j, temp;
|
|
for (i = 1; i < n; i++) {
|
|
if (arr[i] < arr[i - 1]) {
|
|
temp = arr[i];
|
|
for (j = i - 1; j >= 0 && arr[j] > temp; --j) {
|
|
arr[j + 1] = arr[j];
|
|
}
|
|
arr[j + 1] = temp;
|
|
}
|
|
}
|
|
}
|
|
|
|
//交换排序 中的 冒泡排序
|
|
void bubbleSort(int arr[], int n) {
|
|
int i, j, flag, temp;
|
|
for (i = 0; i < n - 1; i++) {
|
|
flag = 0;
|
|
for (j = n - 1; j > i; j--) {
|
|
if (arr[j] < arr[j - 1]) {
|
|
temp = arr[j];
|
|
arr[j] = arr[j - 1];
|
|
arr[j - 1] = temp;
|
|
flag = 1;
|
|
}
|
|
}
|
|
if (flag == 0) {
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
//交换排序中的 快速排序
|
|
int partition(int arr[], int low, int high) {
|
|
int pivot = arr[low];
|
|
while (low < high) {
|
|
while (low < high && arr[high] >= pivot) {
|
|
--high;
|
|
}
|
|
arr[low] = arr[high];
|
|
while (low < high && arr[low] <= pivot) {
|
|
++low;
|
|
}
|
|
arr[high] = arr[low];
|
|
}
|
|
arr[low] = pivot;
|
|
return low;
|
|
}
|
|
|
|
|
|
void quickSort(int arr[], int low, int high) {
|
|
if (low < high) {
|
|
int pivotpos = partition(arr, low, high);
|
|
quickSort(arr, low, pivotpos - 1);
|
|
quickSort(arr, pivotpos + 1, high);
|
|
}
|
|
}
|
|
|
|
|