From b5c1cc7b5910c7ef98ec1b24dcebc31685f8ee74 Mon Sep 17 00:00:00 2001 From: Taha Jamal <60614154+shadowasphodel2919@users.noreply.github.com> Date: Sat, 23 Oct 2021 22:16:01 +0530 Subject: [PATCH] Added bubble sort --- JAVA/Sort/BubbleSort.java | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 JAVA/Sort/BubbleSort.java diff --git a/JAVA/Sort/BubbleSort.java b/JAVA/Sort/BubbleSort.java new file mode 100644 index 0000000..52e515c --- /dev/null +++ b/JAVA/Sort/BubbleSort.java @@ -0,0 +1,37 @@ +package Java.Sort; + +import java.util.*; +class BubbleSort{ + public static void bubbleSort(int[] a){ + + int len = a.length; + for(int i = 0; i < len - 1; i++){ + for(int j = 0; j < len - i -1; j++){ + if(a[j] < a[j+1]){ + int temp = a[j]; + a[j] = a[j+1]; + a[j+1] = temp; + } + } + } + + } + + public static void main(String[] args){ + + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + int[] a = new int[n]; + for(int i = 0; i < n; i++){ + a[i] = sc.nextInt(); + } + + bubbleSort(a); + + for(int i = 0; i < n; i++){ + System.out.println(a[i]); + } + + sc.close(); + } +} \ No newline at end of file