The quick sort was developed by C.a.r.hoare (1962), which selects an element called the main element (pivot) in the array, divides the array into two parts, and all the elements in the first part are less than or equal to the primary, and all the elements of the second part are larger than the primary. Apply fast sorting algorithm recursively to the first part, then apply fast sorting algorithm recursively to the second part.Package ss.sort;/** * Quick Sort * @author zhangss 2016-5-3 11:42:32 *
caching. The default value is "0", indicating that the disk and shared memory are used for caching.
Eaccelerator. compress = "1"Allows or disables content caching. The default value is "1", which allows compression.
Eaccelerator. compress_level = "9"Specifies the compression level of the content cache. The default value is 9, which is the highest level.
Eaccelerator. keys = "disk_only"Eaccelerator. sessions = "disk_only"Eaccelerator. content = "disk_only"Set the location where the content cache
) { intPA =partition (R, Low, high); QuickSort (R, Low, PA-1); QuickSort (R, PA+ 1, high); }}//divides the sequence into two sub-sequences and returns the position of the pivot elementPrivate intPartitionint[] R,intLowintHigh ) { intPivot = R[low];//use R[low] as the pivot element while(Low //alternating inward scanning from both ends while(Low Compare (R[high], pivot)
to algorithms ":
The key to the fast sorting algorithm is the PARTITION process, which rearranges A [p. r] in place:
PARTITION (A, p, r)1 x region A [r] // The last element, with A [r] as the primary element2 I then p-13 for j branch p to r-1 // note that j points from p to a R-1, not r.4 do if A [j] ≤ x5 then I have I + 16 exchange A [I] 7 exchange A [I + 1] 8 return I + 1
Then, recursively sort the entire array:
QUICKSORT (A, p, r)1 if p 2 then q P
Five common sorting methods in java and java
Package com. chenyang. www. demo;/*** Created by red devils on.*/Public class Scortdemo {/*** Bubble sort * * * * ** @ Param numbers* Integer array to be sorted*/Public static void bubbleSort (int [] numbers ){Int temp; // records the temporary median value.Int size = numbers. length; // array sizeFor (int I = 0; I For (int j = I + 1; j If (numbers [I] ;;;// Fast sorting uses the divide and conquer method to divide a sequence into two subsequences./**
;MaxHeapify (A, 0, heap_size );}}// ------------ Quick sorting -----------------------------Int Partition (std: vector Int x = A [r];Int I = p-1;For (int j = p; j If (A [j] ++ I;Std: swap (A [I], A [j]);}}Std: swap (A [I + 1], A [r]);Return I + 1;};Void QuickSort (std: vector If (p Int q = Partition (A, p, r );QuickSort (A, p, q-1 );QuickSort (A, q + 1, r );}};
#
keywords in the left sub-range less than or equal to the datum record (may be remembered as pivot) Keyword Pivot.key, all recorded keywords in the right sub-range are greater than or equal to Pivot.key, while Datum record pivot is in the correct position(Pivotpos), it does not need to participate in subsequent sorting.Attention:The key to partitioning is to require the location of the benchmark record Pivotpos. The results of the partitioning can be simply expressed as (note Pivot=r[pivotpos]):
it is easier to select the intermediate value .)
Step 2, compare each element with the "Reference" in order to form two subsets, one being "less than 45" and the other being "greater than or equal to 45 ".
Step 3: Repeat Step 1 and Step 2 for the two subsets until only one element is left for all subsets.
The following describes how to use Javascript to implement the above algorithm based on the information on the Internet.
First, define a quickSort
Quick sorting(QuickSort) is an improvement in Bubble sorting. The basic idea is to split the records to be sorted into two separate parts by one sort. the keywords of some records are smaller than those of other records, then, the two records can be sorted separately to achieve the whole sequence order.
// Quick sorting Void Cexchangesort: quicksort ( Void ){ Const Int Count = 9 , Len
entire array a [P.. R] is sorted.
The following process achieves quick sorting:
Array Division: partition (key, which sorts the child array a [P. R] in place)
C ++ implementation:
// * Chapter 7 quick sorting of algorithms * // # include
Quick performance analysis:
When the data volume is small, it is about a small sequence of 10 or more elements. The advantage of fast sorting is not obvious, or even slower than insertion sorting. However, once there is a large amount of data, its advan
-end subscript higher;3, define I, j two variables;4, set conditions: If the low >= high algorithm end, otherwise proceed the following steps;5. Take the first number of arrays as standard int standar, j = high, i = Low6, when I Stop when not satisfied, make a[i] = a[j], then the subscript of I move right i++;7, when I Stop when not satisfied, make a[j] = a[i], and then the subscript of I move left j--;8, exit the entire loop body, the value of I position is Standar9. Two sub-arrays of a recursi
","Insertsortadvanced","InsertSortAdvanced2","Binarytreesort","Shellsort","Shellsortadvanced","ShellSortAdvanced2","MergeSort","QuickSort","Heapsort"};public static void Main (string[] args) throws exception{Correctnesstest ();Performancetest (20000);}/*** Correctness Test * Simply test the correctness of each algorithm * Just to make it easy to see if the newly added algorithm is basically correct;* @throws Exception is mainly reflected by reflection
Java 1.0 and 1.1 libraries are missing the same thing is arithmetic operations, not even the simplest method of sorting operations. Therefore, it is best to create a vector that uses the classic quicksort (Quick Sort) method to sort itself.One of the problems with writing generic sorting code is that you have to perform comparison operations based on the actual type of the object to achieve the correct sort. One way to do this, of course, is to write
element is placed in the idle position indicated by the left, and rightIndicates that the position becomes an idle position;(3) Leave the left probe to the right, looking for elements larger than key, and find theThe value of the element is placed in the idle position indicated by right. Position indicated by left at this timeBecome idle;(4) Loop through 2, 3 steps until right is no longer greater than left (at this point,means that all elements are compared to key). The left and right meetPlac
exchange operation then the flat remains at 0 and is assigned to last and jumps directly out of the iteration. If the operation is flat assigned to I, so that it cannot equal 0, to prevent accidental jumping out.voidBubblesort (intArr[],intN) { intI, temp; intflat, last = N-1; while(Last >0) { for(i = flat =0; I i) {if(Arr[i] 1]) {temp=Arr[i]; Arr[i]= Arr[i-1]; Arr[i-1] =temp; Flat=i; }} last=flat; }}Three, quick sortRecursive thinking, find median oncesort (), each time the
Quick-Arrange algorithm? Simply put a position and then put a smaller number on the left, than his large number on the right, which is clearly a recursive definition, according to this idea can easily write the fast line of code? Quick Platoon is I learn the ACM road first let me remember the code, the impression is very deep, the previous study is Pascal, write this to write a long string, but because and merge sort is short, also back down. Curious I click on the encyclopedia to see the Python
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.