Six Algorithms brocade set

Source: Internet
Author: User

1. Bubble sort

In the set of numbers to be sorted, the total number in the range that is not currently in sequence, the top-down pairs of adjacent two numbers are compared and adjusted sequentially, so that the larger number to sink, smaller upward. That is, each time a comparison of two adjacent numbers finds that they are in the opposite order of order, they are interchanged.

The bubbling sort is stable. Algorithm time complexity O (n2)--[n squared ]

Main ()

{

int a[10],i,j,k;

printf ("This is a Maopao sort!\n");

printf ("Please input numbers for sort:");

for (i=0;i<10;i++)

scanf ("%d", &a[i]);

for (i=0;i<9;i++)

for (j=0;j<10-i;j++) if (a[j]>a[j+1])

{

K=A[J];

A[J]=A[J+1];

A[j+1]=k;

}

printf ("The corret sort of those numbers is:");

for (i=0;i<10;i++)

printf ("%d", a[i]);

printf ("\ n");

}

2. Select Sort

In the set of numbers to be sorted, select the smallest number to exchange with the first position, and then in the remaining number, find the smallest and second position of the number of exchanges, so loop to the penultimate number and the last number comparison.

Choosing a sort is not stable. Algorithm complexity O (n2)--[n squared ]

Main ()

{

int t,k,i,j,a[10];

printf ("This is a select sort\n");

printf ("Please input some number, want to sort:");

for (i=0;i<10;i++)

scanf ("%d", &a[i]);

for (i=0;i<9;i++)

{

K=i;

for (j=i+1;j<10;j++)

if (A[k]>a[j])

K=j;

T=a[i];

A[I]=A[K];

a[k]=t;

}

printf ("The correct sort of those number is:");

for (i=0;i<10;i++)

printf ("%d", a[i]);

printf ("\ n");

}

3. Insert Sort

In the set of numbers to sort, assuming that the number of front (n-1) [n>=2] is already in order, now you want to insert the nth number into the ordinal number in front so that n The number is also in the orderly order. This cycle is repeated until all the rows are in order.

The direct insert sort is stable. Algorithm time complexity O (n2)--[n squared ]

Main ()

{

int a[10],j,i,m;

printf ("This is a insert sort\n");

printf ("Please input the ten number you want to sort:");

for (i=0;i<10;i++)

scanf ("%d", &a[i]);

for (j=1;j<10;j++)

{

M=A[J];

for (i=j-1;i>=0;i--)

{

if (a[i]<m)

Break

Else

A[i+1]=a[i];

}

A[i+1]=m;

}

printf ("The correct order of those numbers is:");

for (i=0;i<10;i++)

printf ("%d", a[i]);

printf ("\ n");

}

4. Quick Sort

A quick sort is an essential improvement to the bubbling sort. Its basic idea is that the length of the sequencing sequence can be drastically reduced after a scan. In a bubbling sort, a scan can only ensure that the number of maximum values is moved to the correct position, while the length of the sequence to be sorted may be reduced by only 1. Quick sort through a scan, you can make sure that the number of points on the left is smaller than it, and the number on the right is larger than it. It then uses the same method to manipulate the left and right sides of the number until there is only one element to the left of the datum point.

It is obvious that the fast sort can be implemented recursively, and of course it can be implemented by using the stack to dissolve recursion.

Fast sequencing is not stable. Optimal condition algorithm time complexity O (nlog2n), worst o (n2)

Quick (int first,int end,int l[])

{

int Left=first,right=end,key;

Key=l[first];

while (Left<right)

{

while ((Left<right) && (L[right]>=key))

right--;

if (left<right)

L[left++]=l[right];

while ((Left<right) && (L[left]<=key))

left++;

if (left<right)

L[right--]=l[left];

}

L[left]=key;

return left;

}

Quick_sort (int l[],int first,int end)

{

int split;

if (End>first)

{

Split=quick (first,end,l);

Quick_sort (l,first,split-1);

Quick_sort (L,split+1,end);

}

}

