forked from istamendil/kfu-programming-java1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArraySort.java
More file actions
63 lines (50 loc) · 1.57 KB
/
Copy pathArraySort.java
File metadata and controls
63 lines (50 loc) · 1.57 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
55
56
57
58
59
60
61
62
63
import java.util.Arrays;
/**
*
* @author Alexander Ferenets (Istamendil) <[email protected]>
*/
public class ArraySort {
public static void main(String[] args) {
// Timer variable
double startTime, timeDifferece;
// Size of array for test
int size = 10000;
// Array for test
int[] arr = new int[size];
// Fill array with random values
for (int i = 0; i < size; i++) {
arr[i] = (int) Math.round(Math.random() * 1000);
}
// System.out.println("Generated array:");
// System.out.println(Arrays.toString(arr));
// Remember start time
startTime = System.nanoTime();
sortJavaSimple(arr);
// sortBubble(arr);
// Calculate time wasted for sorting
timeDifferece = (System.nanoTime() - startTime) / 1e6;
// System.out.println("Result array:");
// System.out.println(Arrays.toString(arr));
System.out.format("Time used for sorting: %.0fms\n", timeDifferece);
}
public static void sortJavaSimple(int[] arr) {
Arrays.sort(arr);
}
public static void sortBubble(int[] arr) {
int j;
boolean flag = true; // set flag to true to begin first pass
int temp;
while (flag) {
flag = false; //set flag to false awaiting a possible swap
for (j = 0; j < arr.length - 1; j++) {
if (arr[j] > arr[j + 1]){
temp = arr[j]; //swap elements
arr[j] = arr[j + 1];
arr[j + 1] = temp;
flag = true; //shows a swap occurred
}
// System.out.println(Arrays.toString(arr));
}
}
}
}