counting comparisons when sorting

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2004
Posts: 3
Reputation: andremc is an unknown quantity at this point 
Solved Threads: 0
andremc andremc is offline Offline
Newbie Poster

counting comparisons when sorting

 
0
  #1
Aug 6th, 2004
i'm trying to keep track of how many comparisons and swaps my sorting algorithms are making. i know it's probably a variable referencing problem, but i can't seem to get it to work right. it's fine when i run only one of the sorts, but if i run both, moves comes out to 0. how can i fix this? here's a sample.

  1. InsertionSort(myarray1, size1);
  2. BubbleSort(myarray1, size1);
  3.  
  4.  
  5. void InsertionSort(Item a[], int Numitems)
  6. {
  7. int InsertItem, InnerCount, TempValue;
  8. int swaps = 0; int comparisons = 0;
  9.  
  10. //move through list left to right
  11. for (InsertItem = 1; InsertItem < Numitems; InsertItem++)
  12. {
  13. //copy that value to a temp value
  14. TempValue = a[InsertItem];
  15.  
  16. //move from that item to the left
  17. for (InnerCount = InsertItem - 1; InnerCount >= 0 &&
  18. TempValue < a[InnerCount]; InnerCount--)
  19. {
  20. comparisons += 1;
  21. //move values to right
  22. a[InnerCount+1] = a[InnerCount];
  23.  
  24. }
  25. //put insert value into correct spot
  26. a[InnerCount+1] = TempValue;
  27. swaps +=1;
  28. }
  29. for(int i=0; i<Numitems; i++)
  30. cout<<a[i]<<" " <<endl;
  31. cout<<"swaps = "<<swaps<<endl;
  32. cout<<"comparisons = "<<comparisons;
  33. }
  34.  
  35.  
  36. void BubbleSort(Item a[], int Numitems)
  37. {
  38. int NumPasses;
  39. int InnerCount;
  40. Item TempValue;
  41. int swaps = 0; int comparisons = 0;
  42.  
  43. //start outer loop
  44. for (NumPasses=0; NumPasses < Numitems-1; NumPasses++)
  45. {
  46. //start inner loop
  47. for (InnerCount=0; InnerCount < Numitems-1; InnerCount++)
  48. {
  49. //compare adjacent values
  50. if (a[InnerCount] > a[InnerCount+1])
  51. {
  52. //swap values
  53. TempValue = a[InnerCount];
  54. a[InnerCount] = a[InnerCount+1];
  55. a[InnerCount+1] = TempValue;
  56. swaps+=1;
  57. }
  58. comparisons += 1;
  59. }
  60. }
  61. for(int i=0; i<Numitems; i++)
  62. cout<<a[i]<<" " ;
  63. cout<<"swaps = "<<swaps<<endl;
  64. cout<<"comparisons = "<<comparisons<<endl;
  65. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: counting comparisons when sorting

 
0
  #2
Aug 6th, 2004
>it's fine when i run only one of the sorts, but if i run both, moves comes out to 0.

If you've sorted the list, just how many swaps do you think you'd need to sort the list?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 3
Reputation: andremc is an unknown quantity at this point 
Solved Threads: 0
andremc andremc is offline Offline
Newbie Poster

Re: counting comparisons when sorting

 
0
  #3
Aug 6th, 2004
i'm not sure if i understand your question. of course after it's sorted there won't be any swaps needed. the part that i left out of the code is where i fill the array with random numbers (sorry). so, naturally, it would depend on the numbers & how mixed up in the array they are.


[edit] oh ok, i see what youre saying!!, since i've done the sort once, it's already sorted. duh! thanks.
Reply With Quote Quick reply to this message  
Reply

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