forked from Annex5061/java-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BubbleSort.java
44 lines (43 loc) · 1.3 KB
/
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
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.Scanner;
public class BubbleSort {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[] a = new int[5];
System.out.println("Enter the elements of the array: ");
for (int i = 0; i<a.length; i++){
a[i] = scan.nextInt();
}
System.out.println("Array before sorting: ");
print(a);
long start = System.nanoTime();
bubble(a);
long end = System.nanoTime();
long execution = end - start;
System.out.println("Array after sorting: ");
print(a);
System.out.println("Execution time: " + execution + " nanoseconds");
}
static void print(int[] a){
for (int i : a) {
System.out.print(i + " ");
}
System.out.println();
}
public static void bubble(int[] a){
int x = a.length;
for(int i = 0; i<x-1; i++){
int flag = 0;
for (int j = 0; j<x-1; j++){
if (a[j]> a[j+1]){
int temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
flag++;
}
}
if (flag == 0){
break;
}
}
}
}