quicksort

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

POJ 2299 ultra-quicksort (merge sort or bit tree + discretization available)

;typedef __Int64LL;int N;int A[500001],pos[500001],elem[500001];IntLowbit(int x){return x-x;}IntSum(int ID){int ret=0;While(ID>0){RET+=elem[ID]; Id-=Lowbit(ID);}return ret;}voidAdd(int ID,int Val){While(ID){Elem[ID]+=val; Id+=Lowbit(ID);}}VectorSave;IntMain(){While(scanf("%d", n)!=eofn){Save.Clear();Memset(Elem,0,sizeof(Elem));For(int I=1; I; I++){scanf("%d", a[I]); Save.Push_back(A[I]);}Sort(Save.Begin(), Save.End());For(int I=1; I; I++){POS[I]=Lower_bound(Save.Begin(), Save.End(), A[I])-save.B

Quick Sort (QuickSort)

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

POJ 2299 Ultra-quicksort Reverse order to the template problem

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) { -

POJ2299 Ultra-quicksort

'; CH=GetChar (); } returnAns *op;}intn,a[m],c[m],tot,d[m];ll ans;voidAddintx) { while(x 5) A[x]++,x + =lowbit (x);}intQueryintx) { intCur =0; while(x) cur + = a[x],x-=lowbit (x); returncur;}voidClear () {tot= ans =0; Memset (A,0,sizeof(a));}intMain () { while(1) {n=read (); if(n = =0) Break; Clear (); Rep (I,1, n) c[i] = Read (), d[i] =C[i]; Sort (c+1, c+1+N); Tot= Unique (c+1, c+1+N)-C-1; Per (I,n,1) { intK = Lower_bound (c+1, c+1+tot,d[i])-C; //printf ("#%d\n", k);Ans

Introduction to Reading Notes algorithms quick sorting quicksort uses the last element as the marker

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;

POJ 2299 Ultra-quicksort

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

"Sort" QuickSort

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); -

Cross-platform C ++ millisecond timing -- Taking the Quick Sort Algorithm as an example, quicksort

Cross-platform C ++ millisecond timing -- Taking the Quick Sort Algorithm as an example, quicksort # Include

Play Scala 71 Step-by-step quicksort

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

POJ 2299 Ultra-quicksort (tree-like array + discretization)

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;

POJ 2299 Ultra-quicksort (tree-like array + discretization)

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

--quicksort implementation of C-language generic programming

, typesize); - Char*border = paa + END *typesize; to inti =Beg; + intj =Beg; - for(; J ) the { * if(CMP (paa+j*typesize, border) 0){ $G_swap (Paa+j*typesize, paa+i*typesize,typesize);Panax Notoginsengi++; - } the } +G_swap (Paa+j*typesize, paa+i*typesize,typesize); AQuick_sort (paa,beg,i-1, typesize,cmp); theQuick_sort (paa,i+1, end,typesize,cmp); +}generic_quick_sort.c1#include 2#include 3#include string.h>4#include"generic_quick_sort.h"5 #definePrint_st

QuickSort (MFC)

#include "stdafx.h" #include QuickSort (MFC)

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

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

POJ 2299 ultra-quicksort Merge sort to find the inverse number pair

namespacestd;6 #defineMAXN 5000107 8 intA[MAXN], B[MAXN];9 Long Longcount;Ten One voidMerge (intLintR); A intMain () - { - intn, I; the while(SCANF ("%d", N), N) - { -Memset (A,0,sizeof(a)); -memset (b,0,sizeof(b)); + for(i=0; i) -scanf ("%d", a[i]); +Count =0;//must be used int64,int32 will overflow AMerge (0, n); atprintf ("%lld\n", count); - } - return 0; - } - - voidMerge (intLintr)//merge sort, the parameter is the position of the sub-interval respectively i

POJ 2299 Ultra-quicksort (tree-like array)

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

The method of realizing quicksort fast sorting algorithm in Java by graphics and text _java

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

Fast Sort (Quicksort) JavaScript implementation

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" (

Algorithm-Quick Sort (Quicksort)

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

Ultra-quicksort (tree array + discretization)

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

Total Pages: 15 1 .... 5 6 7 8 9 .... 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.