Bubble Sort [codingblocks]
Take as input N, the size of array. Take N more inputs and store that in an array. Write a function that bubble sorts the array. Print the elements of sorted array.
1.It reads a number N.
2.Take Another N numbers as input and store them in an Array.
3.Bubble sort the array and print the Array.
Input Format
Constraints
N cannot be Negative. Range of Numbers can be between -1000000000 to 100000000.
Output Format
Sample Input
4
2
-18
45
30
Sample Output
-18
2
30
45
Explanation
For each test case write bubble sorting program to sort the elements of the array.
Java Code:
IDE Code: https://ide.geeksforgeeks.org/LTyteiV1XJ
/* Amit Kumar 04-12-2020 IDE Code: https://ide.geeksforgeeks.org/LTyteiV1XJ */ package array; import java.util.Scanner; public class Array_BubbleSort { public static Scanner scanner = new Scanner(System.in); public static void main(String [] args) { int arrSize = scanner.nextInt(); int [] arr = new int[arrSize]; for (int i=0; i<arrSize; i++) { arr[i] = scanner.nextInt(); } bubbleSort(arr); for (int value : arr) { System.out.println(value); } } public static void bubbleSort(int[] arr) { for (int i=0; i<arr.length; i++) { for (int j=0; j<arr.length-i-1; j++) { if (arr[j] > arr[j+1]) { int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } } }
Comments
Post a Comment