八大基本排序算法-----选择排序

选择排序(SelctionSort)


    • 基本思想:
      在长度为N的无序数组中,第一次遍历n-1个数,找到最小的数值与第一个元素交换;
      第二次遍历n-2个数,找到最小的数值与第二个元素交换;
      。。。
      第n-1次遍历,找到最小的数值与第n-1个元素交换,排序完成。

    • 过程:

  •  

     

    • 平均时间复杂度:O(n2)

    • 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;
             }
         }
      }

       

posted @ 2020-11-22 18:23  lovivi  阅读(141)  评论(0)    收藏  举报