一些經典排序演算法的實現 C 實現

2021-07-09 13:31:01 字數 3097 閱讀 8552

#include

#include

#include

using namespace std;

void swap(

int&a,

int&b)

/* 氣泡排序 *

/void bubblesort(

int* a,

int n)}/

* 快速排序 *

/void quicksort(

int*a,

int low,

int high)}if

(i < high)

quicksort(a, i, high);if

(j > low)

quicksort(a, low, j);}

/* 桶排序, 無相同元素 *

/void bucketsortnosameelement(

int*a,

int n)

int*emptybucket = new int

[max]

;for

(int i = 0; i < max;

++i)

for(

int i = 0; i < n;

++i)

int index = 0;

for(

int i = 0; i < max;

++i)

delete [

]emptybucket;}/

* 桶排序, 有相同元素 *

/void bucketsorthassameelement(

int*a,

int n)

vector<

int>

* emptybucket = new vector<

int>

[max]

;for

(int i = 0; i < n;

++i)

int index = 0;

for(

int i = 0; i < max;

++i)

}delete [

]emptybucket;}/

* 插入排序 *

/void insertionsort(

int* a,

int n)

a[j]

= t;}}

/* 希爾排序, 插入排序變種 *

/void shellsort(

int*a,

int n)

a[j - 1]

= t;}}

}/* 基數排序 *

/typedef vector> input_type;

void radixsort(input_type & x)

// transfer results of buckets back into main array

input_type:

:iterator store_pos = x.begin();

for(buckets_type:

:iterator bucket = buckets.begin(

); bucket !

= buckets.

end();

++bucket)

bucket-

>clear();

}}}/

* 鴿巢排序 *

/void pigeonholesort(

int*a,

int n)

int*pigeonhole = new int

[max + 1]

;for

(int i = 0; i < n;

++i)

for(

int i = 0; i < n;

++i)

int index = 0;

for(

int i = 0; i <

= max;

++i)

}delete [

]pigeonhole;}/

* 合併排序 *

/void merge(vector<

int>

left

, vector<

int>

right

, vector<

int>

&result)

else}/

/ 還有左元素,沒有右元素

while

( i <

left

.size())

//還有右元素,沒有左元素

while

( j <

right

.size())

}void mergesort(vector<

int>

&a)for

(int i = middle; i < a.size();

++i)

mergesort(

left);

mergesort(

right);

merge(

left

,right

, a);}

/* 選擇排序 *

/void selectionsort(

int*a,

int n)}}

/* 計數排序 *

/void countingsort(

int*a,

int n)

int range = max - min + 1;

int*count = new int

[range]

;for

(int i = 0; i < range;

++i)

for(

int i = 0; i < n;

++i)

int index = 0;

for(

int i = min; i <

= max; i++)

for(

int j = 0; j < count[i - min];+

+j)a[index++]

= i;

delete [

]count;}

0給主人留下些什麼吧!~~

一些經典排序演算法的實現 C 實現

include include include using namespace std void swap int a,int b 氣泡排序 void bubblesort int a,int n 快速排序 void quicksort int a,int low,int high if i hig...

一些經典排序演算法的實現 C C 實現

include include include using namespace std void swap int a,int b 氣泡排序 void bubblesort int a,int n 快速排序 void quicksort int a,int low,int high if i hig...

常用的一些排序演算法(C 實現)

常用的排序演算法有氣泡排序,選擇排序,插入排序,歸併排序 希爾排序,堆排序等 1 冒泡 氣泡排序就是像旗袍一樣,最大的值逐漸上浮,我的實現方法是採用遞迴的,當然也可以不用遞迴 void bubblesort2 int array,int length if index length 1 bubble...