From c88c41daa93082d2c953ebf0666dfb12dd6a0347 Mon Sep 17 00:00:00 2001 From: KUNDAN SOLANKI <116949800+KUNDAN1334@users.noreply.github.com> Date: Sat, 21 Oct 2023 11:42:56 +0530 Subject: [PATCH] Create Bubble Sort add the code of bubble sort algorithm with example --- Bubble Sort | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Bubble Sort diff --git a/Bubble Sort b/Bubble Sort new file mode 100644 index 0000000..436662e --- /dev/null +++ b/Bubble Sort @@ -0,0 +1,35 @@ +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] + " "); + } + + } +}