Created
July 15, 2017 06:20
-
-
Save ShaileshSurya/c2bb69c443cd88cceb0260dc4e83e59b 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; | |
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