首页 > 其他 > 详细

Seletion Sort

时间:2015-10-06 06:57:55      阅读:216      评论:0      收藏:0      [点我收藏+]

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.

 

Seletion Sort

原文:http://www.cnblogs.com/ireneyanglan/p/4856644.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!