-
// int array[10] = {10, 9, 7, 6, 4, 8, 5, 3, 1, 2};
// int count = 10;
-
冒泡排序
//void bubbleSort(int array[], int count) {
//
// int flag = 0;
// for (int i = 0; i < count - 1 && flag == 0; i++) {
// flag = 1;
// for (int j = 0; j < count - 1 - i; j++) {
// if (array[j] > array[j + 1]) {
// swap(&array[j], &array[j + 1]);
// flag = 0;
// }
// }
// }
//} -
选择排序
//void selectionSort(int array[], int count) {
//
// for (int i = 0; i < count - 1; i++) {
// int minIndex = i;
// for (int j = minIndex + 1; j < count; j++) {
// if (array[minIndex] > array[j]) {
// minIndex = j;
// }
// }
// if (minIndex != i) {
// swap(&array[minIndex], &array[i]);
// }
// }
//}
// -
插入排序
void insertionSort(int array[], int count) {for (int i = 1; i < count; i++) {
int temp = array[i];
int j = i;
while (j > 0 && array[j - 1] > temp) {
array[j] = array[j - 1];
j–;
}
array[j] = temp;
}
}
折半排序
//int halfSeek(int array[], int count, int target) {
//
// int start = 0;
// int end = count - 1;
// int mid = 0;
//
// while (start <= end) {
// mid = (start + end) / 2;
// if (array[mid] > target) {
// end = mid - 1;
// }
// else if (array[mid] < target) {
// start = mid + 1;
// }
// else {
// return mid;
// }
// }
//
// return -1;
//
//}
快速排序
void quickSort(int array[], int count) {
int start = 0;
int end = count - 1;
int temp = array[start];
if (count < 2) {
return;
}
while (start < end) {
while (start < end && array[end] > temp) {
end--;
}
if (start < end) {
array[start] = array[end];
start++;
}
while (start < end && array[start] < temp) {
start++;
}
if (start < end) {
array[end] = array[start];
end--;
}
}
array[start] = temp;
quickSort(array, start);
quickSort(array + start + 1, count - start - 1);
}