冒泡排序

 1 int temp;
 2             int[] arrSort = new int[] { 10, 8, 3, 5, 6, 7, 9 };
 3             for (int i = 0; i < arrSort.Length; i++)
 4             {
 5                 for (int j = i + 1; j < arrSort.Length; j++)
 6                 {
 7                     if (arrSort[j] < arrSort[i])
 8                     {
 9                         temp = arrSort[j];
10                         arrSort[j] = arrSort[i];
11                         arrSort[i] = temp;
12                     }
13                 }
14             }

另一种排序方法(不符合冒泡排序的原理)

 1 int i, j, temp;
 2             int[] arrSort = new int[] { 10, 8, 3, 5, 6, 7, 9 };
 3             for (j = 0; j < arrSort.Length; j++)
 4             {
 5                 for (i = 0; i < arrSort.Length - 1; i++)
 6                 {
 7                     if (arrSort[i] > arrSort[i + 1])
 8                     {
 9                         temp = arrSort[i];
10                         arrSort[i] = arrSort[i + 1];
11                         arrSort[i + 1] = temp;
12                     }
13 
14                     count++;
15                 }
16             }

 如果用一个变量记录内循环 循环次数 就会发现第二种排序方法 循环次数高于冒泡排序

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-03
  • 2021-12-05
  • 2021-10-15
  • 2021-10-16
猜你喜欢
  • 2019-03-22
  • 2022-01-12
  • 2021-08-31
  • 2022-01-06
  • 2021-12-12
  • 2021-06-25
相关资源
相似解决方案