浅析排序方法

源码如下

 1 /*Function:sortMethod
 2  *Written By: lifurong
 3  *Include 1Main_Class&2Part_Class
 4  *Error:input 2->runtime error
 5  */
 6 
 7 import java.util.Scanner;
 8 
 9 public class SortMethod{
10     public static void main(String[] args) {
11         final int NUMBER_OF_ELEMENT=5;
12         @SuppressWarnings("resource")
13         Scanner input=new Scanner(System.in);
14         double[] numbers=new double[NUMBER_OF_ELEMENT];
15         System.out.println("Please enter the array:");
16         for(int i=0;i<NUMBER_OF_ELEMENT;i++){
17             numbers[i]=input.nextDouble();
18         }
19         SortFunction.operaSort(numbers);//这里更改具体方法
20         System.out.println("positiveOutput input 1");
21         System.out.println("negitiveOutput input 2");
22         int FormOf_Sort=input.nextInt();
23         if(FormOf_Sort==1)
24             FormOfSort.positiveOutput(numbers,NUMBER_OF_ELEMENT);
25         if(FormOf_Sort==2)
26             FormOfSort.negativeOutput(numbers,NUMBER_OF_ELEMENT);
27         }
28 }
29 
30 //静态类排序方法
31 class SortFunction{
32     public static void operaSort(double[] list){//from small to high
33         double temp;
34         for(int i=0;i<list.length-1;i++){
35             for(int j=0;j<list.length-1-i;j++){
36                 if(list[j]>list[j+1]){
37                     temp=list[j];list[j]=list[j+1];list[j+1]=temp;
38                 }
39             }
40         }
41     }
42     
43     public static void selectSort(double[] list){
44         double temp;
45         for(int i=0;i<list.length-1;i++){
46             int currentMaxIndex=i;//假设最大值的下标就是i
47             for(int j=i+1;j<list.length;j++){//把i以后的所有值与i值比较,得出当前i这个位置的最大最小值
48                 if(list[j]>list[currentMaxIndex])
49                     currentMaxIndex=j;
50             }
51             temp=list[currentMaxIndex];
52             list[currentMaxIndex]=list[i];
53             list[i]=temp;
54         }
55     }
56 
57     public static void insertSort(double[] list){
58         for(int i=1;i<list.length;i++){
59             double currentElement=list[i];
60             int k;
61             for(k=i-1;k>=0&&list[k]>currentElement;k--){
62                 list[k+1]=list[k];
63             }
64             list[k+1]=currentElement;
65         }
66     }
67 }
68 
69 //静态类
70 class FormOfSort{
71     
72     public static void positiveOutput(double[] list,int n){
73         for(int i=0;i<n;i++){
74             System.out.print(list[i]+" ");
75         }
76     }
77     
78     public static void negativeOutput(double[] list,int n){
79         for(int i=n;i>=0;i--){
80             System.out.print(list[i-1]+" ");
81         }
82     }
83 }

 

转载于:https://www.cnblogs.com/Dark-code/p/8966690.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值