Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Jul 23, 2024
2 parents 0c77558 + 615c328 commit fbc5861
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/algorithm/solution/smileDK/programmers/sort/BubbleSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package algorithm.solution.smileDK.programmers.sort;

import java.util.Arrays;

public class BubbleSort {

public static int[] bubbleSort(int[] arr) {
return bubbleSort(arr, arr.length);
}

public static int[] bubbleSort(int[] arr, int size) {

for (int i = 1; i < size; i++) {
for (int j = 0; j < size - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr, j, j+1);
}
}
}

return arr;
}

private static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

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

0 comments on commit fbc5861

Please sign in to comment.