排序算法一

来源:互联网 发布:闲鱼网淘宝 编辑:程序博客网 时间:2024/05/01 09:30
void swap(int & a, int & b){int temp = a;a = b;b = temp;}//insert sortingvoid insert_sort(int a[], int n){for (int i = 1; i < n; ++i){int cur = a[i];int j = 0;for (; j < i; ++ j){if (cur < a[j]){break;}}//for (int k = i-1; k >=j; --k){a[k+1] = a[k];}a[j] = cur;}}//bubble sortingvoid bubble_sort(int a[], int n){for(int i = 1; i < n; ++i){for (int j = n -1; j >= i; --j){if (a[j-1] > a[j]){int temp = a[j-1];a[j-1] = a[j];a[j] = temp;}}}}//select sortingvoid select_sort(int a[], int n){for (int i = 0; i < n -1; ++i){for (int j = i+1; j < n; ++j){if (a[i] > a[j]){swap(a[i], a[j]);}}}}

0 0
原创粉丝点击