输入10个数字,应用鸡尾酒排序算法用c#实现
请用c#写出一个鸡尾酒冒泡排序算法,要求输入10个整数,输出排序结果。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace CockTailSorter
{
class Program
{
private static int[] myArray;//定义数组
private static int arraySize;//定义数组大小
//调用排序
public static void Sort(int[] a)
{
myArray = a;
arraySize = myArray.Length;
CockTailSort(myArray);
}
//鸡尾酒冒泡排序
private static void CockTailSort(int[] myArray)
{
int low, up, index, i;
low = 0;
up = myArray.Length - 1;
index = low;
while (up > low)
{
for (i = low; i < up; i++)//从上到下
{
if (myArray[i] > myArray[i + 1])
{
Swap(ref myArray[i],ref myArray[i + 1]);
index = i;
}
}
up = index;//记录最后一个交换的位置
for (i = up; i > low; i--)
{
if (myArray[i] < myArray[i - 1])
{
Swap(ref myArray[i], ref myArray[i - 1]);
index = i;
}
}
low = index;
}
}
//交换函数
private static void Swap(ref int left, ref int right)
{
int temp;
temp = left;
left = right;
right = temp;
}
//主函数
static void Main(string[] args)
{
int[] a = new int[] { 4, 2, 4, 12, -2, 0, 7, 2, 8, 10 };
Program.Sort(a);
for (int i = 0; i < myArray.Length; i++)
{
Console.WriteLine(a[i]);
}
}
}
}
原创文章转载请注明出处:云飞扬IT的blog





