From 12138da6cbc70f0b4f1fd8766e431e6ff8f1c133 Mon Sep 17 00:00:00 2001 From: Shivam Gupta Date: Mon, 24 May 2021 17:54:21 +0530 Subject: [PATCH] enh(Java): more optimized way of bubble-sort (#321) --- algorithms/Java/sorting/bubble-sort.java | 57 +++++++++++++++--------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/algorithms/Java/sorting/bubble-sort.java b/algorithms/Java/sorting/bubble-sort.java index 61b88316..65819b9b 100644 --- a/algorithms/Java/sorting/bubble-sort.java +++ b/algorithms/Java/sorting/bubble-sort.java @@ -1,27 +1,40 @@ +import java.util.Scanner; +public class sorting { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); -import java.util.Arrays; + boolean sorted = true; -public class BubbleSort { - static int temp = 0; - static int[] nums = { 13, 34, 12, 45, 56, 32, 20 }; + System.out.println("Enter the size of array: "); + int n=sc.nextInt(); - public static void main(String[] args) { + int a[]=new int[n]; - System.out.println("Before Sorting : "+Arrays.toString(nums)); - sort(); + System.out.println("Enter the elements of array : "); + for (int i=0;i nums[i]) { - temp = nums[j]; - nums[j] = nums[i]; - nums[i] = temp; - } - } - } - System.out.println(Arrays.toString(nums)); - } -} \ No newline at end of file + for (int i=0;i