One, bucket sort
The following code goes from: Bucket sorting
[CPP]View Plaincopy
- #include <iostream>
- #include <list>
- using namespace std;
- struct Node
- {
- Double value;
- Node *next;
- };
- //Bucket sequencing main program
- void Bucketsort (double* arr, int length)
- {
- Node KEY[10];
- int number = 0;
- Node *p, *q; //Insert Node TEMP variable
- int counter = 0;
- For (int i = 0; i <; i++)
- {
- Key[i].value = 0;
- Key[i].next = NULL;
- }
- For (int i = 0; i < length; i++)
- {
- Node *insert = new node ();
- Insert->value = Arr[i];
- Insert->next = NULL;
- Number = arr[i] * 10;
- if (Key[number].next = = NULL)
- {
- Key[number].next = insert;
- }
- Else
- {
- p = &key[number];
- Q = key[number].next;
- While ((q! = NULL) && (Q->value <= arr[i]))
- {
- Q = q->next;
- p = p->next;
- }
- Insert->next = q;
- P->next = insert;
- }
- }
- For (int i = 0; i <; i++)
- {
- p = key[i].next;
- if (p = = NULL)
- continue;
- While (P! = NULL)
- {
- arr[counter++] = p->value;
- p = p->next;
- }
- }
- }
- int main ()
- {
- Double a[] = {0.78, 0.17, 0.39, 0.26, 0.72, 0.94, 0.21, 0.12, 0.23, 0.68};
- Bucketsort (A, 10);
- For (int i = 0; i <; i++)
- {
- cout << A[i] << "";
- }
- cout << Endl;
- return 0;
- }
"Example" to a mix of 52 playing cards by the number of a<2<...<j<q<k sort, you need to set 13 "box", sorting in turn each card by the number of points into the corresponding box, and then in turn these boxes end-to-end, and then get the number of points ascending order of a pair
In general, how many keywords in each box are stored in the same record is unpredictable, so the type of box should be designed as a linked list is appropriate.
[Data structure]--bucket sequencing