ねーあんた何にをしたいの?

 

【Java算法】Selection選擇排序String

import java.util.*;

public class SelectionSortStr {

public static void main(String[] args){

/**Both descending and ascending orders are also going

 * to be case sensitive. * */

Scanner input=new Scanner(System.in);

//Use comparable data type to compare strings easily

Comparable[] arr=new Comparable[5];

for(int i=0;i<arr.length;i++){

System.out.println("Please enter the word No."+(i+1)+": ");

arr[i]=input.nextLine();

}

selectionSort(arr);//Calling selectionSort();

System.out.println("\nIn ascending Order:");

for(int i=arr.length-1;i>=0;i--){

//Output in descending Order

System.out.println(arr[i]);

}

System.out.println("\nIn decending Order:");

for(int i=0;i<arr.length;i++){

System.out.println(arr[i]+"");

    //Output in ascending order

    }

}

//Selection sort algorithm

public static void selectionSort(Comparable[] arr){

int i; int j;

for(i=0; i<arr.length-1;i++){

for(j=i+1; j<arr.length;j++){

//i+1=j because j is greater than i then they swap

if(arr[i].compareTo(arr[j])<0){//Swapping string values

Comparable temp=arr[i];

arr[i]=arr[j];

arr[j]=temp;

}

}

}

}

}