13SCPCa201710161-排序(1)
因為之前基礎十分薄弱所以決定從頭到尾重新學習一遍算法
第一部分是排序啦
洛谷訓練場第一題、快速排序模板
Qsort is a kind of Divide and Conquer algorithm.
The main idea is: choose the pivot, and rearrange the array into a new one with the tag of position of pivot that all elements smaller than the pivot are in the lower position while the bigger ones are higher.
I will have a try according to the simple explanation.
Firstly, determine the functions that are needed
1)qsort();
2)rearr();
in qsort, the array is divided into two equal parts, and then be qsorted respectively.
in rearr, all elements that smaller than pivot are 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;//一開始把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;//媽個巴子這符號寫反了
int mid=zhoux(arr,zuo,you);
qsort(arr,zuo,mid-1);//把-1寫到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.
So 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 is sorted or interspersed by extreme number]
if there are 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;//加了這句話就對了,但是去掉並不是無限循環真難懂??????????????????←←難懂個屁咧、rec變得足夠大不就行了嘛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;//一開始把temp寫成arr[now]了mdzz
now++;
}
}
for(int i=0;i<=rec;i++)//一開始是i<rec第一次出錯竟然改成了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;//媽個巴子這符號寫反了
int mid=zhoux(arr,zuo,you);
qsort(arr,zuo,mid-1);//把-1寫到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 an iterative way of Qsorting that use stack to record the zuos and yous.
學習資源:
洛谷網站: https://www.luogu.org/problem/show?pid=1177
GeekforGeeks: http://www.geeksforgeeks.org/quick-sort/
小工具:
在線IDE: http://ide.geeksforgeeks.org/index.php
New words:
implementation redundant reservation attach theta recursive partition traverse pseudo implement pivot occurrence recursively iterative
13SCPCa201710161-排序(1)