QB / QB64 Discussion Forum      Other Subforums, Links and Downloads
 

 Return to Index  

Extemely Simple and Intuitive Sort

January 31 2008 at 3:52 PM
qbguy  (no login)


Response to Shell Sort, Bubble Sort, Sorting in general

 

Look at how simple and elegant this sort is:

* If the value at the end is smaller than the value at the start, swap them.
* If there are 3 or more elements in the current list subset, then:
    o Sort the initial 2/3 of the list (recursively)
o Sort the final 2/3 of the list (recursively)
o Sort the initial 2/3 of the list again (recursively)
o else: exit the procedure


Very simple, can be accomplished with very little code.

SUB SORT(A())
  CALL SORT(A(), LBOUND(A), UBOUND(A))
END SUB

SUB SORTH(A(), I, J)
  IF A(I)>A(J) SWAP A(I), A(J)
  IF I+1=J THEN EXIT SUB
  K=INT((J-I+1)/3)
  CALL SORTH(A(), I, J-K)
  CALL SORTH(A(), I+K, J)
  CALL SORTH(A, I, J-K)
END SUB

 

 
 Respond to this message   
 Copyright © 1999-2014 Network54. All rights reserved.   Terms of Use   Privacy Statement  

Newbies usually go to www.qbasic.com and click on The QBasic Forum
Forum regulars have their own ways, which include The QBasic Community Forums