Merge pull request #1 from UsernameToLon/UsernameToLon-BubbleSort

Updated to more efficient version if array size is scaled.
This commit is contained in:
UsernameToLon
2017-05-18 14:12:36 -04:00
committed by GitHub

View File

@ -16,8 +16,12 @@ class BubbleSort
*/ */
public static void main(String[] args) public static void main(String[] args)
{ {
int array[]=new int[6]; int size = 6;
int array[]=new int[size];
boolean swap;
int last = size - 1;
Scanner input=new Scanner(System.in); Scanner input=new Scanner(System.in);
//Input //Input
System.out.println("Enter any 6 Numbers for Unsorted Array : "); System.out.println("Enter any 6 Numbers for Unsorted Array : ");
@ -27,18 +31,22 @@ class BubbleSort
} }
//Sorting //Sorting
for(int i=0; i<5; i++) do
{ {
for(int j=i+1; j<6; j++) swap = false;
{ for (int count = 0; count < last; count++)
if(array[j]>array[i]) {
{ if (array[count] > array[count + 1])
int temp=array[j]; {
array[j]=array[i]; int temp = array[count];
array[i]=temp; array[count] = array[count + 1];
} array[count + 1] = temp;
} swap = true;
} }
}
last--;
} while (swap);
//Output //Output
for(int i=0; i<6; i++) for(int i=0; i<6; i++)