1.冒泡排序
时间复杂度: O(n^2),空间复杂度:O(1)。
void budsort(int a[], int length){
for(int i = 0; i < length; i++){
for(int j = 0; j < length - i; j++){
if(a[j+1] < a[j]){
int temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
}
}
2.选择排序
时间复杂度:O(n^2),空间复杂度:O(1).
void selsort(int a[], int length){
int t;
for(int i = 0; i < length; i++){
t = i;
for(int j = i + 1; j < length; j++){
if(a[j] < a[t]){
t = j;
}
}
int temp = a[i];
a[i] = a[t];
a[t] = temp;
}
}
3.插入排序
时间复杂度:O(n^2),空间复杂度:O(1).
void insertSort(int a[], int length){
for(int i = 1; i < length; i++){
int k = i;
while(1){
if((k - 1) >= 0 && a[k - 1] > a[k]){
int temp = a[k - 1];
a[k - 1] = a[k];
a[k] = temp;
}
else{
break;
}
k--;
}
}
}
4.希尔排序
时间复杂度:O(nlogn),空间复杂度:O(1).
void ShellSort(int a[], int length){
int h = length / 2;
while(h){
for(int i = h; i < length; i++){
for(int k = i; k >= h && a[k - h] > a[k]; k-=h){
int temp = a[k];
a[k] = a[k - h];
a[k - h] = temp;
}
}
h /= 2;
}
}
5.快速排序
时间复杂度:O(nlogn),空间复杂度:O(logn).
void quickSort(int a[], int l, int r){
if(l >= r){
return;
}
int piv = a[l];
int i = l, j = r;
while(i < j){
while(a[j] > piv && i < j){
j--;
}
a[i] = a[j];
while(a[i] < piv && i < j){
i++;
}
a[j] = a[i];
}
a[i] = piv;
quickSort(a, l, i - 1);
quickSort(a, i + 1, r);
}
6.归并排序
时间复杂度:O(nlogn),空间复杂度:O(n).
void myMerge(int a[], int *temp, int left, int mid, int right){
int p = left;
int ll = left, rl = mid + 1;
while(ll <= mid && rl <= right){
temp[p++] = (a[ll] < a[rl]) ? a[ll++] : a[rl++];
}
while(ll <= mid){
temp[p++] = a[ll++];
}
while(rl <= right){
temp[p++] = a[rl++];
}
while(left <= right){
a[left] = temp[left];
left++;
}
}
void msort(int a[], int *temp, int left, int right){
if(left < right){
int mid = left + (right - left) / 2;
msort(a, temp, left, mid);
msort(a, temp, mid + 1, right);
myMerge(a, temp, left, mid, right);
}
}
void merge_sort(int a[], int len){
int *tempArr = (int *)malloc(sizeof(int) *len);
assert(tempArr);
msort(a, tempArr, 0, len - 1);
free(tempArr);
}
7.计数排序
时间复杂度:O(n+m),空间复杂度:O(m)
void countSort(int a[], int len){
int min_value = a[0], max_value = a[0];
for(int i = 0; i < len; i++){
if(a[i] < min_value){
min_value = a[i];
}
if(a[i] > max_value){
max_value = a[i];
}
}
int max_len = max_value - min_value + 1;
int *tempArr = new int[max_len];
assert(tempArr);
memset(tempArr, 0, sizeof(tempArr) * max_len);
for(int i = 0; i < len; i++){
tempArr[a[i] - min_value]++;
}
for(int i = 0, j = 0; i < max_len; i++){
while(tempArr[i]--)
a[j++] = i + min_value;
}
delete []tempArr;
}
8.基数排序
时间复杂度:O(n*k),空间复杂度:O(n + k)
void redixSort(int a[], int len){
int (*bucket)[10] = new int[10][10];
assert(bucket);
memset(bucket, 0, sizeof(bucket) * 100);
int max_pow = a[0];
for(int i = 1; i < len; i++){
if(max_pow < a[i]){
max_pow = a[i];
}
}
int pow = 10, pos = 0, j = 0;
while(pow < max_pow * 10){
for(int i = 0; i < len; i++){
j = 0;
pos = (a[i] % pow) / (pow / 10);
while(bucket[pos][j]) j++;
bucket[pos][j++] = a[i];
}
pos = 0;
for(int i = 0; i < 10; i++){
for(int j = 0; j < len; j++){
if(bucket[i][j]){
a[pos++] = bucket[i][j];
bucket[i][j] = 0;
}
}
}
pow *= 10;
}
delete []bucket;
}
9.堆排序
时间复杂度:O(nlogn),空间复杂度:O(1).
第一种:直接利用优先队列。
class myComparison{
public:
bool operator()(const int &a, const int &b){
return a > b;
}
};
priority_queue<int,vector<int>, myComparison>prique;
void heapSort(int a[], int len){
for(int i = 0; i < len; i++){
prique.push(a[i]);
}
for(int i = 0; i < len; i++){
a[i] = prique.top();
prique.pop();
}
return;
}
第二种:
void heapify(int a[], int n, int p){
//维护第p个父节点的堆结构
int largest = p;
int lson = 2 * p + 1, rson = 2 * p + 2;
if(rson < n && a[largest] < a[rson]){
largest = rson;
}
if(lson < n && a[largest] < a[lson]){
largest = lson;
}
if(p != largest){
swap(&a[largest], &a[p]);
heapify(a, n, largest);
}
return;
}
void heapSort2(int a[], int len){
//1.建堆
for(int i = (len - 1) / 2; i >= 0; i--){
heapify(a, len, i);
}
//2.排序
for(int i = len - 1; i >= 0; i--){
int temp = a[0];
swap(&a[0], &a[i]);
heapify(a, i, 0);
a[i] = temp;
}
}