hi to all!!!
I need some help please to figure it out to count number of swaps and comparison in this sort. any input will highly appriciated.
#include<iostream>
#include<iomanip>
#include<ctime>
#include<cstdlib>
using namespace std;
// function prototype
void Insertion_Sort(int arry[]);
int main()
{
// variable declarations
int arry[12];
srand (time(0)); // Seed the random number generator.
// place random numbers into the array
// Output the original array values
for( int i = 0; i < 12; i++ )
{
arry[i] =rand()%100;
cout << setw(4) << arry[i];
}
// creates a line seperator
cout <<"\n--------------------------------------------------------\n";
Insertion_Sort(arry);
// display sorted values
cout<<"The sorted array"<<endl;
// Output the current sorted array values
for ( int i = 0; i < 12; i++ )
{
cout << setw(4) <<arry[i];
}
cout<<endl;
return 0;
}
// end of main
//insertionSort Function
void Insertion_Sort(int arry[])
{
int b=0;
int a=0;
//iterate through entire list
for(int i = 1; i<12; i++)
{
a = arry[i];
b = i - 1;
while(b >=0 && arry[b]>a)
{
arry[b+1]=arry[b];
b = b - 1;
}
arry[b+1]=a;
}
}