八大基础排序 C/C++
一、冒泡排序
#include <stdio.h>
void swap(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
//冒泡排序
void bubble_sort(int arr[], int cnt)
{
for(int idx=0; idx < cnt; idx++){
bool flag = true;
for (int it=0; it<cnt-idx-1; it++){
if (arr[it] > arr[it+1]){
swap(&arr[it], &arr[it+1]);
flag = false;
}
}
if (flag) return ;
}
}
int main(){
int arr[] = { 91, 82, 73, 64, 55, 46, 37, 28, 19, 10 };
int len = (int) sizeof(arr)/sizeof(*arr);
bubble_sort(arr, len);
for(int i=0; i< len; i++)
{
printf("%d \t", arr[i]);
}
printf("\n");
return 1;
}
时间复杂度:O(n^2)
空间复杂度:O(1)
二、选择排序
#include <stdio.h>
void swap(int* a, int* b){
int tmp = *a;
*a = *b;
*b= tmp;
}
void select_sort(int arr[], int length)
{
for(int idx=0; idx<length; idx++){
//int value = arr[idx];
for(int next=idx; next<length; next++){
if(arr[idx]>arr[next]){
swap(&arr[idx], &arr[next]);
}
}
}
}
int main(){
int arr[] = { 91, 82, 73, 64, 55, 46, 37, 28, 19, 10 };
int len = (int) sizeof(arr)/sizeof(*arr);
select_sort(arr, len);
for(int i=0; i< len; i++)
{
printf("%d \t", arr[i]);
}
printf("\n");
return 1;
}
时间复杂度:
空间复杂度:
三、插入排序
#include <stdio.h>
void insert_sort(int arr[], int length)
{
for(int idx=0; idx<length; idx++){
int value = arr[idx];
int next = idx;
for(; next>0 && arr[next-1]>value; next--){
arr[next] = arr[next-1];
}
arr[next] = value;
}
}
int main(){
int arr[] = { 91, 82, 73, 64, 55, 46, 37, 28, 19, 10 };
int len = (int) sizeof(arr)/sizeof(*arr);
insert_sort(arr, len);
for(int i=0; i< len; i++)
{
printf("%d \t", arr[i]);
}
printf("\n");
return 1;
}
时间复杂度:
空间复杂度:
四、快速排序
#include <stdio.h>
void swap(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
int partition(int arr[], int begin, int end){
int pivot = arr[begin];
int left = begin;
int right = end;
while(left < right){
while (left<right && arr[right] > pivot)
right --;
if(left < right) {
arr[left]= arr[right];
left ++;
}
while(left < right && arr[left]< pivot)
left ++;
if (left < right)
{
arr[right] = arr[left];
right--;
}
}
arr[left] = pivot;
return left;
}
//快速排序
void qsort(int arr[], int begin, int end)
{
if(begin < end){
int mid = partition(arr, begin, end);
qsort(arr, begin, mid-1);
qsort(arr, mid+1, end);
}
return;
}
void quick_sort(int a[], int length)
{
qsort(a, 0, length-1);
}
int main(){
int arr[] = { 91, -82, 73, 64, 55, 46, -37, 28, 19, 10, 45,46 };
int len = (int) sizeof(arr)/sizeof(*arr);
quick_sort(arr, len);
for(int i=0; i< len; i++)
{
printf("%d \t", arr[i]);
}
return 1;
}
时间复杂度:
空间复杂度:
五、归并排序
#include <stdio.h>
#include <stdlib.h>
void swap(int*a, int*b){
int tmp = *a;
*a = *b;
*b= tmp;
}
void merge(int arr[], int left, int mid, int end ){
int size = end-left+1;
int * tmp = (int*)malloc(size*sizeof(int));
int tmp_left = left;
int tmp_right = mid+1;
int idx = 0;
while(tmp_left<= mid && tmp_right<=end){
if(arr[tmp_left]<= arr[tmp_right]){
tmp[idx++] = arr[tmp_left++];
}else{
tmp[idx++] = arr[tmp_right++];
}
}
while(tmp_left<=mid){
tmp[idx++] = arr[tmp_left++];
}
while(tmp_right<=end){
tmp[idx++] = arr[tmp_right++];
}
for(int i=0; i<size; i++){
arr[left+i]= tmp[i];
}
free(tmp);
}
void merge_sort(int arr[], int start, int end)
{
int mid = start + (end-start)/2;
if (start>= end) return;
merge_sort(arr, start, mid);
merge_sort(arr, mid+1, end);
merge(arr, start, mid, end );
}
int main(){
int arr[] = { 91, 82, 73, 64, 55, 46, 37, 28, 19, 10 };
int len = (int) sizeof(arr)/sizeof(*arr);
merge_sort(arr, 0, len-1);
for(int i=0; i< len; i++)
{
printf("%d \t", arr[i]);
}
printf("\n");
return 1;
}
时间复杂度:
空间复杂度:
六、希尔排序
void shell_sort(int arr[], int len)
{
int pre, sub, step;
int value;
for(int step=len/2; step>=1; step=step/2){
for(pre=step; pre<=len; pre++){
value = arr[pre];
for(sub=pre; sub>=step; sub-=step){
if(value< arr[sub-step])
arr[sub] = arr[sub-step];
else
break;
}
arr[sub] = value;
}
}
}
时间复杂度:
空间复杂度:
七、堆排序
#include <stdio.h>
void heap_adjust(int arr[], int idx, int size){
int parent = idx;
int child = parent*2 +1 ;
int value = arr[parent];
for(; child<size; child=(parent*2 +1)) {
if ((child+1)<size && arr[child+1]>arr[child]){
child++;
}
if (value < arr[child]) {
arr[parent] = arr[child];
parent = child;
} else{ break;}
}
arr[parent] = value;
}
void heap_sort(int arr[], int length)
{
int size = length-1;
for(int step=length/2; step>=0; step--){
heap_adjust(arr, step, size);
}
for(int idx=length-1; idx>0; idx--){
swap(&arr[0], &arr[idx]);
heap_adjust(arr, 0, idx);
}
}
int main(){
int arr[] = { 91, -82, 73, 64, 55, 46, -37, 28, 19, 10, 45,46 };
int len = (int) sizeof(arr)/sizeof(*arr);
for(int i=0; i< len; i++)
{
printf("%d \t", arr[i]);
}
printf("\n");
heap_sort(arr, len);
for(int i=0; i< len; i++)
{
printf("%d \t", arr[i]);
}
时间复杂度:
空间复杂度:
八、桶排序(基数排序)
时间复杂度:
空间复杂度: