From fc9e3f619673ba95632cfe610b9c911cd18243c2 Mon Sep 17 00:00:00 2001 From: Shubhk15 Date: Sat, 1 Apr 2017 23:42:34 +0530 Subject: [PATCH] Changes in for loop for sorting This would decrease the number of comparisons in the loop used for sorting. --- Bubble Sort.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Bubble Sort.java b/Bubble Sort.java index b1147d1ba..a374d9ff7 100644 --- a/Bubble Sort.java +++ b/Bubble Sort.java @@ -15,15 +15,15 @@ class BubbleSort } //Sorting - for(int i=0; i<6; i++) + for(int i=0; i<5; i++) { - for(int j=0; j<5; j++) + for(int j=i+1; j<6; j++) { - if(array[j]>array[j+1]) + if(array[j]>array[i]) { int temp=array[j]; - array[j]=array[j+1]; - array[j+1]=temp; + array[j]=array[i]; + array[i]=temp; } } } @@ -35,4 +35,4 @@ class BubbleSort } } -} \ No newline at end of file +}