-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHighArray.java
More file actions
196 lines (173 loc) · 4 KB
/
Copy pathHighArray.java
File metadata and controls
196 lines (173 loc) · 4 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
public class HighArray {
private long[] array;
int numElems;
public HighArray(int max) {
this.numElems = 0;
this.array = new long[max];
}
public int size() {
return numElems;
}
public boolean find(long searchKey) {
int j = 0;
for (j = 0; j < numElems; j++) {
if (array[j] == searchKey) {
return true;
}
}
return false;
}
public void insert(long value) {
this.array[numElems] = value;
numElems++;
}
public boolean delete(long value) {
int j;
for (j = 0; j < numElems; j++) {
if (array[j] == value) {
break;
}
}
if (j == numElems) {
return false;
}
for (int k = j; k < numElems; k++) {
array[k] = array[k + 1];
}
numElems--;
return true;
}
public void display() {
for (int j = 0; j < numElems; j++) {
System.out.print(array[j] + " ");
}
System.out.println(" ");
}
public long removeMax() {
if (numElems == 0) {
return -1;
} else {
long max = -1;
for (int i = 0; i < numElems; i++) {
if (array[i] > max) {
max = array[i];
}
}
delete(max);
return max;
}
}
public void removeDups() {
for (int i = 0; i < numElems; i++) {
for (int j = i+1; j < numElems; j++) {
if (array[i] == array[j]) {
array[j] = -1;
}
}
}
while (find(-1)) {
delete(-1);
}
}
public void swap(int index1,int index2) {
long temp = array[index2];
array[index2] = array[index1];
array[index1] = temp;
}
public void bubbleSort() {
for (int i = numElems - 1; i > 1; i--) {
for (int j = 0; j < i; j++) {
if (array[j+1] < array[j]) {
swap(j,j+1);
}
}
}
}
public void selectionSort() {
for (int i = 0; i < numElems - 1; i++) {
int min = i;
for (int j = i + 1; j < numElems; j++) {
if (array[j] < array[min]) {
min = j;
}
}
swap(min,i);
//display();
}
}
public void insertionSort() {
long innerCount = 0;
for (int i = 1; i < numElems; i++) {
int marker = i;
long markerVal = array[i];
while (marker > 0 && array[marker - 1] > markerVal) {
array[marker] = array[marker - 1];
marker--;
innerCount++;
}
array[marker] = markerVal;
//display();
}
System.out.printf("%d \n" ,innerCount);
}
public void insertionSortAndRemoveDups() {
int count = 0;
for (int i = 1; i < numElems; i++) {
int marker = i;
long markerVal = array[i];
while (marker > 0 && array[marker - 1] >= markerVal) {
if (array[marker - 1] == markerVal && markerVal != -1) {
markerVal = -1;
count++;
}
array[marker] = array[marker - 1];
marker--;
}
array[marker] = markerVal;
//display();
}
for (int j = count; j < numElems; j++) {
array[j-count] = array[j];
}
numElems -= count;
}
public void removeDupsSorted() {
int count = 0;
for (int i = 0; i < numElems - 1; i++) {
if (array[i] == array[i+1]) {
count++;
} else {
array[i-count] = array[i];
}
}
array[numElems - 1 - count] = array[numElems - 1];
numElems = numElems - count;
}
public long median() {
// if odd index
insertionSort();
if (numElems % 2 == 0) {
return (array[((numElems - 1 )/ 2)] + array[(((numElems - 1) / 2)) +
1])/2;
} else {
return array[(numElems - 1)/2];
}
}
public void oddEvenSort() {
int numOddSwaps = numElems % 2 == 0 ? numElems / 2 : (numElems / 2 ) + 1;
int count = 0;
while (count != numOddSwaps) {
for (int i = 1; i + 1 < numElems; i = i + 2) {
if (array[i+1] < array[i]) {
swap(i,i+1);
}
}
for (int j = 0; j + 1< numElems; j = j + 2) {
if (array[j + 1] < array[j] && j + 1 < numElems) {
swap(j,j+1);
}
}
count++;
}
}
}