• Post Reply Bookmark Topic Watch Topic
  • New Topic

Merge Sort  RSS feed

 
Aron Silvester
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


QUESTION: I found the merge sort algorithm located above online. Since I want to know how many swaps this code will do I'll just put a counter. I created a mergeSortCompareCount which counts the number of compares and another variable mergeSortSwapCount which counts the number of swaps. These variables are bolded so you can spot them easier. I've input them in the code below. Are they in the right place???





 
Aron Silvester
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just realized that we don't do swaps in merge sort, so I guess we only compare.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You increment the comparison count in the wrong place, because it only increments when the comparison evaluates in favor of the left array.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!