81 lines
1.9 KiB
Java
81 lines
1.9 KiB
Java
/**
|
|
* <PRE>
|
|
* InsertionSort.java
|
|
*
|
|
* Revisions: 1.0 Nov. 04, 2002
|
|
* Created the InsertionSort class
|
|
* 1.1 Nov. 07, 2002
|
|
* Compiled, Commented, Finished
|
|
*
|
|
* </PRE>
|
|
*
|
|
* Collaboration Statement:
|
|
* I worked on the homework assignment alone, using only
|
|
* course materials.
|
|
*
|
|
* Created with JCreatorLE, some indents are off when viewed through notepad
|
|
* or EMACS
|
|
*
|
|
* @author <A HREF="mailto:gtg184g@mail.gatech.edu">Jose Manuel Caban</A>
|
|
* @version Version 1.1, Nov. 07, 2002
|
|
*/
|
|
|
|
public class InsertionSort extends AbstractSort{
|
|
|
|
////////////////
|
|
//Constructors//
|
|
////////////////
|
|
|
|
/**
|
|
*Constructor for InsertionSort
|
|
*@param arrayWrap, the array to be sorted
|
|
*/
|
|
public InsertionSort(ArrayWrapper arrayWrap){
|
|
super(arrayWrap);
|
|
}
|
|
|
|
///////////
|
|
//Methods//
|
|
///////////
|
|
|
|
/**
|
|
*Sort the Array
|
|
*/
|
|
public void doSort(){
|
|
for(int i=1; i<arrayWrap.length(); i++){
|
|
Comparable cTemp = arrayWrap.get(i);
|
|
int z=i;
|
|
|
|
while(z>0 && cTemp.compareTo(arrayWrap.get(z-1))<0){
|
|
arrayWrap.swap(z,z-1);
|
|
z--;
|
|
}
|
|
arrayWrap.set(z,cTemp);
|
|
}
|
|
}//end doSort()
|
|
|
|
|
|
/**
|
|
* Debugging main for class InsertionSort.
|
|
* This method will rigorously test my code.
|
|
*
|
|
* <br><br>
|
|
* @param args a String array of command line arguments.
|
|
*/
|
|
public static void main(String[] args) {
|
|
ArrayWrapper aw = new ArrayWrapper(ArrayWrapper.getRandomIntegerArray(40));
|
|
InsertionSort is = new InsertionSort(aw);
|
|
|
|
for(int i=0;i<aw.length();i++){
|
|
System.out.println(aw.get(i));
|
|
}
|
|
is.doSort();
|
|
System.out.println("\n/****Testing Now****/\n");
|
|
for(int i=0;i<aw.length();i++){
|
|
System.out.println(aw.get(i));
|
|
}
|
|
|
|
}// end of main(String[] args)
|
|
|
|
}// end of class InsertionSort
|