Created
August 14, 2016 09:21
-
-
Save sbsatter/a279c005bbe6bfda8ac1718c1eb24605 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
class SelectionSort{ | |
public static void main(String ... args){ | |
int [] unsorted; | |
if(args.length!=0){ | |
unsorted= new int [args.length]; | |
int i=0; | |
for(String s: args){ | |
unsorted[i++]=Integer.parseInt(s); | |
} | |
}else{ | |
Scanner sc= new Scanner(System.in); | |
System.out.println("Enter the size of the unsorted array"); | |
int n= sc.nextInt(); | |
unsorted= new int[n]; | |
for(int i=0; i<n; i++){ | |
unsorted[i]=sc.nextInt(); | |
} | |
} | |
System.out.println("Starting sort"); | |
sort(unsorted); | |
} | |
public static int [] sort(int [] arr){ | |
int n=arr.length; | |
for(; n>0; n--){ | |
int max= arr[0]; | |
int max_index= 0; | |
for(int i=0; i<n; i++){ | |
if(arr[i]>max){ | |
max=arr[i]; | |
max_index=i; | |
} | |
} | |
int temp= arr[n-1]; | |
arr[n-1]= max; | |
arr[max_index]=temp; | |
System.out.println("Pass Number "+(arr.length-n+1)+" : "+ printArray(arr)); | |
} | |
return arr; | |
} | |
public static String printArray(int [] a){ | |
String array=""; | |
for(int i:a){ | |
array+= String.valueOf(i); | |
array+=" "; | |
} | |
return array; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment