13scpca201710161-Sort (1)

Source: Internet
Author: User

Because the foundation was so weak, I decided to re-learn the algorithm from beginning to end.

The first part is sort of.

Rokua training Field first question, quick sort template

Qsort is a kind of Divide and conquer algorithm.

The main idea is:choose the pivot, and rearrange the array to a new one with the tag of position of pivot, which all elem Ents smaller than the pivot is in the lower position while the bigger ones is higher.

I'll have a try according to the simple explanation.

Firstly, determine the functions that is needed

1) qsort ();

2) Rearr ();

In Qsort, the array was divided into a equal parts, and then was qsorted respectively.

In Rearr, all elements that smaller than pivot be put before pivot and the tag of pivot is returned.

This is the first code

#include <stdio.h>
int shuz[10000],n;
int Zhoux (int arr[],int zuo,int you)
{
int Temp,now=zuo;
int Zhou;
Zhou=arr[you];
for (int i=zuo;i<you;i++)
{
if (Arr[i]<=zhou)
{
Temp=arr[now];
Arr[now]=arr[i];
Arr[i]=temp;//began to write temp Arr[now] Mdzz
now++;
}
}
Arr[you]=arr[now];
Arr[now]=zhou;
return now;
}
int qsort (int arr[],int zuo,int you)
{
if (You<=zuo) return 0;//mom, this symbol is reversed.
int Mid=zhoux (arr,zuo,you);
Qsort (arr,zuo,mid-1);//write 1 to the back of Zuo ...
Qsort (arr,mid+1,you);
return 0;
}
int main ()
{
scanf ("%d", &n);
for (int i=0;i<n;i++)
scanf ("%d", &shuz[i]);
Qsort (shuz,0,n-1);
for (int i=0;i<n;i++)
printf ("%d", shuz[i]);
return 0;
}

Obviously, the time taken formula is:

T (n) =t (k) +t (n-k-1) +theta (n)//theta (n) is the time cost of partition.

The final formula is represented as the sum of Mant theta.

Average time Complexity:o (Nlogn), Worse:o (n2) [Every extreme number is in the pivot point, or to say it's sorted or inte Rspersed by extreme number]

If there is many redundant elements, 3-way QuickSort can be used.

Main idea:

Collect recurring pivots and put into middle together.

Example:

#include <stdio.h>
int shuz[10000],n;
int Zhoux (int arr[],int zuo,int you)
{
int Temp,now=zuo;
int zhou,rec=0;
Zhou=arr[you];
for (int i=zuo;i<you;i++)
{
if (Arr[i]==zhou)
{
rec++;
if (Arr[you-rec]==zhou) break;//added this sentence is right, but remove is not infinite loop really difficult to understand????????????????????????????????????? ←← difficult to understand a fart, REC became big enough not to go hhhhh
ARR[I]=ARR[YOU-REC];
Arr[you-rec]=zhou;
i--;
}
if (Arr[i]<zhou)
{
Temp=arr[now];
Arr[now]=arr[i];
Arr[i]=temp;//began to write temp Arr[now] Mdzz
now++;
}
}
for (int i=0;i<=rec;i++)//The first time I<rec went wrong and changed to I=-1hhhhhh
{
Arr[you-i]=arr[now+i];
Arr[now+i]=zhou;
}
return now;
}
int qsort (int arr[],int zuo,int you)
{
if (You<=zuo) return 0;//mom, this symbol is reversed.
int Mid=zhoux (arr,zuo,you);
Qsort (arr,zuo,mid-1);//write 1 to the back of Zuo ...
Qsort (arr,mid+1,you);
return 0;
}
int main ()
{
scanf ("%d", &n);
for (int i=0;i<n;i++)
scanf ("%d", &shuz[i]);
Qsort (shuz,0,n-1);
for (int i=0;i<n;i++)
printf ("%d", shuz[i]);
return 0;
}

There is also a iterative the qsorting that use stack to record the Zuos and yous.

Learning Resources:

Rokua Website: https://www.luogu.org/problem/show?pid=1177

geekforgeeks:http://www.geeksforgeeks.org/quick-sort/

Gadgets:

Online ide:http://ide.geeksforgeeks.org/index.php

New words:

Implementation redundant reservation attach theta recursive partition traverse pseudo implement pivot Occurrenc E recursively iterative

13scpca201710161-Sort (1)

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.