基礎演算法 排序演算法

2022-05-16 22:39:27 字數 3447 閱讀 2053

話不多說,上**

#include #include 

#include

#include

#include

#define n 10000500

long

long

beg;

intcnt,n;

void

show_time()

void swap(int *a,int *b)

///稱這兩個函式為比較函式,函式名即相當於指標,將其寫入排序演算法中,實現**復用

char ascending(int a,int b)

char descending(int a,int b)//

沒有bool我也很無奈/**

* @param s:開始指標 e:結束指標

* @param cmp 指向函式的指標

* @note 快速排序:平均時間複雜度 o(n*ln(n)) 最壞情況(倒序)o(n^2) */

void quick_sort(int *s,int *e,char (*cmp)(int ,int ))

}*s = *i;///

把key放到適當的位置,這時滿足key左邊的元素均不大於(小於)key,右邊元素均不小於(大於)key

*i =key;

quick_sort(s,i,cmp);

///對key左,右側進行同樣

quick_sort(i+1

,e,cmp);}/*

* * @param s:開始指標 e:結束指標 k:臨時儲存

* @param cmp 指向函式的指標

* @note 歸併排序:時間複雜度 o(n*ln(n))

* @note 相較快速排序更加穩定

* @note 可以通過修改程式在 o(n*ln(n))時間內找出資料的逆序數(傳統需要o(n^2)) */

void merge(int *s,int *e,char (*cmp)(int,int),int *k)

else

}while(i<=mid)

while (j<=e)

while (ori_beg<=e)

}void merge_sort(int *s,int *e,char (*cmp)(int ,int),int *store)

void stable_sort(int *s,int *e,char (*cmp)(int ,int

))/*

* * @note 參考了 csdn

* @note 冪增長複雜度 中等資料規模有較好的表現 */

void shell_sort(int *s,int *e,char (*cmp)(int ,int

)) maxs = e-s;

while(dis_seq>0)

*j =tmp;

}dis_seq = (dis_seq-1)/3

; }

}void bubble_sort(int *s,int *e, char (*cmp)(int,int

)) }

j = last;///

second optimize ,we don't need to sort if the past sequence are order.

}while

(not_order);

}void selection_sort(int *s,int *e,char (*cmp)(int ,int

)) }

if(key!=*i)swap(i,mark);

}}void insertion_sort(int *s,int *e,char (*cmp)(int,int

)) *j =key;

}}///

列印資訊

void show(int *a,int *b)

}int

num[n];

intori[n];

void init(int

n)int

main()

cnt = 0

; init(n);

quick_sort(num,num+n,ascending);

show_time();

init(n);

stable_sort(num,num+n,ascending);

show_time();

init(n);

shell_sort(num,num+n,ascending);

show_time();

init(n);

insertion_sort(num,num+n,ascending);

show_time();

init(n);

selection_sort(num,num+n,ascending);

show_time();

init(n);

bubble_sort(num,num+n,ascending);

show_time();

}}/*

* * n(ln(n))

* 1 快速排序

* 2 歸併排序

* 介於線性和二次方之間

* 3 希爾排序

* 二次方複雜度

* 4 插入排序

* 5 選擇排序

* 5 氣泡排序 */

/*****************

test one:

1000000

case #1: 187.0 ms

case #2: 188.0 ms

case #3: 391.0 ms

test two:

10000000

case #1: 4172.0 ms

case #2: 2136.0 ms

case #3: 11986.0 ms

test three:

10000

case #1: 0.0 ms

case #2: 0.0 ms

case #3: 0.0 ms

case #4: 62.0 ms

case #5: 109.0 ms

case #6: 344.0 ms

test four:

100000

case #1: 15.0 ms

case #2: 15.0 ms

case #3: 32.0 ms

case #4: 6938.0 ms

case #5: 10611.0 ms

case #6: 35866.0 ms

****************

*/

演算法基礎 排序演算法

1.氣泡排序 原理有一列資料,從第乙個數開始,第乙個和第二個進行比較,如果第乙個大就交換,如果第二個大就不變,再第二個和第三個比較,一次類推,第二輪是從第乙個數開始比到倒數第二個數,第三輪是從第乙個數比到倒數第三個數.以此類推.def bubble nums for j in range len n...

基礎演算法 快速排序演算法

時間複雜度 最壞的情況和氣泡排序一樣,為o n 2 平均的時間複雜度 nlog n 基本思想 1 取最左邊的數作為標準 2 從右邊開始查詢,找到從右邊數第乙個小於標準temp的數 3 從左邊繼續查詢,找到第乙個大於標準temp的數 4 將左右兩邊找到的數進行交換,如此往復,直到左邊和右邊碰到一起,即...

演算法 基礎 常見排序演算法

如果h位比flag小,則需要交換,在此處進行。if l 從l位開始,如果比flag小就將l向右移動,即l while l flag 如果l位比flag大,則需要交換,在此處進行 if l flag 一趟結束,判斷flag兩端的子陣列是否需要排序 if l low if h public static...