-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlopSortFinal.java
More file actions
95 lines (72 loc) · 1.8 KB
/
FlopSortFinal.java
File metadata and controls
95 lines (72 loc) · 1.8 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import java.io.*;
import java.util.*;
import java.util.Scanner;
import java.util.Random;
public class FlopSortFinal{
public static int comparisons;
public static int swaps;
public static void main(String args []){
FlopSortFinal f = new FlopSortFinal();
Random r = new Random();
int[] arr = new int[10000];
for (int i = 0; i < arr.length; i++){
arr[i] = r.nextInt(100000);
}
//f.printArr(arr);
long start = System.nanoTime();
f.flopSort(arr, 0, arr.length - 1);
long finish = System.nanoTime();
long totalTime = (finish - start) / 1000;
System.out.println("Total time: " + totalTime + " Microseconds \nPassed: " + f.sorted(arr));
System.out.println("comparisons: " + comparisons + "\nSwaps: " + swaps);
//f.printArr(arr);
}
public FlopSortFinal(){
comparisons = 0;
swaps = 0;
}
/*A recursive sorting algorithm that */
public int[] flopSort(int[] arr, int s, int e){
int i = s;
int j = e;
boolean iMove = true;
int pos = 0;
if ((e - s) < 1)
return arr;
while(i < j){
comparisons++;
if (arr[i] > arr[j]){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
swaps++;
if(iMove)
iMove = false;
else
iMove = true;
}
if(iMove)
i++;
if (!iMove)
j--;
pos = j;
}
arr = flopSort(arr, pos, e);
arr = flopSort(arr, s, pos-1);
return arr;
}
//Test to see an array is sorted correctly
public boolean sorted(int[] arr){
for(int i = 0; i < arr.length-1; i++){
if (arr[i] > arr[i+1])
return false;
}
return true;
}
//Print array...
public void printArr(int[] arr){
for(int i : arr)
System.out.print(i + ", ");
System.out.println("");
}
}