#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;
}
一些經典排序演算法的實現 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 實現
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...
一些排序演算法的簡單實現
排序演算法.cpp 定義控制台應用程式的入口點。include stdafx.h include include include includeusing namespace std const int maxnum 24 void vecprint vectorconst num cout end...