Here is a note from the sort of data structure
is mainly summarize sort on a variety of textbooks er (knock yourself over the code only qwq)
really use them, of course, that sort Dafa is good!
1. bubble sort (BubbleSort) [O (n ^ 2)]
to find each element, if there is less than behind him, let him to the front; take as small bubbles to the top of the same.
The code is very beautiful, simple and good-looking, but the complexity of the eight lines ah ...
void bubblesort(int a[],int l){
for(int i=0;i<l;i++){
for(int j=i;j<l;j++){
if(a[i]>a[j]){
swap(a[i],a[j]);
}
}
}
return ;
}
2. insertion sort (InsertionSort) [O (n ^ 2)]
is on the assumption that a part already sorted premise of continuous enlarged range of solutions to solve the problem
the first element of the sorted list to find (also the first row is not a good element), forward view, finding the right location makes it inserted.
//find the ele in front of it which is smaller
void insertionsort(int a[],int l){
for(int i=0;i<l;i++){
//倒序昂
for(int j=i;j>=0&&a[j]<a[j-1];j--){
swap(a[j],a[j-1]);
}
}
}
3. Insert the sort optimization - Shell sort
//Shellsort
void ShellSort1(int a[],int n){
int gap=n;
while(gap>1){
gap/=3;
gap++;
int id=gap;
while(id<=n){
int tmp=a[id];
int end=id-gap;
while(end>=0&&tmp<a[end]){
a[end+gap]=a[end];
end-=gap;
}
a[end+gap]=tmp;
id+=gap;
}
}
}
Another version
void ShellSort2(int a[],int n){
for(int gap=n/2;gap>2;gap/=2){
for(int j=0;j<gap;j++){
for(int k=gap;k<=n-j;k+=gap){
for(int o=k;o>=gap&&a[o]<a[o-gap];o-=gap){
swap(a[o],a[o-gap]);
}
}
}
}
//InsertionSort(a,12)
for(int k=1;k<=n;k++){
for(int o=k;o>=1&&a[o]<a[o-1];o--){
swap(a[o],a[o-1]);
}
}
}
4. Merge
void Merge(int a[],int l,int mid,int r){
int tmp[r-l+1];
int i=0,idl=l,idr=mid+1;
while(idl<=mid&&idr<=r){
tmp[i++]=a[idl]<=a[idr]?a[idl++]:a[idr++];
}
while(idl<=mid)tmp[i++]=a[idl++];
while(idr<=r) tmp[i++]=a[idr++];
for(int k=0;k<=r-l;k++)a[k+l]=tmp[k];
}
void Merge2(int a[],int l,int mid, int r){
int L[3000],R[3000],cnt1=0,cnt2=0,cnt=l-1;
for(int i=l;i<=mid;i++)
L[++cnt1]=a[i];
for(int i=mid+1;i<=r;i++)
R[++cnt2]=a[i];
int i=1,j=1;
while(i<=cnt1&&j<=cnt2){
if(L[i]<R[j])
a[++cnt]=L[i++];
else
a[++cnt]=R[j++];
}
while(i<=cnt1) a[++cnt]=L[i++];
while(j<=cnt2) a[++cnt]=R[j++];
return;
}
void MergeSort(int a[],int l,int r){
if(l<r){
int mid=(l+r)>>1;
MergeSort(a,l,mid);
MergeSort(a,mid+1,r);
Merge(a,l,mid,r);
}
}
The quick drain
void QuickSort(int a[],int l,int r){
if(l>r)return;
int base=a[l];
int i=l,j=r;
while(i!=j){
while(a[l]<=a[j]&&i<j)j--;
a[i]=a[j];
while(a[l]>=a[i]&&i<j)i++;
a[j]=a[i];
}
a[i]=base;
QuickSort(a,l,i-1);
QuickSort(a,j+1,r);
}
6. heapsort
//Heapsort
void siftdown(int a[],int x,int n){
int pos=x;
while(pos*2<n){
int j=pos*2;
if(j+1<n&&a[j+1]>a[j]) j++;
if(a[pos]>a[j]) return ;
swap(a[pos],a[j]);
pos=j;
}
}
void HeapSort(int a[],int n){
int tmp[500],cnt=0;
for(int i=n/2;i>=1;i--){
siftdown(a,i,n);
}
tmp[++cnt]=a[1];
for(int i=n;i>=2;i--){
swap(a[1],a[i]);
siftdown(a,1,i-1);
tmp[++cnt]=a[1];
}
for(int i=1;i<=n;i++){
a[i]=tmp[n-i+1];
}
}
7. radix sort
//Radix Sort
struct P{
int id[5000];
int l;
}bin[15];
void ini(int base){
for(int i=0;i<base;i++){
bin[i].l=0;
}
}
void getrank(int a[],int n,int id,int base){
for(int i=1;i<=n;i++){
int b=pow(base,id-1);
int ll=(a[i]/b)%base;
bin[ll].id[++bin[ll].l]=i;
}
}
void process(int a[],int n,int base){
int tmp[5000];
int cnt=0;
for(int i=0;i<base;i++){
if(bin[i].l){
for(int j=1;j<=bin[i].l;j++){
tmp[++cnt]=a[bin[i].id[j]];
}
}
}
for(int i=1;i<=n;i++)
a[i]=tmp[i];
}
void RadixSort(int a[],int n,int k,int base){
int bin[base];
for(int i=1;i<=k;i++){
ini(base);
getrank(a,n,i,base);
process(a,n,base);
}
}