Base sort using C ++

Source: Internet
Author: User

There is an integer linked list. The table element's key value is an integer of no more than three digits. A Indicates the hundred bits of the key value, B indicates the ten bits, and C indicates the single bits. First, the single-digit C in the key value splits and links the linked list. First, the linked list is split into up to 10 queue linked lists, then, the chain lists of the ten queues after the split are re-collected into a chain list in the order of 0 to 9 in the C value. Then, perform the same split and link operations on the key values B and A, and the linked list is sorted from small to large. If a linked list is followed by their key values and their table meta links are:
35,298,832,932, 38,635, 48,118,128,138,148,158

// Binsort. cpp: defines the entry point for the console application.
//

# Include "stdafx. H"
# Include <stdio. h>
# Include <stdlib. h>

# Define keyn 3
Int inputnumber [] = {35,298,832,932, 38,635, 48,118,128,138,148,158 };
# Define n sizeof inputnumber/sizeof inputnumber [0]

Struct Element
{
Int key;
Struct element * next;
};

Void basesort (struct element ** H)
{
Int I, j, factor = 1;
Struct element * linklist [10], * tailoflinklist [10], * P, * U;

For (I = 0, P = * h; I <keyn; factor * = 10, I ++)
{
For (j = 0; j <10; j ++)
{
Linklist [J] = NULL;
}

While (P)
{
U = p-> next;
J = (p-> key/factor) % 10;

If (linklist [J] = NULL)
{
Linklist [J] = P;
}
Else
{
Tailoflinklist [J]-> next = P;
}

Tailoflinklist [J] = P;
P-> next = NULL;
P = u;
}

P = NULL;

For (j = 0; j <10; j ++)
{
If (linklist [J] = NULL)
{
Continue;
}

If (P = NULL)
{
P = linklist [J];
}
Else
{
U-> next = linklist [J];
}

U = tailoflinklist [J];
}
}

* H = P;
}

Void main ()
{
Struct element * H, * U;
Int I;
H = NULL;

For (I = 0; I <n; I ++)
{
U = (struct element *) malloc (sizeof (struct element ));
U-> key = inputnumber [I];
U-> next = h;
H = u;
}

Basesort (& H );

For (u = H; U; u = u-> next)
{
Printf ("% 4D", U-> key );
}

Scanf ("");
}

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.