#include<iostream>
#include<algorithm>
#include<cstdlib>
using namespace std;
template<typename T>
void cocktailSort(T arr,int len)
{
int j, left = 0, right = len - 1;
while (left < right)
{
for (j = left; j < right; j++)
{
if (arr[j] > arr[j + 1])
{
swap(arr[j], arr[j + 1]);
}
}
right--;
for (j = right; j > left; j--)
{
if (arr[j - 1] > arr[j])
{
swap(arr[j-1],arr[j]);
}
}
left++;
}
}
int main()
{
int a[] = { 12,232,34,34,45,4,6,56,53,4,423,2 };
int n = sizeof(a) / sizeof(int);
cocktailSort(a,n);
for (int i = 0; i < n; i++)
{
printf("%d\t",a[i]);
}
getchar();
return 0;
}
010.鸡尾酒排序
最新推荐文章于 2024-09-08 12:26:17 发布