算法--排序
来源:互联网 发布:version mac 破解版 编辑:程序博客网 时间:2024/05/16 06:48
- // 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);
}
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- 排序算法
- iOS单一页面横屏显示
- sftp上传遇到的问题
- 二、串口打印
- exports与module.exports的区别
- CSS中父div与子div——子div有内容,父div高度却为0?
- 算法--排序
- 微信web协议分析和实现微信机器人(微信网页版 wx2.qq.com)
- 【Google官方译文】Styles and Themes
- 最大连续子序列和
- Arduino学习(六) 继电器实验
- Service服务详解以及如何使service服务不被杀死
- LA3708-Graveyard
- Docke 1.12 基础篇:48条命令(1)
- Arduino学习(七)中断