Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ShaileshSurya/c2bb69c443cd88cceb0260dc4e83e59b to your computer and use it in GitHub Desktop.
Save ShaileshSurya/c2bb69c443cd88cceb0260dc4e83e59b to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class SelectionSort{
public static final int MAX=9999;
private int [] unsortedArray;
public void setUnsortedArray(int []unsortedArray){
this.unsortedArray= unsortedArray;
}
public int [] getInputs(){
System.out.println("Tell us the number of inputs");
Scanner s= new Scanner(System.in);
int sizeOfArray= s.nextInt();
int [] unsortedArray= new int[sizeOfArray];
for(int i=0;i<unsortedArray.length;i++){
System.out.print("Element:-");
unsortedArray[i]= s.nextInt();
}
return unsortedArray;
}
public void print(int myArray[]){
for(int i=0;i<myArray.length;i++){
System.out.print("\t"+myArray[i]);
}
}
public int[] getUnsortedArray(){
return unsortedArray;
}
public static int [] sort(int []myArray){
int []sortedArray= new int[myArray.length];
for(int i=0;i<myArray.length;i++){
//Index of smallest Integer
int smallest =0;
for(int j=0;j<myArray.length;j++){
if(myArray[j]<myArray[smallest]){
smallest= j;
}
}
sortedArray[i]= myArray[smallest];
myArray[smallest]= MAX;
}
for(int i=0;i<myArray.length;i++){
System.out.println(+sortedArray[i]+">>>");
}
return sortedArray;
}
public static void main(String args[]){
System.out.println("hi ..! You are at the selection sort world");
SelectionSort mySort= new SelectionSort();
mySort.setUnsortedArray(mySort.getUnsortedArray());
mySort.print(mySort.sort(mySort.getUnsortedArray()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment