diff --git a/Sort/InsertSort.c b/Sort/InsertSort.c deleted file mode 100644 index eadc1cf..0000000 --- a/Sort/InsertSort.c +++ /dev/null @@ -1,15 +0,0 @@ -#include -//直接插入排序 -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; - } - } - printf("Over\n"); -} diff --git a/Sort/Sort.c b/Sort/Sort.c new file mode 100644 index 0000000..29dac03 --- /dev/null +++ b/Sort/Sort.c @@ -0,0 +1,62 @@ +#include + +//直接插入排序 +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); + } +} + + diff --git a/main.c b/main.c index fdee038..a127a3f 100644 --- a/main.c +++ b/main.c @@ -6,16 +6,16 @@ //#include "Stack/LinkStack.c" //#include "Queue/SeqQueue.c" //#include "Queue/LinkQueue.c" -#include "Sort/InsertSort.c" +#include "Sort/Sort.c" int main() { - int arr[] = { 21, 3, 92, 6, 9}; + int arr[] = { 1, 24, 3, 27, 2}; int n = sizeof(arr) / sizeof(arr[0]); printf("%d\n",n); - insertSort(arr,n); - for (int i = 1; i < n; i++) { + quickSort(arr,0,4); + for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0;