I'm trying to figure out the correct placement of "copies" and "comparison" integers I have declared. Will someone please help?
After sorting 100,000 floats from 0-99 I have come up with the following output:
The number of comparisions made using insertion sort: 4
The number copies made using insertion sort:9999
This is obviously incorrect, can someone show me/help me understand the correct location?
|
Code:
|
public void insertionSort() {
int comps = 0;
int copies = 0;
int in, out;
for (out = 1; out < nElems; out++) // out is dividing line
{
float temp = theArray[out]; // remove marked item
in = out; // start shifts at out
while (in > 0 && theArray[in - 1] >= temp) // until one is smaller,
{
comps++;
theArray[in] = theArray[in - 1]; // shift item to right
--in; // go left one position
}
theArray[in] = temp; // insert marked item
copies++;
} // end for |