Animation comparison and algorithm summary of various sorting algorithms implemented by HTML5

Source: Internet
Author: User

Animation comparison of various sorting algorithms implemented by HTML5

http://www.webhek.com/misc/comparison-sort/

Comparison of efficiency of several sorting algorithms source: http://blog.chinaunix.net/uid-20773165-id-1847742.html

1. Stability comparison

Insert sort, bubble sort, binary tree sort, two-way merge sort, and other linear sort are stable

Choose Sort, hill sort, quick sort, heap sort is unstable

2. Time complexity Comparison

Insert sort, bubble sort, select sort time complexity O (n2)

Other non-linear sequencing time complexity is O (nlog2n)

The time complexity of linear sorting is O (n);

3. Comparison of auxiliary spaces

The auxiliary space of linear sort, two-way merge sort is O (n), the auxiliary space of other sort is O (1);

4. Other comparisons

Insert, bubble sort is slower, but this sort can reach a faster speed when the sequence in which it is ordered is local or overall.

In this case, instead, the fast sort is slower.

When n is small, the stability is not required to be ordered with the choice of ordering, the stability of the time required to use the insertion or bubble sort.

If the keywords of the records to be sorted are within a significantly limited range, and the space allows to be sorted by buckets.

When n is large, the keyword elements are more random, and the stability is not required to be ordered quickly.

When n is large, the keyword element may appear to be orderly in itself, when there is a requirement for stability, where space is allowed.

It is advisable to sort by merge.

When n is large, the keyword elements may appear to be ordered in their own right, and stability is not required to be sorted by heap.

*************************************************************************************


Relive the classical sort thought--c the general solution of the common sort of language

/*
=============================================================================
Introduction to Knowledge (all definitions are intended only to help the reader understand the relevant concepts, not strictly defined):
1. Stable sequencing and unstable sequencing

Simply put, all of the equal numbers, after some sort of sorting method, can still keep their relative order before they are sorted, and we
That this sort of method is stable. Conversely, it is non-stable.
For example: A group of numbers before the sort is a1,a2,a3,a4,a5, where a2=a4, after some sort of a1,a2,a4,a3,a5,
Then we say that this sort is stable, because the A2 sort before the A4, and it is in front of the A4 after sorting. If it becomes a1,a4,
The A2,A3,A5 is not stable.

2. internal and external sorting

In the sorting process, all the numbers that need to be sorted are in memory, and the memory is adjusted to their storage order, called inner ordering;
During the sorting process, only the partial number is transferred into memory, and the memory-adjusted number of stored-order methods in the storage are called outer ordering.

3. Time complexity and space complexity of the algorithm

The time complexity of the algorithm refers to the computational effort required to perform the algorithm.
The spatial complexity of an algorithm generally refers to the memory space required to execute the algorithm.
================================================================================
*/
/*
================================================
Function: Select sort
Input: array name (i.e. array header address), number of elements in array
================================================
*/
/*
====================================================
The algorithm idea is simple to describe:

In the set of numbers to sort, select the smallest number to exchange with the first position;
Then in the rest of the number to find the smallest and second position of the number exchange, so loop
To the second and last number to compare.

Choosing a sort is not stable. Algorithm complexity O (n2)--[n squared]
=====================================================
*/
void Select_sort (int *x, int n)
{
int I, J, Min, T;

for (i=0; i<n-1; i++)/* To select the number of times: 0~n-2 total n-1 times */
{
min = i; /* Assume that the current subscript is the minimum number of I, and then adjust */
for (j=i+1; j<n; j + +)/* loop to find out which subscript is the smallest number */
{
if (* (X+J) < * (x+min))
{
min = j; /* If the subsequent number is smaller than the previous, write down its subscript */
}
}

if (min! = i)/* If min is changed in the loop, you need to exchange the data */
{
t = * (x+i);
* (x+i) = * (X+min);
* (x+min) = t;
}
}
}


/*
================================================
Function: Direct Insert sort
Input: array name (i.e. array header address), number of elements in array
================================================
*/
/*
====================================================
The algorithm idea is simple to describe:

In the set of numbers to sort, assume that the number of front (n-1) [n>=2] is already a row
In a good order, now you want to insert the nth number into the ordinal number in front, so that the n number
It's also in the right 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]
=====================================================
*/
void Insert_sort (int *x, int n)
{
int I, j, T;

for (I=1; i<n; i++)/* To select the number of times: 1~n-1 total n-1 times */
{
/*
The number of the staging subscript I. Note: The subscript starts at 1, the reason is the start
The first number is the number labeled 0, there is no number in front of, alone, think
It's in the right order.
*/
t=* (X+i);
for (j=i-1; j>=0 && t<* (x+j); j--)/* Note: j=i-1,j--, here is the number of subscript I, in front of which there is a sequence to find the insertion position. */
{
* (x+j+1) = * (X+J); /* If conditions are met, move backwards. The worst case scenario is that T is smaller than the number labeled 0, which is placed at the front, j==-1, exit loop */
}

* (x+j+1) = t; /* Locate the placement position of the number labeled I */
}
}


