算法导论Java实现-选择排序(习题2.2-2)

 

 
 
  1. package lhz.algorithm.chapter.two; 
  2.  
  3. /** 
  4.  * 《选择排序》,《算法导论》习题2.2-2  
  5.  * Consider sorting n numbers stored in array A by first 
  6.  * finding the smallest element of A and exchanging it with the element in A[1]. 
  7.  * Then find the second smallest element of A, and exchange it with A[2]. 
  8.  * Continue in this manner for the first n - 1 elements of A. Write pseudocode 
  9.  * for this algorithm, which is known as selection sort . What loop invariant 
  10.  * does this algorithm maintain? Why does it need to run for only the first n - 
  11.  * 1 elements, rather than for all n elements? Give the best-case and worst- 
  12.  * case running times of selection sort in Θ- notation.  
  13.  * 伪代码: 
  14.  *  for i <- 1 to length[A]-1 
  15.  *      key <- A[i] 
  16.  *      index <- i 
  17.  *      for j <- 2 to length[A] 
  18.  *          key = key < A[j] ? key : A[j]; 
  19.             index = key < A[j] ? index : j; 
  20.  *      A[index] = A[i]; 
  21.         A[i] = key;  
  22.  * @author lihzh(苦逼 coder) 
  23. * 本文地址:http://mushiqianmeng.blog.51cto.com/3970029/728872
  24.  */ 
  25. public class SelectionSort { 
  26.      
  27.     private static int[] input = new int[] { 21549867103 }; 
  28.  
  29.     /** 
  30.      * @param args 
  31.      */ 
  32.     public static void main(String[] args) { 
  33.         for (int i=0; i<input.length-1; i++) {//复杂度数量级:n 
  34.             int key = input[i]; 
  35.             int index = i; 
  36.             //比较当前值和下一个值的关系,记录下较小值的值和索引数,用于交换。 
  37.             for (int j=i+1; j<input.length; j++) {//复杂度:1+2+...+(n-1)=Θ(n^2)
  38.                 key = key < input[j] ? key : input[j]; 
  39.                 index = key < input[j] ? index : j; 
  40.             } 
  41.             input[index] = input[i]; 
  42.             input[i] = key; 
  43.         } 
  44.         /* 
  45.          * 复杂度分析: 
  46.          * 最坏情况下,复杂度为:n*n=n^2(若略微精确的计算即为:n-1+1+2+...+n-1=(2+n)*(n-1)/2, 
  47.          * 所以复杂度仍为n^2。 
  48.          * 最优情况下,由于不论原数组是否排序好,均需要全部遍历以确定当前的最小值,所以复杂度不变仍未n^2。 
  49.          *  
  50.          */ 
  51.         //打印数组 
  52.         printArray(); 
  53.     } 
  54.      
  55.     private static void printArray() { 
  56.         for (int i : input) { 
  57.             System.out.print(i + " "); 
  58.         } 
  59.     } 
  60.  

  

 
 
  1. package lhz.algorithm.chapter.two; 
  2.  
  3. /** 
  4.  * 《选择排序》,《算法导论》习题2.2-2  
  5.  * Consider sorting n numbers stored in array A by first 
  6.  * finding the smallest element of A and exchanging it with the element in A[1]. 
  7.  * Then find the second smallest element of A, and exchange it with A[2]. 
  8.  * Continue in this manner for the first n - 1 elements of A. Write pseudocode 
  9.  * for this algorithm, which is known as selection sort . What loop invariant 
  10.  * does this algorithm maintain? Why does it need to run for only the first n - 
  11.  * 1 elements, rather than for all n elements? Give the best-case and worst- 
  12.  * case running times of selection sort in Θ- notation.  
  13.  * 伪代码: 
  14.  *  for i <- 1 to length[A]-1 
  15.  *      key <- A[i] 
  16.  *      index <- i 
  17.  *      for j <- 2 to length[A] 
  18.  *          key = key < A[j] ? key : A[j]; 
  19.             index = key < A[j] ? index : j; 
  20.  *      A[index] = A[i]; 
  21.         A[i] = key;  
  22.  * @author lihzh(苦逼 coder) 
  23. * 本文地址:http://mushiqianmeng.blog.51cto.com/3970029/728872
  24.  */ 
  25. public class SelectionSort { 
  26.      
  27.     private static int[] input = new int[] { 21549867103 }; 
  28.  
  29.     /** 
  30.      * @param args 
  31.      */ 
  32.     public static void main(String[] args) { 
  33.         for (int i=0; i<input.length-1; i++) {//复杂度数量级:n 
  34.             int key = input[i]; 
  35.             int index = i; 
  36.             //比较当前值和下一个值的关系,记录下较小值的值和索引数,用于交换。 
  37.             for (int j=i+1; j<input.length; j++) {//复杂度:1+2+...+(n-1)=Θ(n^2)
  38.                 key = key < input[j] ? key : input[j]; 
  39.                 index = key < input[j] ? index : j; 
  40.             } 
  41.             input[index] = input[i]; 
  42.             input[i] = key; 
  43.         } 
  44.         /* 
  45.          * 复杂度分析: 
  46.          * 最坏情况下,复杂度为:n*n=n^2(若略微精确的计算即为:n-1+1+2+...+n-1=(2+n)*(n-1)/2, 
  47.          * 所以复杂度仍为n^2。 
  48.          * 最优情况下,由于不论原数组是否排序好,均需要全部遍历以确定当前的最小值,所以复杂度不变仍未n^2。 
  49.          *  
  50.          */ 
  51.         //打印数组 
  52.         printArray(); 
  53.     } 
  54.      
  55.     private static void printArray() { 
  56.         for (int i : input) { 
  57.             System.out.print(i + " "); 
  58.         } 
  59.     } 
  60.  

      本文转自mushiqianmeng 51CTO博客,原文链接:http://blog.51cto.com/mushiqianmeng/728872,如需转载请自行联系原作者




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值