Skip to content

Instantly share code, notes, and snippets.

@sudarshan14
Created April 22, 2018 17:46
Show Gist options
  • Select an option

  • Save sudarshan14/d5d8737fd99ab938f5d4ffc0bdabbf94 to your computer and use it in GitHub Desktop.

Select an option

Save sudarshan14/d5d8737fd99ab938f5d4ffc0bdabbf94 to your computer and use it in GitHub Desktop.
package bhatt.sud.learning;
public class BinarySort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] intArray = new int[] {6,3,18,45,44,65,78,34,78,456,694,2,4,6,56,43,66,55,49,30};
System.out.println("Before: ");
for( int a : intArray){
System.out.print(" "+a);
}
for(int i = 0; i< intArray.length-1; i++) {
for(int j =0 ; j <intArray.length - i -1; j++) {
if(intArray[j] > intArray[j+1]) {
int temp = intArray[j];
intArray[j] = intArray[j+1];
intArray[j+1] = temp;
}
}
}
System.out.println(" \n After: ");
for( int a : intArray){
System.out.print(" "+a);
}
intArray = new int[] {5,3,67,4,6,8,44,6,2,74,84,26,67};
printSortedArray(intArray, 0);
for(int i = 0 ;i< intArray.length-1 ; i++) {
for(int j = 1; j< intArray.length-i ;j++) {
if(intArray[j-1] > intArray[j]) {
int temp = intArray[j-1];
intArray[j-1] = intArray[j];
intArray[j] = temp;
}
}
printSortedArray(intArray,i+1);
}
}
private static void printSortedArray(int [] arr, int iterationNo) {
System.out.print("\n iteration No:" +iterationNo+"\n");
for( int x : arr){
System.out.print(" "+x);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment