sort obtained by this method is generally called a random quick sort. Specific algorithm "See textbook"Note:The fast ordering of randomization is very different from the general fast sorting algorithm. However, after randomization, the performance of the algorithm is greatly improved, especially for the initial order of the file, it is generally not possible to cause the worst case occurrence. The randomization of the algorithm is not only suitable for fast sequencing, but also for other algori
Using a tree-like array to find the only pit point is the sum to use a long long to save the code directly after forgetting can also directly see 2333 ...PS: And the hdu3743 code is the same, because two are reverse to the template problem ...1#include 2#include string.h>3#include 4 using namespacestd;5 ints[500005];6 intN;7 structnum{8 intXuhao,num;9}nu[500005];Ten intQufan (inti) { One returni (-i); A } - intcmp (num nu1,num nu2) { - returnnu1.numNu2.num; the } - intSumminta) { -
Quick sorting is the most common sorting method in practical programming applications.
He has excellent performance.
The worst case time complexity is O (n square meters)
The average time complexity is O (n logn) and has a very small coefficient.
And the space complexity is very small, that is, O (n)
But thisAlgorithmIt is also difficult to understand...
The following is a quick algorithm that uses the last element as the token.
Using System; Using System. Collections. Generic;
line segment tree maintenance interval and, That is, you can know how many numbers appear in a certain interval, and for the current number, you can tell how many of the numbers are larger than the number that is present in the interval of the current number to N.Code:#include Tree array: The idea is the same as the line tree, maintenance interval and, but the time is reduced by half.Code:#include Merge sort: Each time a combination of two sections of the sequence of the interval, when the
double pointer i,j is used. I brushed back from the value less than pivot, JValue that, when I,j is stopped, swaps the value at the point where the I,j is located.3. In a small array (the size of a group 1 voidQuicksortint*nums,intN)2 {3QS (Nums,0, N-1);4 }5 voidQsint*nums,intLeftintRight )6 {7 intPV;8 inti,j;9 intcutoff=Ten;Ten if(left+cutoffRight ) One { Apv=mid3 (nums,left,right); -I=Left ; -j=right-1; the while(1) - { - while(nums[++i]PV); -
really embodies the fast place is "jumping" to exchange nonadjacent two numbers, so that the number of reverse order "may" reduce a lot. Below we continue to improve the divide method. This time, two pointers are used to iterate through the array from each end.packagealgorithm.qs;importjava.util.arrays;publicclassqs{ finalstaticinteger[]a={6,1,2,7,9,3, 4,5,10,8};staticvoid Swap (INTNBSP;I,NBSP;INTNBSP;J) {intt= a[i];a[i]=a[j]; a[j]=t;} /** divides the number of positions between S and T into tw
the 3rd bit to 11 2 3 4 51 1 1 1 1Calculation 1-Is there a number on the 3 that is smaller than 3? This uses the getsum of the tree-like array (3) =3 operation, now with the input subscript 5-Getsum (3) =2you can get an inverse number of 3 for 2. 6.0+1+2+1+2=6This is the final reverse number.1#include 2#include 3#include 4#include 5#include 6#include 7#include 8#include 9#include Ten using namespacestd; One A Const intmaxn=500000+5; - - intN; the - structnode - { - intVal; + intPos;
a number at a time, calling update () and read (), respectivelyOuter loop N, update () and read () Time O (logn) = time complexity or O (NLOGN).The final total is O (Nlogn).Code:1# include2# include3# include4# include5 6 using namespacestd;7 8# define MAX500000+49 TentypedefLong LongLL; One A LL A[max]; - LL Data[max]; - intTree[max]; the intN; - - voiddiscrete () - { +memset (data,0,sizeof(data)); - for(inti =0; I ) + { AData[i] =A[i]; at } -Sort (data,data+n); - intCC = U
Links: Poj 2299Test Instructions : gives a sequence of length n, which can only exchange two adjacent elements at a time.Ask at least a few times to make the sequence an ascending sequence Analysis : Bubble sort can only swap adjacent two elements at a time. That is to askuse bubble sort to make it the number of interchanges of an ascending sequence, every time I exchange a record.However, this data is larger, the bubble sort efficiency is low, will time outIn this case, we can use the merge sor
place the number of the first number before it in the corresponding interval to set its value to 1, then the problem is converted to the sum of the [i+1,n] value. Think again, the value of the interval [1,i] + the value of the interval [i+1,n] = the value of the interval [1,n] (i is already marked as 1), so the sum of the interval [i+1,n] value equals the value of N-[1,i]! Because there are always n numbers, it is either smaller or larger than it (large or equal).Now the problem has been transf
index start that needs to be sorted quickly * @param end of an array that needs to be sorted quickly /public static final void QuickSort (int[] array, int start, int end) {//I is equivalent to assistant 1 position, J is equivalent to assistant 2 position int i = start, j =
End int pivot = Array[i]; Take the 1th element as the datum element int emptyindex = i; The location index that represents the vacancy, the default is the position of the extrac
Japanese programmer Norahiko, wrote a sort algorithm animation demo, very interesting.
This weekend, I use it as a textbook, a good study of a variety of sorting algorithms.
The sorting algorithm (sorting algorithm) is one of the oldest and most basic topics in computer science. To be a qualified programmer, you must understand and master all sorts of sorting algorithms.
At present, the most common sort algorithm is about seven or eight kinds, among which "quick sort" (
The idea of a quick sort: (divided by rank)
(1) In the data set, select a number for comparison ("Datum" Pivot).
(2) Compare data collection traversal to datum data, less than to the left of datum data, and elements greater than datum to the right of datum data.
(3) for the two subsets to the left and to the right of the datum, recursively repeats the first and second steps until only one element is left in all the subsets.
JavaScript implementation//1. Quick SortvarStartTi
DescriptionIn this problem, you have to analyze a particle sorting algorithm. the algorithm processes a sequence of N distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. for the input sequence
9 1 0 5 4,
Ultra-quicksort produces the output
0 1 4 5 9.
Your task is to determine how swap operations ultra-quicksort needs to perform in order to
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.