Insertion Sort Problem

Please support our C++ advertiser: Intel Parallel Studio Home
Closed Thread

Join Date: Jul 2004
Posts: 15
Reputation: silicon is an unknown quantity at this point 
Solved Threads: 0
silicon silicon is offline Offline
Newbie Poster

Insertion Sort Problem

 
0
  #1
May 8th, 2005
Hello everyone, I just wanted to thank you in advance for all of your help.
My code is now working properly except for one thing. The insertion part feels like it takes about 5 minutes to complete. I know that this method is the slowest of the 4
I was wondering if there a problem with the way it was written
My partner wrote this insertion sort function and it does work...but runs very slowly and I am having problems adjusting
it since this code will not work on Visual C++...it will however work on Borland c++

  1.  
  2.  
  3. #include <iostream.h>
  4. #include <conio.h>
  5. #include <time.h>
  6. #include <limits.h>
  7. #include <stdlib.h>
  8. #include <fstream.h>
  9. #include <math.h>
  10.  
  11. //Function-fills the array with random integers
  12. void getRandomIntegers (long [], long);
  13.  
  14. //Function-will swap two long integer numbers
  15. void Swap (long &, long &);
  16.  
  17. //Function-works with HeapSort to rearrange and adjust the heap
  18. void Heapify (long [], long, long);
  19.  
  20. //Function-will be used with QuickSort to partition array
  21. long Partition (long [], long, long);
  22.  
  23. //Function-sorts the array using the QuickSort method
  24. void QuickSort (long [], long, long);
  25.  
  26. //Function-sorts the array using the MergeSort method
  27. void MergeSort (long [], long, long);
  28.  
  29. //Function will be used with MergeSort to merge array back together.
  30. void Merge (long [], long, long, long);
  31.  
  32. //Function-sorts the array using the HeapSort method
  33. void HeapSort (long [], long, long);
  34.  
  35. //Function-sorts the array using the Insertion method
  36. void Insertion (long [], long, long);
  37.  
  38.  
  39. //Global variable constant
  40. //This will be used by the various sorting functions
  41. long const MAX_ARRAY_SIZE = 260000;
  42.  
  43.  
  44. ofstream outf("c:\\Output.txt",ios::app);//This prints the results to the c:drive
  45.  
  46.  
  47. int main()
  48. {
  49.  
  50. //This function will use a for loop to call the different sort functions,
  51. //with varing array sizes.
  52.  
  53.  
  54.  
  55.  
  56. long random_array[MAX_ARRAY_SIZE];
  57.  
  58. long array_sizes[] = {100000, 110000, 130000, 150000, 170000, 190000, 200000, 210000, 230000, 250000, 255000, 260000};
  59.  
  60. long num_elements;
  61.  
  62. clock_t start_time, end_time;
  63.  
  64. srand( time( NULL ));// This generates the random numbers
  65.  
  66. cout << "Sort Type\t\t# of Elements\t\t\tTime(s)\n";
  67.  
  68. cout << "--------\t\t-------------------\t\t-------\n";
  69.  
  70. outf << "Sort Type\t\t# of Elements\t\t\tTime(s)\n";
  71.  
  72. outf << "--------\t\t-------------------\t\t-------\n";
  73.  
  74. for(long i = 0; i < 20; i++)
  75. {
  76. num_elements = array_sizes[i];
  77.  
  78. getRandomIntegers(random_array, num_elements );
  79.  
  80. start_time = clock();
  81.  
  82. QuickSort(random_array, 0, num_elements );
  83.  
  84. end_time = clock();
  85.  
  86. cout << "Quick \t\t\t"<< num_elements << "\t\t\t" << (end_time - start_time)/(double)CLOCKS_PER_SEC << endl;
  87.  
  88. outf<< "Quick \t\t\t"<< num_elements << "\t\t\t" << (end_time - start_time)/(double)CLOCKS_PER_SEC << endl;
  89.  
  90. getRandomIntegers( random_array, num_elements );
  91.  
  92. start_time = clock();
  93.  
  94. MergeSort( random_array, 0, num_elements );
  95.  
  96. end_time = clock();
  97.  
  98. cout << "Merge \t\t\t" << num_elements << "\t\t\t" << (end_time - start_time)/(double)CLOCKS_PER_SEC << endl;
  99.  
  100. outf << "Merge \t\t\t" << num_elements << "\t\t\t" << (end_time - start_time)/(double)CLOCKS_PER_SEC << endl;
  101.  
  102. getRandomIntegers( random_array, num_elements );
  103.  
  104. start_time = clock();
  105.  
  106. HeapSort( random_array, 0, num_elements );
  107.  
  108. end_time = clock();
  109.  
  110. cout << "Heap \t\t\t"<< num_elements << "\t\t\t" << (end_time - start_time)/(double)CLOCKS_PER_SEC << endl;
  111.  
  112. outf << "Heap \t\t\t"<< num_elements << "\t\t\t" << (end_time - start_time)/(double)CLOCKS_PER_SEC << endl;
  113.  
  114. getRandomIntegers( random_array, num_elements );
  115.  
  116. start_time = clock();
  117.  
  118. Insertion( random_array, 0, num_elements );
  119.  
  120. end_time = clock();
  121.  
  122. cout << "Insertion \t\t\t"<< num_elements << "\t\t\t" << (end_time - start_time)/(double)CLOCKS_PER_SEC<< endl << endl;
  123.  
  124. outf << "Insertion \t\t\t"<< num_elements << "\t\t\t" << (end_time - start_time)/(double)CLOCKS_PER_SEC << endl<<endl;
  125.  
  126. }
  127. getch();
  128. return 0;
  129.  
  130. }
  131.  
  132. /******************************************************************************/
  133.  
  134. //This function is used by HeapSort to adjust the heap or "rearrange a heap to maintain the heap property"
  135. void Heapify( long array[], long item, long num )
  136. {
  137.  
  138. while( 2 * item <= num )
  139. {
  140. long j = 2 * item;
  141.  
  142. if( j < num && array[j] < array[j + 1] ) j++;
  143.  
  144. if( !( array[item], array[j]))
  145.  
  146. break;
  147.  
  148. Swap( array[item], array[j] ); item = j;
  149.  
  150. }
  151.  
  152. }
  153.  
  154. /******************************************************************************/
  155.  
  156. void getRandomIntegers ( long arr[], long b )
  157. {
  158. //Fills an array(the first paramater) with
  159.  
  160. //a specified amount( the second parameter ) of random integers.
  161.  
  162. //The range of the random numbers will be from 0 to RAND_MAX.
  163.  
  164.  
  165.  
  166. for( long i = 0; i < b; i++ )
  167.  
  168. arr[i] = rand();
  169.  
  170. }
  171.  
  172. /******************************************************************************/
  173. //Swap function, accepts two long integer values and swaps them.
  174.  
  175. void Swap( long &first, long &second )
  176. {
  177.  
  178. long temp;
  179.  
  180. temp = first;
  181.  
  182. first = second;
  183.  
  184. second = temp;
  185.  
  186. }
  187.  
  188. /******************************************************************************/
  189. //Quick Sort Function
  190.  
  191. void QuickSort( long array[], long left, long right)
  192. {
  193.  
  194. if( right <= left )return;
  195.  
  196. long i = Partition( array, left, right );
  197.  
  198. QuickSort( array, left, i - 1);
  199.  
  200. QuickSort( array, i + 1, right );
  201.  
  202. }
  203.  
  204. /******************************************************************************/
  205. //This function partitions the array
  206.  
  207.  
  208. long Partition( long array[], long left, long right )
  209. {
  210.  
  211.  
  212. long i = left - 1, r = right;
  213.  
  214. long y = array[right];
  215.  
  216. while( 1 )
  217. {
  218. while( array[++i] < y );
  219.  
  220. while( y < array[--r] )if( r == 1 )
  221.  
  222. break;
  223.  
  224. if( i >= r )
  225.  
  226. break;
  227.  
  228. Swap( array[i], array[r] );
  229.  
  230. }
  231.  
  232. Swap( array[i], array[right] );
  233.  
  234. return i;
  235.  
  236. }
  237.  
  238. /******************************************************************************/
  239. //MergeSort function
  240.  
  241. void MergeSort( long array[], long left, long right )
  242. {
  243.  
  244.  
  245. if( right <= left )
  246.  
  247. return;
  248.  
  249. long middle = ( right + left )/2;
  250.  
  251. MergeSort( array, left, middle );
  252.  
  253. MergeSort( array, middle + 1, right );
  254.  
  255. Merge( array, left, middle, right );
  256.  
  257. }
  258.  
  259. /******************************************************************************/
  260. //Merges arrays back together after they are split
  261.  
  262. void Merge( long array[], long left, long middle, long right )
  263. {
  264.  
  265.  
  266. long i, j;
  267.  
  268. long static temp[MAX_ARRAY_SIZE];
  269.  
  270. for( i = middle + 1; i > left; i-- )
  271.  
  272. temp[i - 1] = array[i - 1];
  273.  
  274. for( j = middle; j < right; j++ )
  275.  
  276. temp[right + middle - j] = array[j + 1];
  277.  
  278. for( long k = left; k <= right; k++ )
  279.  
  280. if( temp[j] < temp[i] )
  281.  
  282. array[k] = temp[j--];
  283.  
  284. else
  285.  
  286. array[k] = temp[i++];
  287.  
  288. }
  289.  
  290. /******************************************************************************/
  291.  
  292.  
  293. void HeapSort( long array[], long left, long right )
  294. {
  295.  
  296. long k, num = right - left + 1;
  297.  
  298. long *ip = array + left - 1;
  299.  
  300. for( k = num/2; k >= 1; k-- )
  301.  
  302. Heapify( ip, k, num );
  303.  
  304. while ( num > 1 )
  305. {
  306.  
  307. Swap( ip[1], ip[num] );
  308.  
  309. Heapify( ip, 1, --num );
  310.  
  311. }
  312.  
  313.  
  314.  
  315. }
  316.  
  317. /******************************************************************************/
  318.  
  319. void Insertion( long array[], long left, long right )
  320. {
  321.  
  322.  
  323. long i;
  324.  
  325. for( i = right; i > left; i-- )
  326.  
  327. Swap( array[i - 1], array[i] );
  328.  
  329. for( i = left + 2; i <= right; i++ )
  330. {
  331.  
  332. long j = i, item = array[i];
  333.  
  334. while( item < array[j - 1] )
  335.  
  336. {
  337.  
  338. array[j] = array[j - 1];
  339. j--;
  340.  
  341. }
  342.  
  343. array[j] = item;
  344.  
  345. }
  346.  
  347. }
