您的位置:首页 > 其它

几种常见排序算法及其效率

2016-08-12 14:59 344 查看
原文地址:几种常见排序算法及其效率作者:whatever

介绍了几种交换排序的算法

1。冒泡排序(Bubble Sort)是一种简单的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。这个算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端。

Code:

using System;

usingSystem.Collections.Generic;

namespaceCom.Colobu.Algorithm.Exchange

{

///<summary>

///冒泡排序是这样实现的:

///

/// 1.首先将所有待排序的数字放入工作列表中。

/// 2.从列表的第一个数字到倒数第二个数字,逐个检查:若某一位上的数字大于他的下一位,则将它与它的下一位交换。

/// 3.重复2号步骤(倒数的数字加1。例如:第一次到倒数第二个数字,第二次到倒数第三个数字,依此类推...),直至再也不能交换。

///

///平均时间复杂度:O(n^2)

///Stability:Yes

///</summary>

public classBubbleSortAlgorithm

{

public static voidBubbleSort<T>(IList<T>szArray) whereT:IComparable

{

int i;

int j;

T temp; //交换变量

bool swapped = false;

for (i = szArray.Count - 1; i >= 0;i--)

{

for (j = 1; j <= i;j++)

{

if (szArray[j - 1].CompareTo(szArray[j]) >0)

{

temp = szArray[j - 1];

szArray[j - 1] =szArray[j];

szArray[j] = temp;

swapped = true;

}

}

if (!swapped)

break;

}

}

}

}

using System;

using System.Collections.Generic;

namespace Com.Colobu.Algorithm.Exchange

{

///<summary>

///冒泡排序是这样实现的:

///

/// 1.首先将所有待排序的数字放入工作列表中。

/// 2.从列表的第一个数字到倒数第二个数字,逐个检查:若某一位上的数字大于他的下一位,则将它与它的下一位交换。

/// 3.重复2号步骤(倒数的数字加1。例如:第一次到倒数第二个数字,第二次到倒数第三个数字,依此类推...),直至再也不能交换。

///

///平均时间复杂度:O(n^2)

///Stability:Yes

///</summary>

public classBubbleSortAlgorithm

{

public static voidBubbleSort<T>(IList<T>szArray) where T:IComparable

{

int i;

int j;

T temp; //交换变量

bool swapped = false;

for (i = szArray.Count - 1; i >= 0; i--)

{

for (j = 1; j <= i; j++)

{

if (szArray[j - 1].CompareTo(szArray[j]) > 0)

{

temp = szArray[j - 1];

szArray[j - 1] = szArray[j];

szArray[j] = temp;

swapped = true;

}

}

if (!swapped)

break;

}

}

}

}

2。快速排序

using System;

usingSystem.Collections.Generic;

namespaceCom.Colobu.Algorithm.Exchange

{

///<summary>

///<b>快速排序</b>是所有排序算法中最高效的一种.

///它采用了分治的思想:先保证列表的前半部分都小于后半部分,

///然后分别对前半部分和后半部分排序,这样整个列表就有序了。

///这是一种先进的思想,也是它高效的原因。

///因为在排序算法中,算法的高效与否与列表中数字间的比较次数有直接的关系,

///而"保证列表的前半部分都小于后半部分"就使得前半部分的任何一个数从此以后

///都不再跟后半部分的数进行比较了,大大减少了数字间不必要的比较。

///

///平均时间复杂度:O(nlogn)

///Stability:No

///</summary>

public classQuickSortAlgorithm

{

// QuickSortimplementation

public static voidQuickSort<T>(IList<T>szArray, int nLower, int nUpper) where T :IComparable

{

if (nLower <nUpper)

{

int nSplit = Partition(szArray, nLower,nUpper);

QuickSort(szArray, nLower, nSplit -1);

QuickSort(szArray, nSplit + 1,nUpper);

}

}

// QuickSort partitionimplementation

private static intPartition<T>(IList<T>szArray, int nLower, int nUpper) whereT:IComparable

{

T szPivot = szArray[nLower];//分隔点的值,用数字第一个值代替

int nLeft = nLower +1;//左边开始点

int nRight = nUpper;//右边开始点

T szSwap; //交换变量

//开始查找

//将小于分隔点的值都挪到左边

//将小于分隔点的值都挪到右边

while (nLeft <=nRight)

{

//找到左边第一个大于分隔点的值

while (nLeft <= nRight&&szArray[nLeft].CompareTo(szPivot) <=0)

nLeft = nLeft + 1;

//找到右边第一个小于分隔点的值

while (nLeft <= nRight&&szArray[nRight].CompareTo(szPivot) >0)

nRight = nRight - 1;

//如果左右还没有交叉(碰头),交换左右两个值

if (nLeft <nRight)

{

szSwap = szArray[nLeft];

szArray[nLeft] =szArray[nRight];

szArray[nRight] = szSwap;

nLeft = nLeft + 1;

nRight = nRight - 1;

}

}

//将分隔点移动到最后那个点,此时nRight = nLeft -1

//所以那个点的值小于分隔点,可以与分隔点进行交换

szSwap = szArray[nLower];

szArray[nLower] =szArray[nRight];

szArray[nRight] = szSwap;

//现在在nRight左边的值都小于分隔点,nRight右边的值都大于分隔点

return nRight;

}

}

}

