QuickSort

public class QuickSort {
public static int size = 10;
public int[] table = new int[size];

public void init() {
for (int i = 0; i < size; i = i + 1)
table[i] = ((int) (java.lang.Math.random() * 100));
qSort(table, 0, size - 1);
}

public void qSort(int[]l, int left, int right) {
int i, last;
if (!(left >= right)) {
swap(l, left, (left + right) / 2);
last = left;
for (i = left + 1; i <= right; i = i + 1)
if (l[i] < l[left])
swap(l, last = last + 1, i);
swap(l, left, last);
qSort(l, left, last - 1);
qSort(l, last + 1, right);
}
}

public void swap(int[]l, int first, int second) {
int tmp;
tmp = l[first];
l[first] = l[second];
l[second] = tmp;
}
}

Comments