#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; }
时间: 2024-10-22 10:07:50