Quick reply to this message  
Join Date: Sep 2004
Posts: 7,571
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 708
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Insertion Sort Problem

 
0
  #2
May 8th, 2005
>I was wondering if there a problem with the way it was written
Aside from being copied verbatim from a book that is well known for its awful code (poorly written in C via Pascal), there's nothing wrong with the code except for the first loop should do a conditional swap, not an unconditional swap.

>but runs very slowly
Wow, what a shocker. You're performing multiple quadratic sorts on arrays that have over 100000 randomly permuted items. No amount of tweaking with fix that.

>since this code will not work on Visual C++
Your array size is probably overflowing the stack. 260000 is somewhat large for a static array on some implementations. Using dynamic allocation will probably fix it:
  1. long *random_array = new long[MAX_ARRAY_SIZE];
I'm here to prove you wrong.
Quick reply to this message  
Join Date: May 2005
Posts: 7
Reputation: bsrivastava is an unknown quantity at this point 
Solved Threads: 1
bsrivastava bsrivastava is offline Offline
Newbie Poster

Re: Insertion Sort Problem

 
0
  #3
May 9th, 2005
Hello use the following insertion sort routine a template based version.
  1. //This routine does the insertion sort
  2. //This sorts array of object of template class T (T arr[]) & no. of elements in the array (int n)
  3. template <class T>
  4. void insertionSort(T arr[], int n)
  5. {
  6. for (int i = 1; i < n; i++)
  7. {
  8. //start from 2nd node onwards
  9. T val = arr[i];
  10.  
  11. for (int j = i - 1; j >= 0; j--)
  12. {
  13. if (arr[j] > val)
  14. {
  15. arr[j+1] = arr[j];
  16. }
  17. else
  18. break;
  19. }
  20. arr[j+1] = val;
  21. }
  22.  
  23. }
