Skip to content

Instantly share code, notes, and snippets.

@howardlau1999
Created December 26, 2018 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save howardlau1999/b795f388bb9ff415b40cfb05e54b701c to your computer and use it in GitHub Desktop.
Save howardlau1999/b795f388bb9ff415b40cfb05e54b701c to your computer and use it in GitHub Desktop.
Sort animation
import edu.princeton.cs.algs4.*;
public class HeapSort {
private static int currentSinking;
public static void sink(int[] heap, int k, int N) {
while (2 * k <= N) {
int j = 2 * k;
if (j < N && heap[j] < heap[j + 1]) j++;
if (heap[k] > heap[j]) break;// already ordered
exch(heap, k, j);
k = j;
}
}
public static void exch(int[] heap, int i, int j) {
int t = heap[i];
heap[i] = heap[j];
heap[j] = t;
}
public static void sort(int[] a) {
int N = a.length - 1;
for (int k = N / 2; k >= 1; k--) {
sink(a, k, N);
drawArray(a);
}
while (N > 1) {
exch(a, 1, N--);
sink(a, 1, N);
drawArray(a);
}
}
public static void drawArray(int[] a) {
int n = a.length;
StdDraw.setXscale(0, n * 2);
StdDraw.setYscale(0, n);
StdDraw.clear();
for (int i = 1; i < n; i++) {
double x = 1 + 2 * i;
double y = a[i] / 2;
double halfW = 1;
double halfH = a[i] / 2;
StdDraw.filledRectangle(x, y, halfW, halfH);
}
StdDraw.show();
}
public static void main(String[] args) {
StdDraw.setCanvasSize(240, 160);
StdDraw.enableDoubleBuffering();
int[] a = new int[500];
for (int i = 0; i < a.length; i++) {
a[i] = i;
}
StdRandom.shuffle(a);
a[0] = 0;
HeapSort.sort(a);
for (int x : a) {
StdOut.printf("%d ", x);
}
}
}
import edu.princeton.cs.algs4.*;
public class QuickSort {
private static int n, currentPivot, currentPi, currentPj, currentPlo, currentPhi;
private static int frames;
public static void sort(int[] a, int lo, int hi) {
if (hi <= lo) return;
int j = partition(a, lo, hi);
sort(a, lo, j - 1);
sort(a, j + 1, hi);
currentPi = currentPj = currentPlo = currentPhi = currentPivot = -1;
drawArray(a);
}
private static boolean less(int v, int w) {
return v < w;
}
private static void exch(int[] a, int i, int j) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
private static int partition(int[] a, int lo, int hi) {
int i = lo, j = hi + 1;
int pivot = a[lo];
currentPivot = pivot;
currentPlo = lo;
currentPhi = hi;
while (true) {
while (less(a[++i], pivot)) if (i == hi) break;
while (less(pivot, a[--j])) if (j == lo) break;
if (i >= j) break;
currentPi = i;
currentPj = j;
exch(a, i, j);
drawArray(a);
}
exch(a, lo, j);
return j;
}
private static void drawArray(int a[]) {
n = a.length;
StdDraw.setXscale(0, n * 2);
StdDraw.setYscale(0, n);
StdDraw.enableDoubleBuffering();
StdDraw.clear();
for (int i = 0; i < n; i++) {
double x = 1 + 2 * i;
double y = a[i] / 2;
double halfW = 1;
double halfH = a[i] / 2;
if (i >= currentPlo && i <= currentPhi) StdDraw.setPenColor(StdDraw.LIGHT_GRAY);
if (i <= currentPi && i >= currentPlo) StdDraw.setPenColor(StdDraw.BOOK_LIGHT_BLUE);
if (i >= currentPj && i <= currentPhi) StdDraw.setPenColor(StdDraw.BOOK_BLUE);
StdDraw.filledRectangle(x, y, halfW, halfH);
StdDraw.setPenColor(StdDraw.BLACK);
}
StdDraw.setPenColor(StdDraw.BLUE);
StdDraw.line(0, currentPivot, n * 2, currentPivot);
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.show();
if (frames % 5 == 0)
StdDraw.save("D:\\qsa\\" + frames / 5 + ".png");
frames++;
}
public static void main(String[] args) {
int[] a = new int[1000];
for (int i = 0; i < a.length; i++) {
a[i] = i;
}
StdRandom.shuffle(a);
frames = 0;
StdDraw.setCanvasSize(600, 300);
sort(a, 0, a.length - 1);
for (int x : a) {
StdOut.printf("%d ", x);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment