forked from Dokyung-Hwang/CSAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/Dokyung-Hwang/CSAlgo
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
src/algorithm/solution/smileDK/programmers/sort/BubbleSort.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} |