-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubleSort.java
More file actions
36 lines (31 loc) · 997 Bytes
/
BubleSort.java
File metadata and controls
36 lines (31 loc) · 997 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
import java.util.*;
public class BubleSort {
public void bubbleSort(long arr[]) {
for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; j < arr.length -1 - i; j++) {
if (arr[j] > arr[j + 1]) {
long temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
public static void main(String[] args) {
long arr[] = new long[100000];
Random rand = new Random();
for (int i = 0; i < arr.length; i++) {
arr[i] = rand.nextInt(1000);
System.out.print(arr[i]+" ");
}
BubleSort bs = new BubleSort();
long start = System.nanoTime();
bs.bubbleSort(arr);
long end = System.nanoTime();
long duriation= end - start;
double elapsedTime = (double)duriation/1_000_000_000;
System.out.println("\nThe Sorted array is : ");
System.out.println(Arrays.toString(arr));
System.out.println("The time taken to sort "+arr.length+" elements is: "+elapsedTime+ " seconds");
}
}