冒泡排序
- 取随机数组
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4 #include <stdlib.h>
5
6
7 int main() {
8 int arr[10];
9 int i,j;
10 srand(time(NULL));
11 for(i = 0;i<9;i++){
12 arr[i] = rand() % 100;
13 }
14 for(j = 0;j < 9;j++){
15
16 for(i = 0;i<9;i++){
17 if(arr[i] > arr[i+1]){
18 arr[i] ^=arr[i+1];
19 arr[i+1] ^=arr[i];
20 arr[i] ^= arr[i+1];
21 }
22 }
23 }
24 for(i = 0;i<10;i++){
25 printf("%d\n",arr[i]);
26 }
27 return 0;
28 }
- 构建函数
1 #include <stdio.h>
2 void sort(int a[],int len);
3
4
5 void sort(int a[],int len)
6 {
7 int i,j;
8 for(j=0;j<len-1;j++){
9 for(i=0;i<len-1;i++){
10 if(a[i] >a[i+1]){
11 a[i] ^=a[i+1];
12 a[i+1] ^=a[i];
13 a[i] ^=a[i+1];
14 }
15 }
16 }
17 for(j = 0; j<len; j++){
18 printf("%d ",a[j]);
19 }
20 }
21
22 int main() {
23 int num[5]={36,24,67,8,13};
24 sort(num,5);
25
26 return 0;
27 }
ps:
按位异或交换变量
https://blog.csdn.net/Marco___/article/details/90415304
来源:oschina
链接:https://my.oschina.net/u/4303162/blog/3365666