Base Sorting Algorithm

Source: Internet
Author: User

/* Exp11-8.cpp */
# Include <stdio. h>
# Include <malloc. h>
# Include <string. h>
# Define MaxE 20/* Maximum number of elements in a linear table */
# Define maxr 10/* maximum value of the base */
# Define maxd 8/* maximum value of the keyword multiple */
Typedef struct Node
{
Char data [maxd];/* string defined by the keyword of the record */
Struct node * next;
} Rectype;
Void crealink (rectype * & P, char * A [], int N );
Void displink (rectype * P );
Void radixsort (rectype * & P, int R, int d)/* implement base sorting. * P is the pointer to the List table to be sorted, r is the base, and D is the number of keywords */
{
Rectype * head [maxr], * tail [maxr], * t;/* define the start and end pointers of each chain */
Int I, J, K;
For (I = D-1; I> = 0; I --)/* cycle from low to high */
{
For (j = 0; j <r; j ++)/* initialize the first and last pointers of each chain */
Head [J] = tail [J] = NULL;
While (P! = NULL)/* for each node loop in the original linked list */
{
K = p-> data [I]-'0';/* Find the K leader */
If (head [k] = NULL)/* allocate */
{
Head [k] = P;
Tail [k] = P;
}
Else
{
Tail [k]-> next = P;
Tail [k] = P;
}
P = p-> next;/* Get the next element to be sorted */
}
P = NULL;
For (j = 0; j <r; j ++)/* for each chain loop */
If (head [J]! = NULL)/* collect */
{
If (P = NULL)
{
P = head [J];
T = tail [J];
}
Else
{
T-> next = head [J];
T = tail [J];
}
}
T-> next = NULL;/* set the next field of the last node to null */
Printf ("sort by % d bits", d-i-1); displink (P );
}
}

Void crealink (rectype * & P, char * A [], int N)/* generate a linked list using the post-Insertion Method */
{
Int I;
Rectype * s, * t;
For (I = 0; I <n; I ++)
{
S = (rectype *) malloc (sizeof (rectype ));
Strcpy (S-> data, a [I]);
If (I = 0)
{
P = s;
T = s;
}
Else
{
T-> next = s;
T = s;
}
}
T-> next = NULL;
}

Void displink (rectype * P)/* output linked list */
{
While (P! = NULL)
{
Printf ("% s", p-> data );
P = p-> next;
}
Printf ("\ n ");
}

Void main ()
{
Int n = 10;
Rectype * P;
Char * A [] = {"75", "23", "98", "44", "57", "12", "29", "64 ", "38", "82 "};
Crealink (P, A, N );
Printf ("\ n ");
Printf ("Initial keyword ");
Displink (p);/* output initial keyword */
Radixsort (p, 10, 2 );
Printf ("final result ");
Displink (p);/* output the final result */
Printf ("\ n ");
}

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.