算法--排序

// 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); 

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值