943,712 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2644
  • C++ RSS
Aug 6th, 2004
0

counting comparisons when sorting

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
andremc is offline Offline
3 posts
since Aug 2004
Aug 6th, 2004
0

Re: counting comparisons when sorting

>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?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 6th, 2004
0

Re: counting comparisons when sorting

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
andremc is offline Offline
3 posts
since Aug 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: storing large numbers
Next Thread in C++ Forum Timeline: Data validation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC