Introduction to Algorithms--linear time sorting (counting sort, cardinal sort, bucket sort)

Source: Internet
Author: User
Tags sorts

Linear time Sequencing

several of the sorts described earlier are able to be in the complexity of NLG (n) an algorithm that sorts n numbers in time, which are determined by comparison, which is called the comparison order. Some of the algorithms described below are sorted by operations, and their complexity is linear time.

——————————————————————————————————————

1. Counting Sort

The method of counting is to determine the number of elements that are less than x for each element x, so that you can know where the element x is in the output array.

An important property of the count ordering is that it is stable , that is, for the same two numbers, after sorting, it retains the order in which they are in the input array, which is the basis for the following cardinality ordering .

Although the complexity ratio between the algorithm is reduced, but in the implementation of the algorithm, it requires the input array data in an interval [0,k], because to go through the group, calculate the [0,k] each number in the input array number, this is counted as the disadvantage of sorting!
The following is the debugging program, can be run directly, the detailed process to see the introduction of the algorithm

#include <stdio. h> #define  K                   //data between [0,k]  int a[]={2,7,3,5,3,2,9};int b[20]={0};                  Output array int c[20]={0};                  Count Array int length=sizeof (A)/sizeof (a[0])-1;         void Count_sort (int *a,int *b,int k) {   int J;   for (j=0;j<=length;j++)       //For each number count   {   c[a[j]]=c[a[j]]+1;   }   for (j=1;j<=k;j++)           //calculates how many elements are less than or equal to J   {   c[j]=c[j]+c[j-1];   }   for (j=length;j>=0;j--)      //Flashback output array, ensuring that the data is stable     {       b[c[a[j]]]=a[j];       c[a[j]]=c[a[j]]-1;     A[J] Output, the corresponding count array element minus 1.   }} int main () {    int i; Count_sort (a,b,k); for (i=1;i<=length+1;i++) {printf ("%3d", B[i]);} printf ("\ n"); return 0;}

———————————————————————————————————

2. Base sorting   basic idea: sorting the data of n D-Bits, unlike the traditional idea, it starts with the lowest-significant bit first.     

It is important to ensure that each order is stable, that is, the same data, the order of the output must be the same as the order of input.


Implementation routines:
#include <stdio.    h> #include <string.h>int a[]={329,457,657,839,436,720,355};    Array to sort int length=sizeof (A)/sizeof (a[0]) -1;void count_sort (int *a2,int *b)//Count sort {int J;                 int c[10]={0}; Count array, number between [0,9] for (j=0;j<=length;j++)//For each number of {c[a2[j]]=c[a2[j]]+1;} for (j=1;j<=10;j++)//Calculate how many elements are less than or equal to j{c[j]=c[j]+c[j-1];}       for (j=length;j>=0;j--)//Flashback output array, ensuring that the data is stable {b[c[a2[j]]]=a[j]; Reference C[a2[j]], the size of the array a[j] output c[a2[j]]=c[a2[j]]-1;}}               void Radix_sort (int *a,int d) {int i,j,k,temp;int a2[10]={0};                  Store each bit int b[20]={0};   Output array for (i=1;i<=d;i++) {for (j=0;j<=length;j++) {temp=a[j];    K=i;   while (k>1) {TEMP=TEMP/10; k--;}    a2[j]=temp%10;       Take the specified bit to a2[j], wait for the sort} count_sort (a2,b);      memcpy (A,&b[1], (length+1));   }}int Main () {int J;   Radix_sort (a,3);   for (j=0;j<=length;j++) {printf ("%5d\n", A[j]); }}

———————————————————————————————————————————————————————————————————————————
3. Sorting Bucketsbucket sequencing is the assumption that the input data is uniformly distributed and, on average, linear time. Assuming that the input data is generated by a random process, the process distributes the elements evenly and independently on the [0,1] interval. it divides the [0,1] interval into n equal-sized sub-ranges, called buckets, and then iterates through the input data into the specified bucket (in the case of a bucket, with a linked list inserted into the sorting process). Finally, the data in each bucket can be exported sequentially.

Routines :
#include <stdio. H> #include <stdlib. H>int a[]={78,17,39,26,72,94,21,12,23,68};//If the input data is evenly distributed within the [0,99] interval int Length = sizeof (A)/sizeof (a[0]) -1;typedef   struct Node//list cell structure {int num; struct Node *next;} Node;       Node *bucket[10]={0};   Divided into 10 barrels, i.e. 10 inter-cell void bucket_sort (int *a) {int i;   int A;   Node * TEMP=NULL,*PRE_TEMP=NULL;   for (i=0;i<=length;i++)//Traversal input array, put into the specified bucket {a = (int) (A[I]/10);  if (bucket[a] = = 0) {bucket[a]= (node *) malloc (sizeof (node));  bucket[a]->num=a[i];   bucket[a]->next=null;   } else//insert sort {Temp=pre_temp=bucket[a] for non-empty list;              while (A[i] > Temp->num) {pre_temp=temp;  temp=temp->next;   if (temp==null) break;   if (temp = = NULL)//insert to the last location {temp= (node *) malloc (sizeof (node));   temp->num=a[i];   temp->next=null;   pre_temp->next=temp; } else if (temp = = Bucket[a])//INSERT into first position {temp= (Node *) malloc (sizEOF (Node));   temp->num=a[i];   temp->next=bucket[a];   Bucket[a]=temp;   } else//INSERT into middle position {temp= (node *) malloc (sizeof (node));   temp->num=a[i];   temp->next=pre_temp->next;       pre_temp->next=temp;     }}}}void free_list (node * head)//release list structure memory {node *n=head;     while (head!=null) {n=head->next; free (head); head=n;    }}int Main () {Node * Temp=null;int i=1;    Bucket_sort (A); for (i=0;i<=9;i++)//Output the data in each bucket sequentially {temp=bucket[i];while (temp!=null) {printf ("%3d\n", Temp->num);temp=temp-> Next;}  Free_list (Bucket[i]); Release the memory of the bucket I}return 0;}




Introduction to Algorithms--linear time sorting (counting sort, cardinal sort, bucket sort)

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.