# 经典排序：冒泡排序+选择排序 小结

``` 1 #include <stdio.h>
2 void BubbleSort(int *, int);
3 int main()
4 {
5     int a[] = {9,5,3,6,7,2,5,4,3,9}, i;
6     int s = sizeof(a) / sizeof(int);
7     puts("Before sort:");
8     for(i = 0; i < s; i++)
9         printf("%d ",a[i]);
10     BubbleSort(a,s);
11     puts("\nAfter sort:");
12     for(i = 0; i < s; i++)
13         printf("%d ",a[i]);
14     return 0;
15 }
16 void BubbleSort(int *a, int s)
17 {
18     int i, j, tmp;
19     for(i = 0; i < s-1; i++)
20     {
21         for(j = 1; j < s-i; j++)
22         {
23             if(a[j-1] > a[j])
24             {
25                 tmp = a[j-1];
26                 a[j-1] = a[j];
27                 a[j] = tmp;
28             }
29         }
30     }
31 }```

``` 1 void BubbleSort(int *a, int s)
2 {
3     int i, j, tmp;
4     char swap;
5     for(i = 0; i < s-1; i++)
6     {
7         swap = 0;
8         for(j = 1; j < s-i; j++)
9         {
10             if(a[j-1] > a[j])
11             {
12                 swap = 1;
13                 tmp = a[j-1];
14                 a[j-1] = a[j];
15                 a[j] = tmp;
16             }
17         }
18         if(swap == 0) break;
19     }
20 }```

```void SelectSort(int *a, int s)
{
int i, j, tmp, min_num_index;
for(i = 0; i < s-1; i++)
{
min_num_index = i;
for(j = i+1; j < s; j++)
{
if(a[j] < a[min_num_index])
min_num_index = j;
}
if(min_num_index != i)
{
tmp = a[i];
a[i] = a[min_num_index];
a[min_num_index] = tmp;
}
}
}```

(0)
(0)

0条