SteppedInsertionSort

SteppedInsertionSort - while (position > 0...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
//******************************************************************** // SteppedInsertionSort.java Author: Lewis and Loftus // // Solution to Programming Project 6.15 // // Performs an insertion sort one step at a time. //******************************************************************** import java.awt.*; public class SteppedInsertionSort { private int[] numbers; private int index; //----------------------------------------------------------------- // Creates a new sort object to sort the values in the parameter // unsortedArray //----------------------------------------------------------------- public SteppedInsertionSort(int[] unsortedArray) { numbers = unsortedArray; index = 1; } //----------------------------------------------------------------- // Performs one step of the insertion sort algorithm. //----------------------------------------------------------------- public void nextStep () { int key = numbers[index]; int position = index; // shift larger values to the right
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: while (position > 0 && numbers[position-1] > key) { numbers[position] = numbers[position-1]; position--; } numbers[position] = key; index++; } //-----------------------------------------------------------------// Returns true if all integers in array are sorted //-----------------------------------------------------------------public boolean sortFinished() { return (index >= numbers.length); } //-----------------------------------------------------------------// Draws a graphical representation of the sort. //-----------------------------------------------------------------public void draw(Graphics page, int width, int height) { if (sortFinished()) page.drawString("Finished Sort", 10, height - 10); int step = width / (numbers.length + 1); for (int i=0; i<numbers.length; i++) { page.setColor(Color.blue); page.drawLine((i+1)*step, 0, (i+1)*step, numbers[i]); } } }...
View Full Document

Page1 / 2

SteppedInsertionSort - while (position > 0...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online