源码如下
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 }