-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble_sort.java
More file actions
54 lines (42 loc) · 1.58 KB
/
bubble_sort.java
File metadata and controls
54 lines (42 loc) · 1.58 KB
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
47
48
49
50
51
52
53
54
import java.util.*;
public class bubble_sort{
public static void sort(int arr[]){
for(int i=0;i<arr.length-1;i++) {
int swap=0;
for (int j = 0; j <arr.length-1- i; j++) {
if (arr[j] > arr[j + 1]) {
int temp;
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swap++;
}
}
if(swap==0){
break;
}
}
/* for(int i=0;i<arr.length;i++) {
System.out.println(arr[i]);
}
*/
}
public static void main(String[] args){
int repeat=0;
long time[]=new long[30];//array for storing time
int arr[]=new int[10000];//the number of elements that are sorted are 10000
while(repeat<30) {
Random random=new Random(System.currentTimeMillis());
for(int i=0;i<arr.length;i++){
int k=random.nextInt(0,999999);
arr[i]=k;//all number generated by random generator are stored in a form of array
}
long begin = System.currentTimeMillis();//begin time
sort(arr);//if all number store in arr then it time to sort it so we pass our array in sort function
long end = System.currentTimeMillis();//end time
time[repeat] = end - begin;
System.out.println(time[repeat]);
repeat++;
}
}
}