-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubble Sort.java
More file actions
33 lines (33 loc) · 954 Bytes
/
Bubble Sort.java
File metadata and controls
33 lines (33 loc) · 954 Bytes
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
33
package com.company;
import java.util.*;
public class Main {
public static void main(String[] args) throws java.lang.Exception {
//your code here
//taking Inputs
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
//function called
bubbleSort(arr);
}
//bubble sort
static void bubbleSort(int[] arr) {
int temp = 0;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i- 1; j++) {
if (arr[j] > arr[j + 1]) {
//swap
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}