各種排序演算法總結

2021-08-09 04:15:29 字數 1871 閱讀 6377

/* created by vencent on 2008.8.29 */

1.插入排序

1.1 一般插入排序 insertsort(int* array, int length)

1.2 折半插入排序 bininsertsort(int* array, int length)

1.3 希爾排序     shellsort(int* array, int length)

2.交換排序

2.1 氣泡排序     bubblesort(int* array, int length)

2.2 快速排序

quicksort(int* array, int length)

3.選擇排序

3.1 一般選擇排序 selectsort(int* array, int length)

3.2 堆排序       stacksort(int* array, int length)

*/#include "sort.h"

//排序演算法的測試

void testsort();/*

int * array;

int i;

array = (int*)malloc(sizeof(int) * len);

for (i = 0; i < len; i++)

*/heapsort(array,10);

printf("\nthe result :\n");

for (i = 0; i < len; i++)

}//插入排序:

void insertsort(int *array, int length)

array[j] = temp;}}

//折半插入排序

void bininsertsort(int *array, int length)

j = low ;

temp = array[i];

for(k = i ;  k > j ; k--) 

array[j] = temp;}}

//氣泡排序:

void bubblesort(int *array, int length)}}

//快速排序

void quicksort(int *array, int start, int end)

}//快速排序分割槽

int partition(int *array, int start, int end)

array[low] = array[high];

//此處low不能加1。

while((array[low] < target || array[high] == target) && ( low < high ) )

array[high] = array[low];

//此處high不能減1。

}array[low] = target;

return low;

}//堆排序(將堆看成是一棵完全二叉樹,存放在一維陣列中)

//adjust()函式:假設初始堆已經有序,現將最小的元素取出,並用最後乙個元素至於小堆的堆頂,

//重新調整堆,使其重新調整成為乙個小堆。

void adjust(int *array, int start, int end)

else

if( temp > array[j]) //注意此處為temp,而不是array[location]

}array[location] = temp;

}//完整的堆排序過程:

void heapsort(int *array, int length)

//不斷調整堆來排序:

for( i = length - 1; i > 0 ; i--)

}

各種排序演算法總結

注 以下所講排序,以公升序排序為例!選擇排序 作者思路 在一組數中,選擇第乙個數標記為最小值,在剩下的數中找比它小的數,若找到則交換兩數,標記新的 最小值 然後繼續往下找,這樣一趟下來就可以找到一組數中第二小的值,第二次以第二個數作為最小值,如此迴圈下去。這是最簡單 最基礎的一種排序演算法。例子 1...

各種排序演算法總結

1 插入排序 void insertsort int a,int n a j 1 key 插入排序是穩定的排序,平均和最壞時間複雜度是o n 2 最好的時間複雜度是o n 對應於全部排好序的情況。2 氣泡排序 void bubblesort int a,intn 氣泡排序是穩定的排序,平均和最壞時間...

各種排序演算法總結

一 排序分類 內部排序 整個排序過程不需要訪問外存便能完成。外部排序 參加排序的記錄數量很大,整個序列的排序過程不可能在記憶體中完成。二 效能比較 排序方法 平均情況 最好情況 最壞情況 空間穩定性 冒泡o n2 o n o n2 o 1 穩定簡單選擇排序 o n2 o n2 o n2 o 1 不穩...