This section describes the quick sorting method. You cannot always use the Bubble sorting method. In addition, some programming languages also have their own sorting methods, such as with sort and in array. But by the way, in as3, do not use Recursive Algorithms easily. You can create a test by yourself. When you start from 1 + 2 + 3 ...... when the number is increased to 50 (using recursive algorithms), the program will be stuck. recursion in as3 is feasible, but the number of times must not be too large. The reason why I wrote this article in C # Is that the quick algorithm uses recursion. In as3, do not use quick sorting, because it is hard to guarantee that the number of elements in your set does not exceed the limit of as3 recursion. Then, use sort in array, in vector, I wrote "Sort sorting of vectors in as3". You can check it.
First, let's talk about ideas.
Compared with the overall sorting of Bubble sorting, the quick sorting method uses branch sorting.
First, he has a benchmark to sort out the elements on the left and right of the benchmark (when sorted in descending order, the elements on the left of the marker> = marker, elements on the right <= povit, and vice versa), and then select sort again for sorting (recursion)
void QuickAscSort( ref vector<int> v, int left, int right){ if(left < right){ int key = v[left]; int low = left; int high = right; while(low < high){ while(low < high && v[high] > key){ high-=1; } v[low] = v[high]; while(low < high && v[low] < key){ low+=1; } v[high] = v[low]; } v[low] = key; QuickAscSort(v,left,low-1); QuickAscSort(v,low+1,right); }}
The above is a sort in ascending order.
Below is a descending Method
void QuickDesSort( ref vector<int> v, int left, int right){ if(left < right){ int key = v[left]; int low = left; int high = right; while(low < high){ while(low < high && v[high] < key){ high-=1; } v[low] = v[high]; while(low < high && v[low] > key){ low+=1; } v[high] = v[low]; } v[low] = key; QuickDesSort(v,left,low-1); QuickDesSort(v,low+1,right); }}
For example: [,] sorting of
Quickascsort (Ref A, 0, A. lenght-1); this is an ascending order, and vice versa.
Add a comprehensive method:
void QuickSort( ref vector<int> v, int left, int right , bool isAsc ){ if(left < right){ int key = v[left]; int low = left; int high = right; while(low < high){ if( !isAsc ){ while(low < high && v[high] < key){ high-=1; } v[low] = v[high]; while(low < high && v[low] > key){ low+=1; } v[high] = v[low]; }else{ while(low < high && v[high] > key){ high-=1; } v[low] = v[high]; while(low < high && v[low] < key){ low+=1; } v[high] = v[low]; } } v[low] = key; QuickDesSort(v,left,low-1 , isAsc); QuickDesSort(v,low+1,right , isAsc); }}
Use Quick ORT (Ref A, 0, A. lenght-1, true) in ascending order );
Descending quicksort (Ref A, 0, A. lenght-1, false );
This article is from the "better_power_wisdom" blog, please be sure to keep this source http://aonaufly.blog.51cto.com/3554853/1546245
Quick Sorting Algorithm