/*
================================================
Function: Bubble sort
Input: array name (i.e. array header address), number of elements in array
================================================
*/
/*
====================================================
The algorithm idea is simple to describe:

In the set of numbers to be sorted, all the numbers in the range that are not currently sequenced, from the top
And the next pair of adjacent two numbers in turn to compare and adjust, so that the larger number to sink, more
Small to go up. That is, each time a comparison of two adjacent numbers is found, they are sorted and sorted to
Instead, they are exchanged.

The following is an improved bubbling algorithm that records the last number of sinks after each scan
Position k, which reduces the number of times the outer loop is scanned.

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

void bubble_sort (int *x, int n)
{
Int J, K, H, T;
  
for (h=n-1; h>0; h=k)/* Loop to no comparison range */
{
   for (j=0, k=0; j    {
    if (* (X+J) > * (x+j+1))/* Large in the back, small put to front */
     {
     t = * (X+J);
     * (X+J) = * (x+j+1);
     * (x+j+1) = t;/* Complete exchange */
     k = j;/* Save last sinking position. So that all the k after the sort is arranged. */
   }
  }
}
}


/*
================================================
Features: Hill sort
Input: array name (i.e. array header address), number of elements in array
================================================
*/
/*
====================================================
The algorithm idea is simple to describe:

In the direct insertion sorting algorithm, inserting one number at a time causes the ordered sequence to increment by only 1 nodes,
And does not provide any help in inserting the next number. If you compare distant distances (called
increments), so that when a number moves across multiple elements, a comparison can eliminate
Multiple element exchanges. D.l.shell was implemented in 1959 in a sorting algorithm named after his name.
This idea. The algorithm first sorts the set of numbers by an increment d into groups, in each group
The subscript difference of the record D. Sort all the elements in each group and then use a smaller increment
It's done, and then sorted in each group. When the increment is reduced to 1 o'clock, the entire number to be sorted is divided into
A group, sorting complete.

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;
}
}
}


/*
================================================
Function: Quick Sort
Input: Array name (that is, array header address), subscript of starting and ending elements in array
================================================
*/
/*
====================================================
The algorithm idea is simple to describe:

A quick sort is an essential improvement to the bubbling sort. Its basic idea is to pass a trip
After the scan, the length of the sorting sequence can be greatly reduced. In a bubbling sort, one
Scanning only ensures that the number of maximum values is moved to the correct position, while the length of the sequence to be sorted may only
Reduced by 1. Quick sort by a scan, you can make sure that you have a number (it's the benchmark)
The number on the left is smaller than it, and the number on the right is bigger than it. And then, in the same way,
It is left and right on both sides until there is only one element to the left and right of the datum point. It is made up of
C.a.r.hoare was introduced in 1962.

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. The following
Functions are implemented recursively, and interested friends can be changed to non-recursive.

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

=====================================================
*/
void Quick_sort (int *x, int low, int. high)
{
int I, j, T;

if (Low < high)///The element to be sorted starts and ends, keep the small on the left and large on the right. Here the following elements marked Low are datum points */
{
i = low;
j = high;
t = * (X+low); /* Number of staging datum points */

while (I<J)/* Cycle Scan */
{
while (i<j && * (x+j) >t)/* On the right side is still on the right as long as it is larger than the reference point */
{
j--; /* Move forward one position */
}

if (I<J)
{
* (x+i) = * (X+J); /* Loop exit above: That is, a number smaller than the Datum point, the number of replacement datum points */
i++; /* Move back one position and use this as the datum point */
}

while (i<j && * (x+i) <=t)/* On the left as long as the less than equals datum point remains on the left */
{
i++; /* Move back one position */
}

if (I<J)
{
* (X+J) = * (X+i); /* Loop exit above: That is, a number larger than the datum point, put to the right */
j--; /* Move forward one position */
}
}

* (x+i) = t; /* Once the scan is over, place it in place */
Quick_sort (x,low,i-1); /* Perform a quick sort on the number to the left of the datum */
Quick_sort (X,i+1,high); /* Perform a quick sort on the number to the right of the datum points */
}
}


/*
================================================
Function: Heap Sort
Input: array name (i.e. array header address), number of elements in array
================================================
*/
/*
====================================================
The algorithm idea is simple to describe:

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
Meet (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 stored sequentially, adjusting their order of storage,
Make it a heap, at which point the heap has the largest number of root nodes. Then the root node and the last node of the heap
Exchange. The number of fronts (n-1) is then re-adjusted to make it a heap. And so on, until there are only two nodes
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.

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

*/
/*
Function: Infiltration Build heap
Input: Array name (that is, the first address of the array), the number of elements involved in building the heap, starting from the first few elements
*/
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)
{
if (j<n-1 && * (X+J) < * (x+j+1))/* Determine if the condition of the heap is satisfied: meet on the next round of comparisons, otherwise adjust. */
{
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
Input: array name (i.e. array header address), number of elements in array
*/
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. *
}
}


void Main ()
{
#define MAX 4
int *p, I, A[max];

/* Input TEST data */
p = A;
printf ("Input%d number for sorting: \ n", MAX);
for (i=0; i<max; i++)
{
scanf ("%d", p++);
}
printf ("\ n");

/* Test selection Sort */


p = A;
Select_sort (P,max);
/**/


/* Test Direct Insert Sort */

/*
p = A;
Insert_sort (P,max);
*/


/* Test bubble sort */

/*
p = A;
Insert_sort (P,max);
*/

/* Test Quick sort */

/*
p = A;
Quick_sort (p,0,max-1);
*/

/* Test heap sequencing */

/*
p = A;
Heap_sort (P,max);
*/

For (P=a, i=0; i<max; i++)
{
printf ("%d", *p++);
}

printf ("\ n");
System ("pause");
}

Programmers must know the 8 big sort and 3 big find

Http://www.cr173.com/html/15301_1.html

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.