Created
June 16, 2012 13:19
-
-
Save idkravitz/2941318 to your computer and use it in GitHub Desktop.
Hello Qsort on ActionScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package | |
| { | |
| /** | |
| * ... | |
| * @author idkravitz | |
| */ | |
| public class Sort | |
| { | |
| public static function qsort(array:Array):Array | |
| { | |
| _qsort(array, 0, array.length); | |
| return array; | |
| } | |
| private static function _qsort(array:Array, left:uint, right:uint):void | |
| { | |
| if (left >= (right-1)) return; | |
| var pivot:uint = (left + right) / 2; | |
| var swap:Number; | |
| swap = array[left]; | |
| array[left] = array[pivot]; | |
| array[pivot] = swap; | |
| pivot = left; | |
| for (var i:uint = left; i < right; i++) | |
| { | |
| if (array[i] < array[pivot]) | |
| { | |
| swap = array[pivot]; | |
| array[pivot++] = array[i]; | |
| array[i] = array[pivot]; | |
| array[pivot] = swap; | |
| } | |
| } | |
| _qsort(array, left, pivot); | |
| _qsort(array, pivot+1, right); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment