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
__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
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
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
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>
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 (*
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
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,
} + 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
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
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
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
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
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: 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
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.