Main ()

{

int a[10],i;

printf ("This is a quick sort\n");

printf ("Please input numbers for sort:");

for (i=0;i<10;i++)

scanf ("%d", &a[i]);

Quick_sort (a,0,9);

printf ("The correct sort of those numbers is:");

for (i=0;i<10;i++)

printf ("%d", a[i]);

printf ("\ n");

}

5. Hill Sort

D.l.shell This idea in the 1959 year in a sort algorithm named after his name. The algorithm first sorts the set of numbers by an increment D into groups, each group of records of the subscript difference D. sorts all the elements in each group, then uses a smaller increment to do it, and then sorts them in each group. When the increment is reduced to 1 , the entire number to be sorted is divided into a group, and the sort is completed.

The following function is an implementation of a hill sort algorithm, in which half of the first fetch sequence is incremental,

Halve each time later, until the increment is 1.

The hill sort is not stable.

void Shell_sort (int *x, int n)

{

int H, J, K, T;

for (H=N/2; h>0; h=h/2)/* control increment * /

{

for (j=h; j<n; j + +)/* This is actually the above direct insert sort * /

{

t = * (X+J);

for (k=j-h; (K>=0 && t<* (x+k)); K-=H)

{

* (X+K+H) = * (X+K);

}

* (x+k+h) = t;

}

}

}

6. Heap sequencing

Heap sorting is a sort of tree selection, which is an effective improvement on direct selection sorting. The heap is defined as follows: A sequence with n elements (h1,h2,..., hn), when and only if satisfied (hi>=h2i,hi>=2i+1 ) or (hi<=h2i,hi<=2i+1)(i=1,2,..., n/2)

is called a heap. Only the heap that satisfies the former condition is discussed here.

As can be seen from the definition of a heap, the top element of the heap (that is, the first element) must be the largest. A complete binary tree can

The structure of the heap is visually represented. Heap top is the root, the other is Zuozi, right subtree. The sequence of the numbers to be sorted is initially treated as a two-fork tree that is stored sequentially, adjusting their order of storage to become a heap, when the heap has the largest number of root nodes. The root node is then exchanged with the last node of the heap. The number of fronts (n-1) is then re-adjusted to make it a heap. And so on, until there are only two nodes of the heap, and exchange them, and finally get an ordered sequence of n nodes.

From the algorithm description, heap sequencing requires two processes, one is to build the heap, the other is the heap top and the last element of the heap

Swap location. So the heap sort has two functions. One is to build the seepage function of the heap, the second is to call the infiltration function repeatedly

A function that implements sorting. There is a maximum heap and a minimum heap of points.

Heap sequencing is not stable. Algorithm time complexity O (nlog2n).

Function: Infiltration Build heap

void Sift (int *x, int n, int s)

{

int T, K, J;

t = * (x+s); /* Staging start element * /

K = s; /* start element subscript * /

j = 2*k + 1; /* Right Sub-tree element subscript * /

while (J<n)

{

/* Determine if the condition of the heap is satisfied: meet on the next round to continue the comparison, otherwise adjust. */

if (j<n-1 && * (X+J) < * (x+j+1))

{

j + +;

}

if (t<* (X+J))/* Adjust * /

{

* (X+K) = * (X+J);

K = J; /* After adjustment, the starting element is also adjusted * /

j = 2*k + 1;

}

else/* There is no need to adjust, it is already a heap, exiting the loop. */

{

Break

}

}

* (x+k) = t; /* start element put to its correct position * /

}

Function: Heap Sort

void Heap_sort (int *x, int n)

{

int I, k, t;

int *p;

for (i=n/2-1; i>=0; i--)

{

Sift (x,n,i); /* Initial build heap * /

}

for (k=n-1; k>=1; k--)

{

t = * (x+0); /* heap top to last * /

* (x+0) = * (X+K);

* (x+k) = t;

Sift (x,k,0); /* The remaining number will be built again. *

}

}

Six Algorithms brocade set

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.