diff --git a/BubbleSort.java b/BubbleSort.java new file mode 100644 index 0000000..bd651cd --- /dev/null +++ b/BubbleSort.java @@ -0,0 +1,51 @@ +public class BubbleSort { + + public static void bubbleSort(int[] arr) + { + int n = arr.length; + int flag = 0; + for(int i=0;iarr[j+1]) + { + int temp = arr[j]; + arr[j] = arr[j+1]; + arr[j+1] = temp; + flag=1; + } + } + if(flag==0) break; //This exits the loop if the swap statement in inner j loop doesn't execute, it means the array is sorted. + } + } + + static void printArray(int arr[]) + { + int n = arr.length; + + for(int i=0; i