选择排序(SelctionSort)

2020-04-15  本文已影响0人  风一样的code
1.基本思想

在长度为N的无序数组中,第一次遍历n-1个数,找到最小的数值与第一个数交换;
第二次遍历n-2个数,找到最小的数值与第二个数交换;
。。。。
第n-1次遍历,找到最小的数值与n-1个数进行交换;

2.过程
选择排序
3.平均时间复杂度:O(n2)
4.java代码实现:
public static void select_sort(int array[],int lenth){
      
      for(int i=0;i<lenth-1;i++){
          
          int minIndex = i;
          for(int j=i+1;j<lenth;j++){
             if(array[j]<array[minIndex]){
                 minIndex = j;
             }
          }
          if(minIndex != i){
              int temp = array[i];
              array[i] = array[minIndex];
              array[minIndex] = temp;
          }
      }
  }
上一篇 下一篇

猜你喜欢

热点阅读