1 package select;
 2 
 3 import java.util.Scanner;
 4 
 5 /*采用最简单的选择方式:从头到尾扫描序列找出最小的记录和第一个记录交换,接着在剩下的记录中继续这种选择和交换,最终使序列有序
 6  * 时间复杂度:O(n^2)
 7  * 此算法的额外空间只有一个temp,因此空间复杂度为O(1)*/
 8 public class jiandanselectSort {
 9 
10     public static void main(String[] args){
11         Scanner cin = new Scanner(System.in);
12         String str = cin.nextLine();
13         String[] st = str.split(" ");
14         int[] c = new int[st.length];
15         for(int i=0;i<c.length;i++){
16             c[i]=Integer.parseInt(st[i]);
17         }
18         sort(c);
19         for(int i=0;i<c.length;i++){
20             System.out.print(c[i]);
21             System.out.print(" ");
22         }
23         
24     }
25     public static void sort(int[] R){
26         int i,j,k;
27         int temp;
28         for(i=0;i<R.length;i++){
29             k=i;//k表示最小的元素的坐标;
30             //在无序序列中找到最小的元素
31             for(j=i+1;j<R.length;j++){
32                 if(R[k]>R[j]){
33                     k=j;
34                 }
35             }
36             //最小元素和无序序列的第一个元素交换
37             temp = R[k];
38             R[k]=R[i];
39             R[i]=temp;
40         }
41     }
42 }

 

分类:

技术点:

相关文章: