Skip to content

Commit

Permalink
Merge pull request vanshikaarora#39 from Criptdestroyer/master
Browse files Browse the repository at this point in the history
Add Bubble sort with optimization java
  • Loading branch information
nb2998 authored Oct 4, 2019
2 parents 7a1a419 + 3faab23 commit 84f4a02
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions Sorting/BubbleSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.util.Arrays;

public class BubbleSort {

public static void main(String[] args) {
int[] arr = {1, 4, 2, 3, 6, 5};
bubbleSort(arr);
System.out.println(Arrays.toString(arr));
}

public static void bubbleSort(int[] arr) {
boolean swap = true;
int i = 0;

while(swap && i < arr.length){
swap = false;
for(int j=i; j<arr.length-1; j++){
if(arr[j] > arr[j+1]){
swap = swap(arr, j, j+1);
}
}
i++;
}
}

public static boolean swap(int[] arr, int index1, int index2) {
int temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
return true;
}
}

0 comments on commit 84f4a02

Please sign in to comment.