Sorting Efficiency Question

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2008
Posts: 67
Reputation: c++noobie is an unknown quantity at this point 
Solved Threads: 1
c++noobie c++noobie is offline Offline
Junior Poster in Training

Sorting Efficiency Question

 
0
  #1
Mar 29th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Sorting Efficiency Question

 
0
  #2
Mar 29th, 2009
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 67
Reputation: c++noobie is an unknown quantity at this point 
Solved Threads: 1
c++noobie c++noobie is offline Offline
Junior Poster in Training

Re: Sorting Efficiency Question

 
0
  #3
Mar 29th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 67
Reputation: c++noobie is an unknown quantity at this point 
Solved Threads: 1
c++noobie c++noobie is offline Offline
Junior Poster in Training

Re: Sorting Efficiency Question

 
0
  #4
Mar 30th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Sorting Efficiency Question

 
0
  #5
Mar 30th, 2009
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...
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 67
Reputation: c++noobie is an unknown quantity at this point 
Solved Threads: 1
c++noobie c++noobie is offline Offline
Junior Poster in Training

Re: Sorting Efficiency Question

 
0
  #6
Mar 30th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum


Views: 364 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC