冒泡排序:

 1 //复杂度:O(n^2)
 2 //比较次数:n*(n-1)/2 ;移动等数量级
 3 #include<iostream>
 4 #include<cstdio>
 5 #include<cstdlib>
 6 using namespace std;
 7 const int INF = 0x7fffffff;
 8 void Bubble_sort(int* A, int n)
 9 {
10     for(int i = n-1; i >= 0; i--)
11     {
12         A[n] = A[i];
13         int _max = -INF, pos = -1;
14         for(int j = 0; j <= i; j++)
15         {
16             if(_max < A[j]) _max = A[j], pos = j;
17         }
18         A[pos] = A[n];
19         A[i] = _max;
20     }
21     for(int i = 0; i < n; i++)  {if(i) printf(" "); printf("%d", A[i]);}
22     printf("\n");
23 }
24 
25 int main()
26 {
27     int A[10] = {1, 9, 3, 7, 5, 2, 8, 4, 6, 10};
28     Bubble_sort(A, 10);
29     return 0;
30 }
View Code

相关文章:

  • 2022-12-23
  • 2021-11-20
  • 2021-11-20
  • 2021-11-20
猜你喜欢
  • 2021-12-15
  • 2021-11-20
  • 2021-11-20
  • 2021-11-20
  • 2021-11-20
  • 2021-11-16
  • 2021-06-23
相关资源
相似解决方案