using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConAppSequenceLearn
{
class Program
{
/// <summary>
/// 冒泡排序法
/// </summary>
public class BubbleSorter
{
public void Sort(int[] list)
{
int i, j, temp;
bool done = false;
j = 1;
while ((j < list.Length) && (!done))
{
done = true;
for (i = 0; i < list.Length - j; i++)
{
if (list[i] > list[i + 1])
{
done = false;
temp = list[i];
list[i] = list[i + 1];
list[i + 1] = temp;
}
}
j++;
}
}
}
/// <summary>
/// 选择排序法
/// </summary>
public class SelectionSorter
{
private int min;
public void Sort(int[] list)
{
for (int i = 0; i < list.Length - 1; i++)
{
min = i;
for (int j = i + 1; j < list.Length; j++)
{
//如果外层的值大于里层的值那么把标识位换一下
if (list[j] < list[min])
min = j;
}
int t = list[min];
list[min] = list[i];
list[i] = t;
}
}
}
/// <summary>
/// 插入排序法
/// </summary>
public class InsertionSorter
{
public void Sort(int[] list)
{
for (int i = 1; i < list.Length; i++)
{
int iCrrentValue = list[i];
int iCrrentIndex = i;
while (i > 0 && list[i - 1] > iCrrentValue)
{
list[i] = list[i - 1];
--i;
}
list[i] = iCrrentValue;
}
}
}
/// <summary>
/// 快速排序法
/// </summary>
public class QuickSorter
{
public void Sort(int[] list, int min, int max)
{
int iMin, iMax, iTempVlue;
int iAvgValue;
iMin = min;
iMax = max;
iAvgValue = list[(iMax + iMin) / 2];
do
{
while (list[iMin] < iAvgValue && iMin < max) iMin++;
while (list[iMax] > iAvgValue && iMax > min) iMax--;
if (iMax >= iMin)
{
iTempVlue = list[iMin];
list[iMin] = list[iMax];
list[iMax] = iTempVlue;
iMin++;
iMax--;
}
} while (iMax >= iMin);
if (iMin < max) Sort(list, iMin, max);
if (iMax > min) Sort(list, iMax, min);
}
}
public static void Main()
{
int[] iArrary = { 1, 5, 3, 6, 2, 4 };
SelectionSorter ss = new SelectionSorter();
ss.Sort(iArrary);
for (int m = 0; m < iArrary.Length; m++)
Console.Write("{0} ", iArrary[m]);
Console.WriteLine();
Console.ReadLine();
}
}
}