3。奇偶排序

using System;

usingSystem.Collections.Generic;

namespaceCom.Colobu.Algorithm.Exchange

{

///<summary>

///<b>奇偶排序</b>的思路是在数组中重复两趟扫描。

///第一趟扫描选择所有的数据项对,a[j]和a[j+1],j是奇数(j=1, 3,5……)。

///如果它们的关键字的值次序颠倒,就交换它们。

///第二趟扫描对所有的偶数数据项进行同样的操作(j=2,4,6……)。

///重复进行这样两趟的排序直到数组全部有序。

///

///平均时间复杂度:O(n^2)

///Stability:Yes

///</summary>

public classOddEvenSortAlgorithm

{

public static voidOddEvenSort<T>(IList<T>szArray) where T :IComparable

{

bool sorted = false;

while (!sorted)

{

sorted = true;

// odd-even

for (int i = 1; i < szArray.Count - 1; i +=2)

{

if (szArray[i].CompareTo(szArray[i + 1]) >0)

{

Swap(szArray, i, i + 1);

sorted = false;

}

}

// even-odd

for (int j = 0; j < szArray.Count - 1; j +=2)

{

if (szArray[j].CompareTo(szArray[j + 1]) >0)

{

Swap(szArray, j, j + 1);

sorted = false;

}

}

}

}

private static voidSwap<T>(IList<T>szArray, int i, int j)

{

T tmp = szArray[i];

szArray[i] = szArray[j];

szArray[j] = tmp;

}

}

}

4。鸡尾酒排序,也就是定向冒泡排序 , 鸡尾酒搅拌排序 , 搅拌排序 (也可以视作选择排序的一种变形), 涟漪排序 , 来回排序 or 快乐小时排序 ,是冒泡排序的一种变形。此算法与冒泡排序的不同处在于排序时是以双向在序列中进行排序

using System;

usingSystem.Collections.Generic;

namespaceCom.Colobu.Algorithm.Exchange

{

///<summary>

///<b>鸡尾酒排序</b>,也就是双向冒泡排序(bidirectionalbubble sort), 鸡尾酒搅拌排序,搅拌排序

///(也可以视作选择排序的一种变形), 涟漪排序, 来回排序 or快乐小时排序,

///是冒泡排序的一种变形。

///此算法与冒泡排序的不同处在于排序时是以双向在序列中进行排序。

///

///平均时间复杂度:O(n^2)

///Stability:Yes

///</summary>

public classCocktailSortAlgorithm

{

public static voidCocktailSort<T>(IList<T>szArray) where T :IComparable

{

int bottom = 0;

int top = szArray.Count -1;

bool swapped = true;

while (swapped == true) // if no elements have been swapped, thenthe list is sorted

{

swapped = false;

for (int i = bottom; i < top; i = i +1)

{

if (szArray[i].CompareTo(szArray[i + 1]) >0) // test whether the two elements are in thecorrect order

{

Swap(szArray,i,i + 1); // let the two elements changeplaces

swapped = true;

}

}

// decreases top the because the element with the largest value inthe unsorted

// part of the list is now on the positiontop

top = top - 1;

for (int i = top; i > bottom; i = i -1)

{

if (szArray[i].CompareTo(szArray[i - 1]) <0)

{

Swap(szArray,i,i - 1);

swapped = true;

}

}

// increases bottom because the element with the smallest value intheunsorted

// part of the list is now on the positionbottom

bottom = bottom + 1;

}

}

private static voidSwap<T>(IList<T>szArray, int i,int j)

{

T tmp = szArray[i];

szArray[i] = szArray[j];

szArray[j] = tmp;

}

}

}

