forked from 14visheshjain/DataStructure-Algorithm-in-java
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBubbleSort.java
More file actions
46 lines (36 loc) · 815 Bytes
/
BubbleSort.java
File metadata and controls
46 lines (36 loc) · 815 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
34
35
36
37
38
39
40
41
42
43
44
45
46
//Bubble Sort Code
import java.util.Scanner;
public class BubbleSort {
static Scanner a = new Scanner(System.in);
public static void main(String[] args) {
int[] arr = takeinput();
bubbleSort(arr);
display(arr);
}
public static int[] takeinput() {
System.out.println("size?");
int n = a.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = a.nextInt();
}
return arr;
}
public static void display(int[] a) {
for (int val : a) {
System.out.println(val);
}
}
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int counter = 0; counter < n - 1; counter++) {
for (int j = 0; j < (n - counter - 1); j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}