-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble.c
79 lines (69 loc) · 1.19 KB
/
bubble.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<stdio.h>
#include<stdlib.h>
//two kind of the bubble sort
//1.the right side
/**
* @brief right_bubble 用i记录冒泡多少次
*
*
* @param arr the arr of the number
* @param len the length of the arr
*/
void right_bubble(int *arr, int len){
if(NULL == arr || len <= 0)
return;
int i;
int j;
int tmp;
for(i = 0;i<len-1;i++){
for(j = 0;j<len-1-i;j++){
//compare the two num
if(arr[j] > arr[j+1]){
tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}
return ;
}
//2.the left side
void left_bubble(int *arr, int len){
int i;
int j;
int tmp;
for(i=0;i<len-1;i++){
for(j=len-1;j>i;j--){
if(arr[j] < arr[j-1]){
tmp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = tmp;
}
}
}
return ;
}
//3.print the arr
void show(int *arr, int len){
int i;
for(i=0;i<len;i++){
printf("%d\n",arr[i]);
}
printf("----------------\n");
return ;
}
//test function
int main()
{
int arr_left[6] = {5,2,4,6,1,3};
int arr_right[6] = {5,2,4,6,1,3};
int *arr = NULL;
int *arr_out;
left_bubble(arr_left, 6);
show(arr_left,6);
right_bubble(arr_right,6);
right_bubble(arr,6);
right_bubble(arr_out,6);
show(arr_right,6);
return 0;
}