<pre>
// 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);
}
//打印数组
void printArray(const int array[], int count) {
for (int i = 0; i < count; i++) {
printf("%d\n", array[i]);
}
}
//
//交换2个整数(参数是指针类型)
//void swap(int *p1, int *p2) {
//
// int temp = 0;
// temp = *p1;
// *p1 = *p2;
// *p2 = temp;
//
//}
//
//打乱一个数组
//void breakArray(int array[], int count) {
//
// for (int i = count - 1; i > 0; i--) {
// int random = arc4random() % (i + 1);
// swap(array + i, array + random);
// }
//}
//桶排序
int array[] = {100, 88, 1, 55, 23, 44, 7, 88};
int count = sizeof(array) / sizeof(array[0]);
int max = array[0];
for (int i = 1; i < count; i++) {
if (max < array[i]) {
max = array[i];
}
}
int min = array[0];
for (int i = 1; i < count; i++) {
if (min > array[i]) {
min = array[i];
}
}
max++;//最大值+1才是brray的元素个数
int brray[max];//brray数组元素个数=array数组元素最大的值
for (int i = 0; i < max; i++) {
brray[i] = 0;
}
for(int i = 0; i < count; i++) {
for (int j = 0; j < max; j++) {
if (array[i] == j) {
brray[j]++;
}
}
}
for(int i = 0 ; i < max; i++) {
while (brray[i] != 0) {
brray[i]--;
printf("[%2d]\n", i);
}
}
</pre>
网友评论