跳转至

鸡尾酒排序

一种改进的冒泡排序,原理是对要排序的数组进行双向冒泡,双向冒泡排序又称鸡尾酒排序。

原理是:大数向后排,小数向前排。

代码

#include<iostream>
using namespace std;
void CockTail(int num[],int n) {
    int tail = n - 1;
    int i;
    for ( i = 0; i < tail;) {   //将最小的数排到前面
        for ( int j =tail; j > i; j--) {
            if (num[j] < num[j - 1]) {
                int temp = num[j];
                num[j] = num[j - 1];
                num[j - 1] = temp;
            }
        }
        i++;          //最小数已排好 向后+1
        for (int j = i; j <tail ; j++) {   //将最大的数排到后面
            if (num[j] > num[j + 1]) {
                int temp = num[j];
                num[j] = num[j + 1];
                num[j + 1] = temp;
            }
        }
        tail--;  //最大数已排好 向前移动
    }

}
void main(){
    int num[12] = { 10,6,2,33,15,12,23,76,1,54,22,9 };
    CockTail(num, 12);
    for (auto x : num) {
        cout << x << " ";
    }
    cout << endl;
    system("pause");
}

本文总阅读量本站访客数人次