943,845 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 533
  • C RSS
Mar 29th, 2009
0

Sorting Efficiency Question

Expand Post »
I recently learned a little bit about sorting algorithm efficiency. I looked at the merge sort and saw how it could be implemented using a scratch array to store the result until it was finished. It then talked about in place algorithms but didn't offer one for a merge sort, so I tried to build my own. I came up with one:
  1. /* this assumes sub1 and sub2 are start points of two sub arrays of one contiguous array starting at sub1 with size elements */
  2. int* merge ( int *sub1, int *sub2, int size )
  3. {
  4. int *final= sub1, count= 0;
  5.  
  6. while ( ++count < size )
  7. {
  8. if ( *sub1 > *sub2 )
  9. {
  10. /* if arr2 is less, move it to next slot in final */
  11. int *loc= sub2++;
  12. while ( loc-- > arr1 )
  13. {
  14. loc[0]^= loc[1];
  15. loc[1]^= loc[0];
  16. loc[0]^= loc[1];
  17. }
  18. }
  19. /* move the first pointer */
  20. arr1++;
  21. }
  22.  
  23. return final;
  24. }
  25.  
  26. int* merge_sort ( int *arr, int size )
  27. {
  28. if ( size == 1 )
  29. return arr;
  30. else
  31. return merge( merge_sort( arr, size/2 ),
  32. merge_sort( arr+size/2, size/2+size%2 ),
  33. size );
  34. }

I was wondering if this defeats the program efficiency in order to obtain the in place functionality when it has to iterate in the sort function to move a lesser value up in the final. How would this affect the runtime? Thanks in advance for anything anyone may offer.
Similar Threads
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
c++noobie is offline Offline
71 posts
since Feb 2008
Mar 29th, 2009
0

Re: Sorting Efficiency Question

Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Mar 29th, 2009
0

Re: Sorting Efficiency Question

Thank you, but I had seen something similar to that in what I was reading. What I read was talking about in place algorithms (ones that don't consume extra memory, e.g. no second array for merge sort) though and I was wondering if my in place solution provided above sacrificed it's execution speed by becoming in place like it is. Perhaps I should clarify what I was asking:

1) Does the in place solution provided above lose it's execution speed by saving memory like it does? (specifically lines 8 to 18 when it iterates down the array to move a value)

2) Is it even still a merge sort after the modifications I made? (maybe it's still a logical merge sort but no longer a physical merge sort?)

If I need to clarify anything in the code or anything else please ask, and thanks to anyone who may have an answer.
Last edited by c++noobie; Mar 29th, 2009 at 9:01 pm.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
c++noobie is offline Offline
71 posts
since Feb 2008
Mar 30th, 2009
0

Re: Sorting Efficiency Question

I'm sorry I quickly brushed over the example on my way out the door. Thank you for your response. Now that I have time to go back and look at it, it is a little more helpful. One more question I would like to know though, would my code provided even be a merge sort at this point or is it something else?
Last edited by c++noobie; Mar 30th, 2009 at 2:41 am.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
c++noobie is offline Offline
71 posts
since Feb 2008
Mar 30th, 2009
0

Re: Sorting Efficiency Question

It's wonderful but just before talking about this code run-time speed and other characteristics correct obvious errors in it. Now you can't compile this code: arr1 is not declared in the merge function (if it's a global variable better don't display this "improved" sort function)...

When you compile this improved merge sort code you can test and profile it - and you will get all answers to your questions...

PS. As usually, xor based solution is much more expensive than a simple swap...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Mar 30th, 2009
0

Re: Sorting Efficiency Question

Thanks to both of you for your input. As for the error, it started off as arr1 and arr2 instead of sub1 and sub2 but I changed them to make it more apparent that they are sub arrays of a larger one, not complete separate arrays. As for my question, after looking further into it, it is less efficient to save memory in that case. It winds up having run time similar to a selection or insertion sort. Thanks to both of you.
Last edited by c++noobie; Mar 30th, 2009 at 6:53 pm.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
c++noobie is offline Offline
71 posts
since Feb 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: create a variable with a given name
Next Thread in C Forum Timeline: Scanf with structure





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


Follow us on Twitter


© 2011 DaniWeb® LLC