Updated to more efficient version if array size is scaled.
Changed loop from "for" to "do-while". Added decrement at end of do loop to decrease size of array tested after each iteration.
This commit is contained in:
parent
1bd077ea41
commit
e5dd6eff02
@ -16,9 +16,13 @@ class BubbleSort
|
||||
*/
|
||||
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);
|
||||
|
||||
|
||||
//Input
|
||||
System.out.println("Enter any 6 Numbers for Unsorted Array : ");
|
||||
for(int i=0; i<6; i++)
|
||||
@ -27,19 +31,23 @@ class BubbleSort
|
||||
}
|
||||
|
||||
//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];
|
||||
array[i]=temp;
|
||||
}
|
||||
int temp = array[count];
|
||||
array[count] = array[count + 1];
|
||||
array[count + 1] = temp;
|
||||
swap = true;
|
||||
}
|
||||
}
|
||||
|
||||
last--;
|
||||
} while (swap);
|
||||
|
||||
//Output
|
||||
for(int i=0; i<6; i++)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user