-
Notifications
You must be signed in to change notification settings - Fork 0
/
BubbleSort.java
32 lines (29 loc) · 923 Bytes
/
BubbleSort.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.priyank;
import java.util.Arrays;
import java.util.Scanner;
public class BubbleSort {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("Enter the number of elements in the array");
int n = sc.nextInt();
int[] arr = new int[n];
//Getting input from the User
for (int i = 0; i < n; i++) {
arr[i]= sc.nextInt();
}
//Bubble Sort Algorithm
int temp;
for (int i = 0; i < n; i++) {
temp=0;
for (int j = 0; j < arr.length-(i+1); j++) {
if(arr[j]>arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1]= temp;
}
}
}
//Printing the Sorted Algorithm
System.out.println(Arrays.toString(arr));
}
}