Four kinds of Nlogn sorting algorithm codes

Source: Internet
Author: User

1. Quick sort: Unstable sort, best case O (NLOGN), worst case O (n^2). The average situation is the fastest sort in all nlogn sort

Quick sort in two steps
The first step is to block the L-r interval (assuming the last chunk of the interval as the keyword) is less than or equal to the keyword, the next chunk is greater than the keyword, and returns the position of the first greater-than keyword, which is the function partition
The second part is the quick sorting of l~pos-1 and Pos+1~r respectively.

Recursive type:
int partition (int l,int R)
{
int k=r;
while (L<R)
{
int c=0;
if (A[l]<=a[k])
l++;
Else
C + +;
if (A[r]>=a[k])
r--;
Else
C + +;
if (c==2)
{
int tem=a[l];
A[L]=A[R];
A[r]=tem;
}
}
if (l==r)
{
if (A[k]>=a[r])
return r+1;
Else
return R;
}
Else
return l;

}
void quicksort (int l,int R)
{
if (l>=r)
Return
int POS;
Pos=partition (L,R);
int Val=a[r];
A[r]=a[pos];
A[pos]=val;
Quicksort (L,POS-1);
Quicksort (POS+1,R);
}

Non-recursive:
The implementation of the stack, in the function of various variables and parameters are not a lot of cases with a recursive method is better
See more online

2.shell sort: Unstable sort, best case O (NLOGN), worst case O (n^x), 1<x<2
Variable-length insertion sort, with different K-lengths for the original array, note: The last must be an insertion sort of length 1
void Shellsort ()
{
for (int i=0;i<=x;i++)//x The number of groups representing the length
{
for (int j=len[i];j<n;j++)//Here is J + + instead of J+=len[i] attention!! , Hill sort idea of variable length
{
int k=j-len[i];
while (k>=0)
{
if (A[j]<a[k])
{
int TEM=A[J];
A[J]=A[K];
A[k]=tem;
Break
}
Else
K-=len[i];
}
}
}
}

3. Heap sequencing: Unstable sorting, best and worst time complexity are O (NLOGN):
The main is to write down the infiltration function, and then note:
1. When the array starts from 0 the left son is 2*x+1 and the right son is 2*x+2
Recursive return condition I (x+1)/2-1
2. When the array starts from 1 the left son is 2*x and the right son is 2*x+1
Recursive return condition I (x+1)/2
Class Heaps
{
Public
int heap[50]={9,8,7,6,5,4,3,2,1,34,5,6,74,24,5,1,2,3,4,55,21,13,41,3};
int maxsize=23;
void Godown (int i,int x)
{
if (i> (x+1)/2-1)//Return condition, easy wrong point
Return
int MAX;
Int J;
if (i*2+2>x)//When only left child situation!! Easy wrong point
{
MAX=HEAP[I*2+1];
j=i*2+1;
}
else//has two children.
{
if (heap[i*2+1]>heap[i*2+2])
{
MAX=HEAP[I*2+1];
j=i*2+1;
}
Else
{
MAX=HEAP[I*2+2];
j=i*2+2;
}
}
if (Max>heap[i])
{
Heap[j]=heap[i];
Heap[i]=max;
Godown (J,X);
}

}
void Buildheap ()
{
for (int i= (maxsize+1)/2-1;i>=0;i--)
Godown (i,maxsize);
}
void Heapsort ()
{
int k=maxsize;
for (int i=1;i<=maxsize;i++)
{
int tem=heap[k];
HEAP[K]=HEAP[0];
Heap[0]=tem;
k--;
Godown (0,k);//Note that the size of the heap is constantly changing
}
}
};

4. Merge sort: Stable sorting, best and worst time complexity are O (NLOGN)

void merge (int* a,int l,int R)
{
int tem[100];
int mid= (L+R)/2;
int i=l;
int j=mid+1;
int k=l;
for (; i<=mid+1| | j<=r+1;i++,j++)
{
if (i==mid+1&&j==r+1)
Break
if (i==mid+1)
{
TEM[K++]=A[J];
i--;
Continue
}
if (j==r+1)
{
Tem[k++]=a[i];
j--;
Continue
}
if (A[i]<a[j])
{
Tem[k++]=a[i];
j--;
}
Else
{
TEM[K++]=A[J];
i--;
}
}
}
void Merge_sort (int* a,int l,int R)
{
int mid= (L+R)/2;
if (mid+1>r)
Return
Merge_sort (A,l,mid);
Merge_sort (A,MID+1,R);
Merge (A,l,r);
}

There are also 3 sorts of O (n^2) that are not described in detail
1. Select sort: Unstable, time complexity O (n^2)
2. Insertion sort: Stable, time complexity O (n^2)
3. Bubble sort: Stable, time complexity O (n^2)

Four kinds of Nlogn sorting algorithm codes

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.