From 946e1b81b419b3bf9aeb876603d8a445f134b242 Mon Sep 17 00:00:00 2001 From: 4bh1n4v <54211410+4bh1n4v@users.noreply.github.com> Date: Thu, 1 Oct 2020 02:16:11 +0530 Subject: [PATCH] Here is a JAVA Code for Bubble Sort just thought of adding it --- Bubblesort.java | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Bubblesort.java diff --git a/Bubblesort.java b/Bubblesort.java new file mode 100644 index 0000000..e99d22e --- /dev/null +++ b/Bubblesort.java @@ -0,0 +1,36 @@ +import java.util.*; +public class BubbleSortExample { + static void bubbleSort(int[] arr) { + int n = arr.length; + int temp = 0; + for(int i=0; i < n; i++){ + for(int j=1; j < (n-i); j++){ + if(arr[j-1] > arr[j]){ + //swap elements + temp = arr[j-1]; + arr[j-1] = arr[j]; + arr[j] = temp; + } + + } + } + + } + public static void main(String[] args) { + int arr[] ={3,60,35,2,45,320,5}; + + System.out.println("Array Before Bubble Sort"); + for(int i=0; i < arr.length; i++){ + System.out.print(arr[i] + " "); + } + System.out.println(); + + bubbleSort(arr);//sorting array elements using bubble sort + + System.out.println("Array After Bubble Sort"); + for(int i=0; i < arr.length; i++){ + System.out.print(arr[i] + " "); + } + + } +} \ No newline at end of file