# Sorts of sorts (i)

Source: Internet
Author: User
Tags sorts

Recently began to learn sort. The role of sequencing does not need to say much, the current view of the comparison pits is not with high precision operation is to use the sort. As Gromah the Great God said: "Do not sort of learning what information Science contest AH!" "So we summarize here so that we can learn the next step. Here are some of the more simple sorts that are currently learned.

Resources:

"Informatics Competition", "Algorithmic competition Primer", and a sort of schematic software and its code on the site:

Http://panthema.net/2013/sound-of-sorting

Body:

Select Sort:

Principle: Each order selects a value element from the data to be sorted, placed in the front of the sequence to be ordered, until all the data elements are sorted out.

Algorithmic complexity: O (n^2)

Realize:

void Ssort ()//selectsort
{
int i,n,temp,k=1;
scanf ("%d", &n)
for (int i=1;i<=n;++i) scanf ("%d", &a[i]);
for (int i=1;i<=n-1;++i)
{
for (int j=1;j<=n;++j)
{
if (a[j]<a[k]) k=j;
}
if (k!=1)
{
Temp=a[i];
A[I]=A[K];
A[k]=temp;
}
}
for (int i=1;i<=n;++i) printf ("%d", a[i]);
printf ("/n");
}

Bubble Sort:

Principle: Compare the adjacent two data in turn, put the big one in front, the small put back.

Algorithmic complexity: O (n^2)

Realize:

void Bsort ()//bubblesort
{
int n,temp;
scanf ("%d", &n);
for (int i=1;i<=n;++i) scanf ("%d", &a[i]);
for (int i=1;i<=n-1;++i)
{
for (int j=1;j<=n-1;++j)
{
Temp=a[j];a[j]=a[j+1];a[j]=temp;
}
}
for (int i=1;i<=n;++i) printf ("%d", &a[i]);
printf ("/n");
}

Insert Sort:

Principle: Suppose the data to be sorted is stored in the array r[1→n], adding a sentinel node X.
(1) r[1] Self into 1 ordered area, disordered area is r[2→n];

(2) from i=2 up to I=n, put R[i] in the proper position, so that the R[1→i] data series orderly;
(3) Generate an ordered area with N data.

Algorithmic complexity: O (n^2)
Realize:

void Isort ()//insertionsort
{

const int MAXN = 10000;
int N,X,A[MAXN];
scanf ("%d", &n);
for (int i=1;i<=n;++i)
scanf ("%d", &a[i]);
for (int i=1;i<=i;++i)
{
X=a[i];
J=i-1;
while (X<a[j])
{
A[J+1]=A[J];
j--;
}
A[j+1]=x;
}
}
for (int i=1;i<=n;++i)
printf ("%d", &a[i]);
printf ("/n");
}

Bucket sort:

Principle: This is a sort of mapping relationship that is similar to a function. What's more specific is that if the key of the record to be sorted is within a distinctly limited range (integer), a finite ordered bucket can be designed, each bucket is loaded with a value (several values can be loaded), sequentially outputting the values of each bucket, and an ordered sequence is obtained.

Algorithm complexity: Stable ordering, the complexity of the linear order. O (n) when it is exactly a value in each bucket, if the general case complexity is O (n+m) where M is the range of the bucket

Realize:

void B2sort ()//bucketsort
{
int k,l,n;
memset (A,0,sizeof (a));
printf ("%d", &n);
for (int i=1;i<=n;++i)
{
scanf ("%d", &k);
a[k]++;
}
for (int i=0;i<=n;++i)
{
while (a[i]>0)
{
printf ("%d", I);

a[i]--;

}
printf ("/n");
}
}

Quick sort:

The first time to learn the fast platoon, thought it is very simple, only need to use the C + + STL Template Library #include<algorithm> and then

Sort (array+0,array+n); However, it is now found that the principle is slightly more complex, and also divided into randomized and non-randomized fast, here for the moment only to summarize the non-randomized fast row. If you want to use a randomized fast row, you can use the self-contained express in the STL Template Library.

Principle: By dividing the pending records into two separate parts by a single pass, where some of the recorded keywords are smaller than the keywords in the other part of the record, you can continue sorting the two records until the sequence is ordered.

Algorithm complexity: O (NLOGN), although it is an unstable sort, but it is the most effective in the sorting of the current.

Realize:

void qsort (int l,int R)//quicksort;
{
int n,temp,i=l,j=r,mid;
scanf ("%d", &n);
for (int i=1;i<=n;++i) scanf ("%d", &a[i]);
mid=a[(L+R)/2]; Two-part algorithm
Do
{
while (A[i]<mid) i++;
while (A[j]>mid) j--;
if (I&LT;=J)
{
Temp=a[i];
A[I]=A[J];
A[j]=temp;
i++;
j--;
}
}while (I&LT;=J);
if (i<j) qsort (I,J);
for (int i=1;i<=n;++i)
printf ("%d", &a[i]);
printf ("/n");
}

Merge sort:

Principle: Combine two or more ordered columns (or ordered tables) into a sequence (ordered table) that is still ordered, which is called a merge operation. Such a method is often used to merge multiple sequential data files into an ordered data file.

The merge process is compared to the size of a[i] and a[j], if A[I]≤A[J], then the elements in the first ordered table A[i] copied to r[k], and I and K add 1 respectively, even if it refers to the next unit, or the second ordered TABLE element A[j] copied to R[k], and make J and K add 1 respectively; so loop until one of the ordered tables is finished, and then copy the remaining elements from the other ordered table to the cells in R from subscript K to subscript t.

Algorithmic complexity: Stable sequencing with O (NLOGN) complexity

Realize:

void Msort (int s,int t)//mergesort
{
int n,mid,l,j,k;
memset (t,0,sizeof (t));
scanf ("%d", &n);
for (int i=1;i<=n;++i) scanf ("%d", &a[i]);
if (s==t) return;
Mid= (s+t)/2;
Msort (S,mid);
Msort (mid+1,t);
i = s;
j = m+1;
K = s;
while (i<=m&&j<=t) do
{
if (A[i]<=a[j])
{
T[k] = a[i];i++;k++;
}
Else
{
T[k] = a[j];j++;k++;
}
}end;
while (I&LT;=M)
{
T[k] = a[i];i++;k++;
}
while (j<=t)
{
t[k]=a[j];j++;k++;
}
for (I=s;i<=t;++i) a[i]=t[i];
for (int h=1;h<=n;h++)
printf ("%d", a[i]);
printf ("\ n");
}

For the time being summarized here, now carries on the next study.

Sorts of sorts (i)

Related Keywords:

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.