Skip to content

Commit

Permalink
Added remove, swap, and sort to ArrayList
Browse files Browse the repository at this point in the history
  • Loading branch information
durantjm198 authored and heuermh committed May 27, 2017
1 parent 1ad141e commit 0953574
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions lick/collect/ArrayList.ck
Expand Up @@ -197,4 +197,52 @@ public class ArrayList extends List
}
return result;
}

fun void remove(int index) {
for (index => int i; i < size() - 1; i++) {
set(i, get(i+1));
}
values.size(size() - 1);
}

// QuickSort
fun void sort(Comparator c) {
quickSort(0, size() - 1, c);
}

fun void quickSort(int low, int high, Comparator c) {

if (low < high) {
partition(low, high, c) => int p;
quickSort(low, p, c);
quickSort(p + 1, high, c);
}
}

fun int partition(int low, int high, Comparator c) {
get(low) @=> Object pivot;
low - 1 => int i;
high + 1 => int j;
while (true) {
do {
1 +=> i;
} while (c.compare(get(i), pivot) < 0);

do {
j - 1 => j;
} while (c.compare(get(j), pivot) > 0);

if (i >= j) {
return j;
}
swap(i, j);
}
}

fun void swap(int a, int b) {
get(a) @=> Object temp;
set(a, get(b));
set(b, temp);
}

}

0 comments on commit 0953574

Please sign in to comment.