main
lwy 2 years ago
parent 3cd4510428
commit 4cb59bb971

@ -1,15 +0,0 @@
#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;
}
}
printf("Over\n");
}

@ -0,0 +1,62 @@
#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);
}
}

@ -6,16 +6,16 @@
//#include "Stack/LinkStack.c" //#include "Stack/LinkStack.c"
//#include "Queue/SeqQueue.c" //#include "Queue/SeqQueue.c"
//#include "Queue/LinkQueue.c" //#include "Queue/LinkQueue.c"
#include "Sort/InsertSort.c" #include "Sort/Sort.c"
int main() { int main() {
int arr[] = { 21, 3, 92, 6, 9}; int arr[] = { 1, 24, 3, 27, 2};
int n = sizeof(arr) / sizeof(arr[0]); int n = sizeof(arr) / sizeof(arr[0]);
printf("%d\n",n); printf("%d\n",n);
insertSort(arr,n); quickSort(arr,0,4);
for (int i = 1; i < n; i++) { for (int i = 0; i < n; i++) {
printf("%d ", arr[i]); printf("%d ", arr[i]);
} }
return 0; return 0;

Loading…
Cancel
Save