数据结构-冒泡排序算法

来源:互联网 发布:python 字符串转日期 编辑:程序博客网 时间:2024/05/21 14:47

int temp;
 int[] arr = new int[] { 1, 3, 8, 4, 5, 7, 10, 12 };
          
 for (int j = 0; j < arr.Length; j++)
 {
      for (int i = arr.Length - 1; i > j; i--)
      {
             int a = arr[j];
             int b = arr[i];
             if (arr[j] > arr[i])
             {
                  temp = arr[j];
                  arr[j] = arr[i];
                  arr[i] = temp;
              }
       }
 }

实现原理:

 1.外层取一个元素和内层一一对比,如果外层的值大于内层,将其兑换位置。(升序)

原创粉丝点击