首页 > 代码库 > 冒泡排序的改进-鸡尾酒排序

冒泡排序的改进-鸡尾酒排序

#include <iostream>using namespace std;void Cocktail(int *arr, int size){    int tail = size - 1;    for(int i = 0; i < tail;){        for(int j = tail; j > i; j--){            if(arr[j] < arr[j-1]){                int temp = arr[j];                arr[j] = arr[j-1];                arr[j-1] = temp;            }        }        ++i;        for(int j = i; j < tail; j++){            if(arr[j] > arr[j+1]){                int temp = arr[j];                arr[j] = arr[j+1];                arr[j+1] = temp;            }        }        --tail;    }    for(int i = 0; i < size; i++)        cout << arr[i] << \t;        cout << endl;}int main(){    int arr[] = {1,4,5,56,5,6,14,98,9,5,233,32,6,48,8,98,95,5,35,2,3};    Cocktail(arr, sizeof(arr)/sizeof(int));    return 0;}

 

冒泡排序的改进-鸡尾酒排序