C + + count sort detailed _c language

Source: Internet
Author: User

The counting sort differs from the comparison sort, is based on the counting method, and for counting, suppose each input is an integer between 0~k. For each INPUT element x, determine the number of elements less than x. If there are 17 elements less than x, then x is the 18th output position.
Counting sort involves three arrays a[0.....length-1],length the length of an array A, array B is equal to the length of array A, storing the result of the final order; c[0. K] holds the number of each element in a, K is the maximum value in array a.

int count_k (int a[],int length), which determines the length of the C array in order to determine the largest element in array A.

int count_k (int a[],int length)
{
  int j,max;
  max = a[0];

  for (j=1;j<=length-1;j++)
  {
    if (a[j]>=max)
      max = A[j];
  }

  return max;
}

Implementation of Count Sort:

void Count_sort (int a[],int b[],int k)
{
  int *c = (int *) malloc ((k+1) * sizeof (int));
  int i,j;
  for (i=0;i<=k;i++)//Initialize array C
    c[i]=0;

  for (j=0;j<=length-1;j++)//Calculate the number of elements in a
    c[a[j] = c[a[j]]+1;
  for (i=1;i<=k;i++)//To calculate the number of elements less than or equal to C[i]
    c[i] = C[i] + c[i-1];
  for (j=length-1;j>=0;j--)
  {
    int k=c[a[j]]-1;
    B[K] = a[j];
    C[A[J]] = C[a[j]]-1;

  }

  Free (C);

}

Count_sort (A,B,K);

K=5

for (j=0;j<=length-1;j++)//Calculate the number of elements in a    
c[a[j] = c[a[j]]+1;

Represents an array of 2 0, 0 1, 2 2, 3 3, 0 4, and 5

  for (i=1;i<=k;i++)//To calculate the number of elements less than or equal to C[i]    
c[i] = C[i] + c[i-1];

A number less than or equal to 0 has two, a number less than or equal to 1 has two, 2 is less than 4, 3 is less than or equal to 7, 4 is less than or equal to 7, and 5

for (j=length-1;j>=0;j--)
  {
    int k=c[a[j]]-1;
    B[K] = a[j];
    C[A[J]] = C[a[j]]-1;

  }

For loop analysis is as follows

j=7; a[j]=a[7]=3; c[a[j]]=c[3]=7; c[a[j]]-1=6; b[c[a[j]]-1]=b[6]=a[j]=3; C[a[j]]=c[a[j]]-1=6

j=6; a[j]=a[6]=0; c[a[j]]=c[0]=2; C[a[j]]-1=1; b[c[a[j]]-1]=b[1]=a[j]=0; C[a[j]]=c[a[j]]-1=1

j=5; a[j]=a[5]=3; c[a[j]]=c[3]=6; c[a[j]]-1=5; b[c[a[j]]-1]=b[5]=a[j]=3; c[a[j]]=c[a[j]]-1=5;

j=4; a[j]=a[4]=2; c[a[j]]=c[2]=4; c[a[j]]-1=3; b[c[a[j]]-1]=b[3]=a[j]=2; c[a[j]]=c[a[j]]-1=3;

j=3; a[j]=a[3]=0; C[a[j]]=c[0]=1; c[a[j]]-1=0; b[c[a[j]]-1]=b[0]=a[j]=0; c[a[j]]=c[a[j]]-1=0;

j=2; a[j]=a[2]=3; c[a[j]]=c[3]=5; c[a[j]]-1=4; b[c[a[j]]-1]=b[4]=a[j]=3; c[a[j]]=c[a[j]]-1=4;

J=1; a[j]=a[1]=5; c[a[j]]=c[5]=8; c[a[j]]-1=7; b[c[a[j]]-1]=b[7]=a[j]=5; c[a[j]]=c[a[j]]-1=7;

j=0; a[j]=a[0]=2; c[a[j]]=c[2]=3; c[a[j]]-1=2; b[c[a[j]]-1]=b[2]=a[j]=2; c[a[j]]=c[a[j]]-1=2;

Count sort of last run screenshot

Count Sort analysis: j=length-1;j>=0;j– here for the reverse, is to ensure the stability of the order, this in the cardinal order has an important role.

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.