1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#include <iostream>
using namespace std;
void insertionSort(int list[], int size);
void showInsertion(const int[], int);
int comparisons = 0,
swaps = 0,
passes = 0;
int main()
{
const int SIZE = 6;
int values[SIZE] = { 6,9,1,4,2,7 };
cout << "The unsorted values are: ";
showInsertion(values, SIZE);
insertionSort(values, SIZE);
cout << endl;
cout << "The sorted values are: ";
showInsertion(values, SIZE);
cout << endl;
cout << "Number of comparisons: " << comparisons << endl;
cout << "Number of swaps: " << swaps << endl;
cout << "Number of passes: " << passes << endl;
cout << endl;
system("PAUSE");
return 0;
}
void insertion(int list[ ], int maxSize)
{
int temp1, temp2;
for (int i = 1; < maxSize; i++)
{
if (list[i] < list[i - 1])
{
temp1 = list[i];
temp2 = i;
do
{
list[temp2] = list[temp2 - 1];
--temp2;
} while ((temp2 > 0) && (list[temp2 - 1] > temp1));
list[temp2] = temp1;
}
}
}
void showInsertion(const int list[], int size)
{
for (int count = 0; count < size; count++)
cout << list[count] << " ";
cout << endl;
}
|