5。Gnome Sort is a sorting algorithm which issimilar to insertion sort, except that moving an element to itsproper place is accomplished by a series of swaps, as in bubblesort. The name comes from the supposed behavior of the Dutchgarden
gnome in sorting a line of flowerpots and is described onDick Grune's Gnome sort page.

Code:

using System;

usingSystem.Collections.Generic;

namespaceCom.Colobu.Algorithm.Exchange

{

///<summary>

///<b>Gnomesort</b> is a sorting algorithm whichis similar to insertionsort,

/// exceptthat moving an element to its proper place isaccomplished

/// by aseries of swaps, as in bubblesort.

/// GnomeSort is based on the technique used by the standard Dutch GardenGnome.

/// Here ishow a garden gnome sorts a line of flowerpots.

///Basically, he looks at the flower pot next to him and the previousone;

/// if theyare in the right order he steps one potforward,

///otherwise he swaps them and steps one potbackwards.

/// Boundaryconditions: if there is no previouspot,

/// he stepsforwards; if there is no pot next to him, he isdone.

///

///平均时间复杂度:O(n^2)

///Stability:Yes

///</summary>

public classGnomeSortAlgorithm

{

public static voidGnomeSort<T>(IList<T>szArray) where T :IComparable

{

int i = 1;

int j =2;

int count =szArray.Count;

while (i <count)

{

if (szArray[i - 1].CompareTo(szArray[i]) < 0) //fordescending sort, reverse the comparison to>=

{

i = j;

j = j + 1;

}

else

{

Swap(szArray, i - 1, i);

i = i - 1;

if (i == 0)

{

i = j;

j = j + 1;

}

}

}

}

private static voidSwap<T>(IList<T>szArray, int i, int j)

{

T tmp = szArray[i];

szArray[i] = szArray[j];

szArray[j] = tmp;

}

}

}

6。Comb Sort

using System;

usingSystem.Collections.Generic;

namespaceCom.Colobu.Algorithm.Exchange

{

///<summary>

///<b>Combsort</b> improves on bubble sort, andrivals algorithms likeQuicksort.

/// Thebasic idea is to eliminate turtles, or small values near the end ofthelist,

/// since ina bubble sort these slow the sorting downtremendously.

///

///平均时间复杂度:O(nlogn)

///Stability:No

///</summary>

public classCombSortAlgorithm

{

public static voidCombSort<T>(IList<T>szArray) where T :IComparable

{

int gap = szArray.Count;

bool swapped = true;

while (gap > 1 ||swapped)

{

if (gap >1)

{

gap = (int)(gap / 1.25);

}

int i = 0;

swapped = false;

while (i + gap <szArray.Count)

{

if (szArray[i].CompareTo(szArray[i + gap]) >0)

{

T t = szArray[i];

szArray[i] = szArray[i +gap];

szArray[i + gap] = t;

swapped = true;

}

i++;

}

}

}

}

}



随机排序结果:下面是对一个随机序列的排序效率结果:

test1:

quicksorttime: 0.10864 ms (*)

bubblesorttime: 5.83652 ms

cocktailtime: 6.13016 ms

comb sorttime: 1.38076 ms

gnome sorttime: 4.38388 ms

odd-even sorttime: 6.846 ms (@)

test2:

quicksorttime: 0.1225ms (*)

bubblesorttime: 5.35685 ms

cocktailtime: 5.53548 ms

comb sorttime: 0.22765 ms

gnome sorttime: 4.10683 ms

odd-even sorttime: 6.7337ms (@)

test3:

quicksorttime: 0.11808 ms (*)

bubblesorttime: 5.49997 ms

cocktailtime: 5.32455 ms

comb sorttime: 0.20061 ms

gnome sorttime: 4.66813 ms

odd-even sorttime: 7.18163 ms
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: