/* * @(#)InsertSortAlgorithm.java 1.0 95/06/23 Jason Harrison * * Copyright (c) 1995 University of British Columbia * * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL purposes and without * fee is hereby granted provided that this copyright notice * appears in all copies. Please refer to the file "copyright.html" * for further important copyright and licensing information. * * UBC MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. UBC SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. */ /** * An insertion sort demonstration algorithm * SortAlgorithm.java, Thu Oct 27 10:32:35 1994 * * @author Jason Harrison@cs.ubc.ca * @version 1.0, 23 Jun 1995 * * 27 Nov 2006: (bwbecker@uwaterloo.ca) Changed the overall architecture to * use model-view-controller. Re-examined algorithm to ensure that counting * was consistent across all algorithms (the quicksorts used to ignore most * of the comparisons in the partitioning, for example). * */ class InsertionSortAlgorithm extends SortAlgorithm { void sort(int a[]) { super.lowMarker = 0; for (int i = 1; i < a.length; i++) { super.hiMarker = i; int j = i; int B = a[i]; super.other++; super.compares++; // first time into the loop while ((j > 0) && (a[j - 1] > B)) { if (stopRequested) { return; } a[j] = a[j - 1]; super.activeMarker = j; super.moves++; j--; super.compares++; // go around the loop again super.updateAllViews(); } a[j] = B; super.moves++; super.updateAllViews(); } super.lowMarker = -1; super.hiMarker = -1; super.updateAllViews(); } }