On a whim I wrote a small program that shows a step-by-step insertion sort routine running on a randomized array. Here is the code for that..
int[] unSorted = new int[10]; int iter = 1; int holePos = 0; int val = 0; PGraphics pg; void setup(){ size(500,400); pg = createGraphics(500,400); background(255); frameRate(1); initialize(); debug(); } void initialize() { for(int x=0;x<10;x++) { unSorted[x] = int(random(10)); } } void sort() { print("Iter=" + iter + "\n"); val = unSorted[iter]; holePos = iter; while(holePos > 0 && val < unSorted[holePos - 1]) { unSorted[holePos] = unSorted[holePos - 1]; holePos = holePos -1; } unSorted[holePos] = val; show(); debug(); } void debug() { for(int x=0;x<10;x++) { print("[" + unSorted[x] + "]"); } print("\n"); } void show(){ pg.clear(); pg.beginDraw(); pg.stroke(100,200,30); float yPos = 500/2; float xPos = 400/2 - (0.5 * 160); float offset = 0; for(int x=0;x<10;x++){ pg.fill(0,0,0); pg.rect(xPos + 20 * (x + 1) + offset,yPos - (10 * float(unSorted[x])),20, 10 * float(unSorted[x])); pg.endDraw(); image(pg,0,0); offset = offset + 4; } } void draw() { if(iter > 9) { noLoop(); } else { background(255); sort(); iter = iter + 1; } }