Code tags added. -Narue

* Above is compiled and running on VC6.0 & i dont think will give hitch for porting on unix/linux/.NET.
*Doesn't have runtime issues..use it & give feedback
--------------------------------------------------------------------------



Originally Posted by Narue
>I was wondering if there a problem with the way it was written
Aside from being copied verbatim from a book that is well known for its awful code (poorly written in C via Pascal), there's nothing wrong with the code except for the first loop should do a conditional swap, not an unconditional swap.

>but runs very slowly
Wow, what a shocker. You're performing multiple quadratic sorts on arrays that have over 100000 randomly permuted items. No amount of tweaking with fix that.

>since this code will not work on Visual C++
Your array size is probably overflowing the stack. 260000 is somewhat large for a static array on some implementations. Using dynamic allocation will probably fix it:
  1. long *random_array = new long[MAX_ARRAY_SIZE];
Quick reply to this message  
Join Date: Sep 2004
Posts: 7,571
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 708
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Insertion Sort Problem

 
0
  #4
May 9th, 2005
>Hello use the following insertion sort routine a template based version.
I don't see how your solution would have any effect on the OP's problem.

>i dont think will give hitch for porting on unix/linux/.NET
It fails to compile on any conforming compiler.

>Doesn't have runtime issues
The algorithm may not have run-time issues when you tested it, but the sort function is not what the problem was.
I'm here to prove you wrong.
Quick reply to this message  
Join Date: Oct 2006
Posts: 3
Reputation: willzyba is an unknown quantity at this point 
Solved Threads: 0
willzyba willzyba is offline Offline
Newbie Poster

Re: Insertion Sort Problem

 
0
  #5
Oct 31st, 2006
The best solutions I've seen lately for a variety of sorting methods are at: http://www.codecogs.com/pages/catgen...ory=array/sort
Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Insertion Sort Problem

 
0
  #6
Nov 1st, 2006
Tsk tsk.. thread closed.
I don't accept change; I don't deserve to live.
Quick reply to this message  
Closed Thread

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC