insertion sort

Archived from the original Sajha.com — preserved as posted, replies can no longer be added here.
Start a New Discussion
Archived Post

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; } }  

nepali8 · Nov 4, 2012 1:51 PM · 2,131 views

Replies
No replies yet.

This conversation is preserved exactly as it was on the original Sajha.com and can't accept new replies.

Start a New Discussion

You might be interested in...

Recent Classifieds View all
Upcoming Events View all
Service Providers View all