-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArraySort.java
More file actions
164 lines (141 loc) · 4.37 KB
/
Copy pathArraySort.java
File metadata and controls
164 lines (141 loc) · 4.37 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package basicAlgorithm;
import java.io.IOException;
public class ArraySort {
private int[] array;
private int array_len;
public static int count = 0;
public static int count2 = 0;
public ArraySort(String filename) throws IOException {
super();
array = new int[10000];
array_len = ArrayFormation.readFileToArray(array, 10000, filename);
}
/*
* currently just choose first element in the partition as pivot
*/
static public int choosePivot(int[] array, int lowIndex, int highIndex) {
// choose first one as Pivot, do nothing
// choose last one as Pivot, swap the last one and first one;
//int tmp = array[lowIndex];
// // array[lowIndex]=array[highIndex];
// // array[highIndex]=tmp;
//
// // choose the median of first, last and middle(left middle if the
// array
// // is even length)
// int midIndex = (highIndex + lowIndex) / 2;
// if (array[lowIndex] < array[midIndex]
// && array[midIndex] < array[highIndex]
// || array[highIndex] < array[midIndex]
// && array[midIndex] < array[lowIndex]) {
// // midIndex is median
// array[lowIndex] = array[midIndex];
// array[midIndex] = tmp;
// } else if (array[midIndex] < array[highIndex]
// && array[highIndex] < array[lowIndex]
// || array[lowIndex] < array[highIndex]
// && array[highIndex] < array[midIndex]) {
// // highIndex is median
// array[lowIndex] = array[highIndex];
// array[highIndex] = tmp;
// }// lowIndex is median, don't do anything
//
return array[lowIndex];
}
public static void quickSort(int[] array, int lowIndex, int highIndex) {
// this.printArray();
// System.out.println("low:high="+lowIndex+":"+highIndex);
if (lowIndex >= highIndex)
return;
count2 += (highIndex - lowIndex);
int i = lowIndex + 1;
int pivot = choosePivot(array, lowIndex, highIndex);
int tmp = 0;
for (int j = lowIndex + 1; j <= highIndex; j++) {
count++;
// compare array[j] and pivot, see if needs swap
if (array[j] < pivot) {
// if there is nothing <p yet, then just move i forward;
if (i != j) {
// swap array[j] and array[i] unless there is nothing
// smaller than pivot yet
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
}
i++;
}
}
// swap pivot and array[i-1]
tmp = array[i - 1];
array[i - 1] = pivot;
array[lowIndex] = tmp;
// element i-1 is the pivot, doesn't need to sort.
quickSort(array, lowIndex, i - 2);
quickSort(array, i, highIndex);
}
public static void quickSort2(int[] array, int[] array2, int lowIndex,
int highIndex) {
// this.printArray();
// System.out.println("low:high="+lowIndex+":"+highIndex);
if (lowIndex >= highIndex)
return;
count2 += (highIndex - lowIndex);
int i = lowIndex + 1;
int pivot = choosePivot(array, lowIndex, highIndex);
int pivot2 = choosePivot(array2, lowIndex, highIndex);
int tmp = 0;
int tmp2 = 0;
for (int j = lowIndex + 1; j <= highIndex; j++) {
count++;
// compare array[j] and pivot, see if needs swap
if (array[j] < pivot) {
// if there is nothing <p yet, then just move i forward;
if (i != j) {
// swap array[j] and array[i] unless there is nothing
// smaller than pivot yet
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
tmp2 = array2[j];
array2[j] = array2[i];
array2[i] = tmp2;
}
i++;
}
}
// swap pivot and array[i-1]
tmp = array[i - 1];
array[i - 1] = pivot;
array[lowIndex] = tmp;
// swap pivot and array[i-1]
tmp2 = array2[i - 1];
array2[i - 1] = pivot2;
array2[lowIndex] = tmp2;
// element i-1 is the pivot, doesn't need to sort.
quickSort2(array, array2, lowIndex, i - 2);
quickSort2(array, array2, i, highIndex);
}
public static void printArray(int array_len,int[] array) {
for (int i = 0; i < array_len; i++) {
if (i % 100 == 0)
System.out.println();
System.out.print(array[i] + " ");
}
System.out.println();
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
ArraySort newObj = new ArraySort("QuickSort.txt");
// newObj.printArray();
System.out.println("array_len =" + newObj.array_len);
// newObj.quickSort(newObj.array, 0, newObj.array_len - 1);
System.out.println("--------sorted: ---------");
//newObj.printArray();
System.out.println("count of iteration: " + ArraySort.count);
System.out.println("count2 of iteration: " + ArraySort.count2);
}
}