zoukankan      html  css  js  c++  java
  • Seletion Sort

    referrence: GeeksforGeeks

    The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array.

    1) The subarray which is already sorted.
    2) Remaining subarray which is unsorted.

    In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray.

     1 public static void SelectionSort ( int [ ] num )
     2 {
     3      int i, j, min, minIndex;  
     4      for ( i = 0; i < num.length; i++ )  
     5      {
     6           min = num[i];   //initialize min
     7           for(j = i; j < num.length; j ++)   //inside loop
     8           {
     9                if( num[j] < min) { 
    10                  min = num[j];     
    11                  minIndex = j;
    12                }
    13           }
    14           //swap min and num[i];
    15           num[j] = num[i];
    16           num[i] = min;
    17       }           
    18 }

    Time comlexity O(n^2), space cost O(1), and it is in-place sorting.

  • 相关阅读:
    go语言与区块链
    git+jenkins
    cicd
    devops
    Go 并发
    Thinkphp5-未定义数据库类型
    用golang写爬虫
    kubernetes-通过VMware搭建k8s集群遇到的问题
    亚马逊全球开店2019
    kafka命令大全
  • 原文地址:https://www.cnblogs.com/ireneyanglan/p/4856644.html
Copyright © 2011-2022 走看看