public class QuickSort<T extends Comparable<T>> {
public void sort(T[] t) {
sort(t, 0, t.length - 1);
}
private void sort(T[] t, int lo, int high) {
if (high <= lo)
return;
int index = partition(t, lo, high);
sort(t, lo, index - 1);
sort(t, index + 1, high);
}
private int partition(T[] t, int lo, int high) {
int i = lo;
int j = high + 1;
T pivot = t[lo];
while (true) {
while (t[++i].compareTo(pivot) < 0) {
if (i == high)
break;
}
while (t[--j].compareTo(pivot) > 0) {
if (j == lo)
break;
}
if (i >= j)
break;
exch(t, i, j);
}
exch(t, lo, j);
return j;
}
private void exch(T[] t, int i, int j) {
T temp = t[i];
t[i] = t[j];
t[j] = temp;
}
public String print(T[] a) {
String str = "";
for (int i = 0; i < a.length; i++) {
str += a[i];
str += " ";
}
return str;
}
public static void main(String[] args) {
Integer[] t = { 5, 9, 1, 10, 3, 6, 8, 2, 7, 4 };
for (int i = 0; i < t.length; i++) {
System.out.print(t[i] + " ");
}
System.out.println("\n-----------------------------");
QuickSort<Integer> quick = new QuickSort<Integer>();
quick.sort(t);
for (int i = 0; i < t.length; i++) {
System.out.print(t[i] + " ");
}
}
}
import java.lang.reflect.Array;
public class MergeSort<T extends Comparable<T>> {
public void sort(T[] t) {
sort(t, 0, t.length - 1);
}
private void sort(T[] t, int lo, int high) {
if (lo >= high)
return;
int mid = (lo + high) / 2;
sort(t, lo, mid);
sort(t, mid + 1, high);
merge(t, lo, mid, high);
}
private void merge(T[] t, int lo, int mid, int high) {
T[] copy = (T[]) Array.newInstance(t[0].getClass(), t.length);
for (int i = 0; i < t.length; i++) {
copy[i] = t[i];
}
System.out.println(print(copy));
int i = lo;
int j = mid + 1;
for (int k = lo; k <= high; k++) {
if (i > mid)
t[k] = copy[j++];
else if ( j > high)
t[k] = copy[i++];
else if (copy[i].compareTo(copy[j]) <= 0) {
t[k] = copy[i++];
} else {
t[k] = copy[j++];
}
}
}
private String print(T[] a) {
String str = "";
for (int i = 0; i < a.length; i++) {
str += a[i];
str += " ";
}
return str;
}
public static void main(String[] args) {
Integer[] t = { 5, 9, 1, 10, 3, 6, 8, 2, 7, 4 };
for (int i = 0; i < t.length; i++) {
System.out.print(t[i] + " ");
}
System.out.println("\n-----------------------------");
MergeSort<Integer> quick = new MergeSort<Integer>();
quick.sort(t);
for (int i = 0; i < t.length; i++) {
System.out.print(t[i] + " ");
}
}
}
public class MaxHeap<T extends Comparable<T>> {
private T[] pq;
private int N = 0; //in pq[1...N] with pg[0] unused
public MaxHeap(int maxN) {
pq = (T[]) new Comparable[maxN + 1];
}
private boolean less(int i, int j) {
return pq[i].compareTo(pq[j]) < 0;
}
private void exch(int i, int j) {
T t = pq[i];
pq[i] = pq[j];
pq[j] = t;
}
public int size() {
return N;
}
public boolean isEmpty() {
return N == 0;
}
public void insert(T t) {
pq[++N] = t;
swim(N);
}
public T delMax() {
T max = pq[1];
exch(1, N);
pq[N--] = null;
sink(1);
return max;
}
public void sink(int k) {
while (2 * k <= N) {
int j = 2 * k;
if (j < N && less(j, j + 1)) j++;
if (!less(k, j))
break;
exch(k, j);
k = j;
}
}
public void swim(int k) {
while (k > 1 && less(k / 2, k)) {
exch(k, k / 2);
k /= 2;
}
}
public static String print(Comparable[] a) {
String str = "";
for (int i = 0; i < a.length; i++) {
str += a[i];
str += " ";
}
return str;
}
public static void main(String[] args) {
String[] test = {"A", "N", "E", "P", "H", "O", "T", "R", "I", "G"};
int M = 11;
MaxHeap<String> sort = new MaxHeap<>(M);
for (int i = 0; i < test.length; i++) {
sort.insert(test[i]);
System.out.println(print(sort.pq));
if (sort.size() > M)
System.out.println(sort.delMax());
}
System.out.println(print(sort.pq));
sort.insert("S");
System.out.println(print(sort.pq));
}
}