参考博客:https://www.cnblogs.com/jymblog/p/5408752.html
1. 冒泡排序
popSort(int a[], int num) { int ix = 0; int jx = 0; for(ix=0; ix<num-1; ix++) { for(jx=0; jx<num-1-ix; jx++) { if (a[jx]>a[jx+1]) { swap(&a[jx], &a[jx+1]); } } } }
2. 选择排序
#define <stdio.h>
swap(int *pa, int *pb) { int temp = *pa; *pa = *pb; *pb = temp; } selectSort(int a[], int num) { int ix = 0; int jx = 0; for(ix=0; ix<num-1; ix++) { for(jx=ix+1; jx<num; jx++) { // 和后面的元素依次比较 if (a[ix]>a[jx]) { swap(&a[ix], &a[jx]); } } } }
display(int a[], int num) {
int jx=0;
for(jx=0; jx<num; jx++) {
printf("%d ", a[jx]);
}
printf(" ");
}
main() {
int a[6] = {1,3,2,6,4,5};
display(a, 6);
selectSort(a, 6);
display(a, 6);
}