From 1f931ab0a1aa4005102254e04effb79ceb12eb58 Mon Sep 17 00:00:00 2001 From: "Smile:DK" Date: Tue, 23 Jul 2024 16:37:23 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Feat=20:=20[20240723=20|=20smile:DK?= =?UTF-8?q?]=20Bubble=20Sort=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../smileDK/programmers/sort/BubbleSort.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/algorithm/solution/smileDK/programmers/sort/BubbleSort.java diff --git a/src/algorithm/solution/smileDK/programmers/sort/BubbleSort.java b/src/algorithm/solution/smileDK/programmers/sort/BubbleSort.java new file mode 100644 index 0000000..c1a57e4 --- /dev/null +++ b/src/algorithm/solution/smileDK/programmers/sort/BubbleSort.java @@ -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))); + } +}