您的位置:首页 > 编程语言 > C#

用C# 实现的几种排序算法

2010-06-10 11:41 267 查看
using System;
using System.Collections.Generic;
using System.Text;

namespace Sort
{
class Program
{
int n;
static int count;
int[] a = new int[50];
public void Accept()
{
Console.WriteLine("Please input the number that you want to sort!");
n = Convert.ToInt32 (Console.ReadLine());
Console.WriteLine("input the elements one by one");
for (int i = 0; i < n; i++)
{
a[i] = Convert.ToInt32(Console.ReadLine());
}
}
public void Display()
{
for (int i = 0; i < n; i++)
{
Console.Write(a[i] + " ");
}
Console.Write("/n");
}
public void BubbleSort()
{
for (int i = 1; i < n; i++)
{
int swap = 0,temp;
for (int j = 0; j < n - i; j++)
{
if (a[j] > a[j + 1])
{
temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp;
swap = 1;
}
}
Display();
if (swap == 0) break;

}
}
public void SelectionSort()
{
int min,temp;
for (int i = 1; i < n; i++)
{
min = i - 1;
for (int j = i; j < n ; j++)
{
if(a[min]>a[j]) min=j;
}
if (min != i - 1)
{
temp = a[i - 1];
a[i - 1] = a[min];
a[min] = temp;
}
Display();
}

}
public void InsertionSort()
{
int temp,j;
for (int i = 1; i < n; i++)
{
temp = a[i];
for (j = i - 1; j >= 0 && temp < a[j]; j--)
{
a[j + 1] = a[j];
}
a[j + 1] = temp;
Display();
}
}
public void ShellSort()
{
int i,j,temp;
for (int incr = 3; incr >0; incr--)
{
for (int length = 0; length < incr; length++)
{

for ( i = length + incr;i < n ;i = i + incr)
{
temp = a[i];
for (j = i - incr;j >= 0 && a[j] > temp; j = j - incr)
{
a[j + incr] = a[j];

}
a[j + incr] = temp;

}
}
Display();
}
}
public void MergeSort(int low,int high)
{
int mid; mid = (low + high) / 2;
if (low < high)
{
MergeSort(low, mid);
MergeSort(mid + 1, high);
Merge(low, mid, high);
if (high == n - 1||high==n-2)
{
Display();
}
}

}
public void Merge(int low, int mid, int high)
{
int[] arrTans = new int[high - low + 1];
int k = 0,first=low,second=mid+1;
for( ;first<=mid&&second<=high;)
{
if (a[first] < a[second])
{
arrTans[k++] = a[first];
first++;
}
else
{
arrTans[k++] = a[second];
second++;
}
}
while (first <= mid)
{
arrTans[k++] = a[first];
first++;
}
while (second <= high)
{
arrTans[k++] = a[second];
second++;
}
for (int i = 0, m = low; i < arrTans.Length; i++, m++)
{
a[m] = arrTans[i];
}
}
public void QuickSort(int low, int high,int n)
{
count++;
int pivot,temp;
if (low <=high)
{
int i=low+1,j=high; pivot = a[low];
while(i <=j)
{
while ((a[i] <= pivot) && (i <= high)) i++;
while ((a[j] > pivot) && (j >= low)) j--;
if (i < j) { temp = a[i]; a[i] = a[j]; a[j] = temp; }
}
if (low < j)
{
temp = a[low]; a[low] = a[j]; a[j] = temp;
}
if(n==2)
Display();
QuickSort(low, j - 1,1); QuickSort(j + 1, high,2);

}
}
static void Main(String[] args)
{
string choice = "";
Program obj = new Program();
obj.Accept();
Console.Clear();
do
{
Console.WriteLine("1- - Bubble Sort/n" +
"2- - Selection Sort/n" +
"3- - Insertion Sort/n" +
"4- -Hell Sort:Increments-3,2,1/n" +
"5- -Merge Sort/n" +
"6- -Quick Sort/n" +
"0- - Exit");
choice = Console.ReadLine();
if (choice == "1")
{
Console.Clear();
obj.BubbleSort();
}
else if (choice == "2")
{
Console.Clear();
obj.SelectionSort();

}
else if (choice == "3")
{
Console.Clear();
obj.InsertionSort();
}
else if (choice == "4")
{
Console.Clear ();
obj.ShellSort();
}
else if (choice == "5")
{
Console.Clear();
obj.MergeSort(0, obj.n - 1);
}
else if (choice == "6")
{
Console.Clear();
obj.QuickSort(0, obj.n - 1, 2);
}

} while (choice != "0");
Console.ReadLine();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: