quicksort

Read about quicksort, The latest news, videos, and discussion topics about quicksort from alibabacloud.com

Quick Sort Method QuickSort---Java

Quick Sort Method:public class Main {public static void Main (string[] args) {int a[]={7,8,1,3,5}; New Main (a); Public Main (int[] a) {System.out.println ("before sorting:"); Print (a); QuickSort (a,0,a.length-1); System.out.println (); System.out.println ("After sorting:"); Print (a); } public int Getmiddle (int[] list,int low,int high) {int temp=list[low];//array first as the middle axis while (LOWL

Quick Sort QuickSort

Learning Links: Sitting on the toilet see algorithm: Quick SortJava code:1 classquicksort{2 int[] Sortans;3 4 QuickSort () {}5 6QuickSort (int[] nums) {7QSort (nums,0,nums.length-1);8sortans=nums;9 }Ten One protected voidQSort (int[] Nums,intAintb) { A if(aC) { - intpos=patition (nums,a,b); -QSort (nums,a,pos-1); theQSort (nums,pos+1, b); - } - } - intPatition (int[] Nums,intAintb) { +

Python Quicksort Demo

__author__ = ' student ' Quicksortstep 1, choose one pivot, such as Pivot=la[0]step 2, scan the data from right side, find Data less than pivot, then swap this with Pivotpivot=1 [4] 5 7 3 9 [J]then scan from left side, find data greater tha n Pivot, then swap the position J and I4 [] 7 3 9 5when i>=j then finish one loop. Then put the pivot in the I; All data is dived by pivot now. than pivot and right is greater than Pivot.think, step by step, do it and try Somethingstep 3. Then you have the pa

POJ 2299 ultra-quicksort (merge sort, reverse order number) __ Sort

Description In this problem, your have to analyze a particular 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 many swap operations Ultra-quicksort needs to perform in order to

poj2299:ultra-quicksort-merge sort to solve the problem of reverse order number

1#include 2#include 3 using namespacestd;4 Long Longans;5 voidMergeint*a,intLeintMidintRT) {6 int*sort_data= (int*) malloc (sizeof(int) * (rt-le+1));7 if(!sort_data)return;8 intI=le,j=mid+1, pt=0;9 while(iRT) {Ten if(a[i]A[j]) { Onesort_data[pt++]=a[i++]; A } - Else{//exist swap action -sort_data[pt++]=a[j++]; the /* - once we swap the postion of both the a[i] and A[j], - We do change the positon of A[i+1......mid] - as a result, we do the pos

POJ 2299 ultra-quicksort (merge sort)

Test instructions: Swap adjacent two numbers to sort at least a few timesIdeas:Test instructions can be converted into several reverse order numbers in the sequenceCan be thought of as bubble sort, but complexity is too high to complete with merge sort(note n#include #include#include#includeusing namespacestd;inta[5000000+ -];intt[5000000+ -];__int64 ans;intL,r;voidMergeintXinty) { if(y-x>1) { intm=x+ (y-x)/2; intP=x,q=m,i=x; Merge (X,m); Merge (M,y); while(py) {if(q>

Quick Sort (QuickSort)

the range of data partitions, range is the number of elements ***************************************************************/int* hoarepartition (int* Low, int* high, int range) {///three-digit int a = * ((int) ((double) rand ()/rand_max) * range) + low); int b = * ((int) ((double) rand ()/rand_max) * range) + low); int c = * ((int) ((double) rand ()/rand_max) * range) + low); int pivot = a > B? (b > C? B: (A > C. c:a)): (A > C? A: (B > C? c:b)); low--; while (true) {while (*

Quick Sort Qsort quicksort C language

Now the online search of the fast row and I played before the same, feeling a bit complicated, I used the freepascal/demo/text/qsort.pp style, feeling particularly concise.1#include 2 #defineMAXN 100003 intA[MAXN];4 intN;5 voidMysort (intLintr) {6 intx,y,mid,t;7Mid = a[(l+r)/2];8x=l;9y=R;Ten Do { One while(a[x]; A while(A[y]>mid) y--; - if(xy) { -t=A[x]; thea[x]=A[y]; -a[y]=T; -X + +; -y--; + } -} while(xy); + if(ly) Mysort (l,y); A if(xR) Mysor

POJ 2299 Ultra-quicksort (tree-like array && discretization && reverse order)

);}voidAddintKintnum) { while(kN) {c[k]+=num; K+=Lowbit (k); }}intSumintk) {intans=0; while(k) {ans+=C[k]; K-=Lowbit (k); } returnans;}intMain () { while(SCANF ("%d", n)! =EOF) {memset (c,0,sizeof(c)); if(n==0) Break; for(intI=0; I) {scanf ("%d",A[i]); B[i]=A[i]; } sort (B,b+N); intsumm=0; for(intI=0; I) {A[i]= (Lower_bound (b,b+n,a[i])-B) +1; Summ+ = (sum (n)-sum (a[i])); //summ+= (I-sum (a[i]+1)Add (A[i],1); } printf ("%d\n", Summ); }}View CodePOJ 2299 Ultra-

Quicksort Quick Sort

Javascriptfunction Qsort (A, comp) {if (typeof comp=== "undefined") {comp = function (A, b) {return b   Quicksort Quick Sort

POJ 2299 ultra-quicksort discretization + tree-like array

Title Link: http://poj.org/problem?id=2299Discretization + Tree-like arrayTextbook Examples of topics#include #include#include#include#include#include#include#includeSet>#include#include#includeusing namespaceStd;typedefLong Longll;Const intMAXN =500010;structpre{intnum, id; BOOL operatorConstPre b)Const { returnNum B.num; }}P[MAXN];intA[MAXN], BIT[MAXN], N;intSuminti) { ints =0; while(I >0) {s+=Bit[i]; I-= i -i; } returns;}voidAddintIintx) { while(I N) {Bit[i]+=x; I+ = i -i

POJ2299 Ultra-quicksort "tree-like array" "Reverse order Number"

Topic Links:http://poj.org/problem?id=2299Main topic:Give you a sequence of n integers that can only be exchanged for adjacent numbers and eventually into ascending order, Q: How many interchanges are required at least.Ideas:is to ask the number of times the bubble sort is exchanged. In fact, it is to find the inverse number of the original sequence. It can be done with merge sort, line tree, tree array.But if you do it with a line tree and a tree array, because the number of elements is 500000,

POJ-2299 ultra-quicksort (merge sort)

;typedefLong Longll;templateclassT>voidTest (T a) {coutEndl;} TemplateclassTclassT2>voidTest (T a,t2 b) {cout" "Endl;} TemplateclassTclassT2,classT3>voidTest (T a,t2 b,t3 c) {cout" "" "Endl;} TemplateclassT>inlineBOOLScan_d (T ret) { CharCintSGN; if(C=getchar (), c==eof)return 0; while(c!='-' (c'0'|| C>'9')) c=GetChar (); SGN= (c=='-')?-1:1; RET= (c=='-')?0:(C-'0'); while(C=getchar (), c>='0'c'9') ret = ret*Ten+ (C-'0'); RET*=SGN; return 1;}//const int N = 1E6+10;Const intINF =0x3f3f3f3f;Co

Sort of quick sort (quickSort)

} + the } - Public Static voidMain (string[] args) { $ the int[] B =New int[]{2,7,4,5,10,1,9,3,8,6}; the int[] C =New int[]{1,2,3,4,5,6,7,8,9,10}; the int[] D =New int[]{10,9,8,7,6,5,4,3,2,1}; the int[] A =New int[]{1,10,2,9,3,2,4,7,5,6}; - inSort (A, 0, a.length-1); the theSYSTEM.OUT.PRINTLN ("Sorted Result:"); About for(intx:a) { theSystem.out.print (x+ ""); the

Ultra-quicksort (array sorting problem)

to input8 while(I//Two input space is not 09 {Ten if(A[i]>a[j])//take the minimum value into the output space One { Ab[p++]=a[j++]; -ans+=m-i+1;//A[i] In reverse order of values relative to A[i] - } the Else -b[p++]=a[i++]; - } - while(i//non-empty input space into output space + for(i=0; i//merge End, result copied to original input array -a[l+i]=B[i]; + } A voidMergeSort (intL2,intR2)//Merge Sort at { - if(l2R2) - { - intM

Ultra-quicksort---poj2299 (merge sort)

Title Link: http://poj.org/problem?id=2299Test instructions is to ask the array in order from small to large, at a time can only exchange adjacent two number, for at least a few exchangesis to find the reverse number by merging sort#include #includestring.h>#include#defineN 501000using namespacestd;intA[n], B[n];__int64 cnt;voidMerge (intRintMidintl) { inti = r, j = mid+1, k =0; while(Il) {if(A[i] >A[j]) {B[k+ +] = a[j++]; CNT+ = mid-i+1;///indicating that the subscript from I to mid values

Fourth week of training (high efficiency algorithm design) a topic Ultra-quicksort

Original title POJ 2299:http://poj.org/problem?id=2299Test instructions, give you an array, to count their inverse number, because the title of the array can be up to 500,000, then O (n^2) sorting algorithm do not think again, the next choice is fast row, merge, see you likeHere is a list of merge solutions:#include"Iostream"UsingNamespace Std;Constint MAXN=500000+10;int T[MAXN];int a[MAXN];LongLong sum;voidMerge_sort(Int*a,int x,int y,Int*t){If(Y-X>1){int m=x+ (Y-X)/2;int P=xQ=m, I=x;Merge_sort

POJ 2299 Ultra-quicksort (tree-like array)

Test instructions: To find the number of times a sequence of bubbles is exchanged;Reference: http://blog.csdn.net/suwei19870312/article/details/5293694Ideas:The number of bubbles in a sequence is the sum of the inverse logarithm corresponding to each number, and the naïve thought is the complexity of two for,o (n^2);The number range is 0-999999999, the array size is 500000, so the first discretization, the structure of the original sequence of the subscript and the value of the record;For the nu

Introduction to the MIT algorithm--fourth. Quicksort

process.After getting the recursion as shown, how to solve it becomes a problem. The method used here is the substitution method (Substitution).In this way, we get the algorithm efficiency of the stochastic fast-row is θ (NLGN).In practice, fast sequencing is a good algorithm. Although it is not guaranteed that the merge sort provides the worst-case NLGN run time, in practice, if the randomized fast sorting algorithm is used, it is usually 3 times times faster than the merge sort. Admittedly, i

Algorithms Part 1-question 2-quicksort-quick Sorting Algorithm

Algorithms: design and analysis, Part 1 Chapter 1 describes the partitioning algorithm, namely, DC. This chapter describes quick sorting. The difficulty of the job has been increased. After the problem sets were done twice, they accidentally scored only four points, and the programming job was done twice. This job is to achieve fast sorting and change the selection method of the sentry element to compare the performance. The Sentinel can be the first and last elements, or the second largest num

Total Pages: 15 1 .... 6 7 8 9 10 .... 15 Go to: Go

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.