冒泡排序分析

冒泡排序是一种比较基础的排序,通过遍历若干次要排序的数列,以达到将数列排序。每次遍历都会从前往后依次比较两个数的大小,如果前者比后者大,就交换两个数的位置,这样通过一趟遍历就能够将最大的数放之末尾,第二次遍历就可以将第二大的数放之倒数第二的位置,依此类推

冒泡排序源码

 1 #include <iostream>
 2 using namespace std;
 3 
 4 void BubbleSort(int arr[], int n)
 5 
 6 {
 7 
 8   int tmp;
 9     for (int i = 0; i < n - 1; i++)
10     {
11         for (int j = 0; j < n - 1; j++)
12         {
13             if (arr[j] > arr[j + 1])
14             {
15                 tmp = arr[j];
16                 arr[j] = arr[j + 1];
17                 arr[j + 1] = tmp;
18             }
19         }
20 
21     }
22 
23 }
24 
25  
26 
27 int main()
28 
29 {
30 
31   int arr[10] = {3,6,4,1,7,9,5,0,2,8};
32 
33   BubbleSort(arr, 10);
34 
35 }
View Code