From af37b928abe4387abbb501f7e8f200401f616895 Mon Sep 17 00:00:00 2001 From: Shrutiiig <32226484+Shrutiiig@users.noreply.github.com> Date: Wed, 23 Oct 2019 22:04:14 +0530 Subject: [PATCH] bubble-sort Bubble-Sort by Shruti --- Intermediate/bubble-sort | 41 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Intermediate/bubble-sort diff --git a/Intermediate/bubble-sort b/Intermediate/bubble-sort new file mode 100644 index 00000000..6bb0083f --- /dev/null +++ b/Intermediate/bubble-sort @@ -0,0 +1,41 @@ +package com.java2novice.algos; + +public class MyBubbleSort { + + // logic to sort the elements + public static void bubble_srt(int array[]) { + int n = array.length; + int k; + for (int m = n; m >= 0; m--) { + for (int i = 0; i < n - 1; i++) { + k = i + 1; + if (array[i] > array[k]) { + swapNumbers(i, k, array); + } + } + printNumbers(array); + } + } + + private static void swapNumbers(int i, int j, int[] array) { + + int temp; + temp = array[i]; + array[i] = array[j]; + array[j] = temp; + } + + private static void printNumbers(int[] input) { + + for (int i = 0; i < input.length; i++) { + System.out.print(input[i] + ", "); + } + System.out.println("\n"); + } + + public static void main(String[] args) { + int[] input = { 4, 2, 9, 6, 23, 12, 34, 0, 1 }; + bubble_srt(input); + + } +}