[Data structure]--bucket sequencing

Source: Internet
Author: User

One, bucket sort

The following code goes from: Bucket sorting

[CPP]View Plaincopy
  1. #include <iostream>
  2. #include <list>
  3. using namespace std;
  4. struct Node
  5. {
  6. Double value;
  7. Node *next;
  8. };
  9. //Bucket sequencing main program
  10. void Bucketsort (double* arr, int length)
  11. {
  12. Node KEY[10];
  13. int number = 0;
  14. Node *p, *q; //Insert Node TEMP variable
  15. int counter = 0;
  16. For (int i = 0; i <; i++)
  17. {
  18. Key[i].value = 0;
  19. Key[i].next = NULL;
  20. }
  21. For (int i = 0; i < length; i++)
  22. {
  23. Node *insert = new node ();
  24. Insert->value = Arr[i];
  25. Insert->next = NULL;
  26. Number = arr[i] * 10;
  27. if (Key[number].next = = NULL)
  28. {
  29. Key[number].next = insert;
  30. }
  31. Else
  32. {
  33. p = &key[number];
  34. Q = key[number].next;
  35. While ((q! = NULL) && (Q->value <= arr[i]))
  36. {
  37. Q = q->next;
  38. p = p->next;
  39. }
  40. Insert->next = q;
  41. P->next = insert;
  42. }
  43. }
  44. For (int i = 0; i <; i++)
  45. {
  46. p = key[i].next;
  47. if (p = = NULL)
  48. continue;
  49. While (P! = NULL)
  50. {
  51. arr[counter++] = p->value;
  52. p = p->next;
  53. }
  54. }
  55. }
  56. int main ()
  57. {
  58. Double a[] = {0.78, 0.17, 0.39, 0.26, 0.72, 0.94, 0.21, 0.12, 0.23, 0.68};
  59. Bucketsort (A, 10);
  60. For (int i = 0; i <; i++)
  61. {
  62. cout << A[i] << "";
  63. }
  64. cout << Endl;
  65. return 0;
  66. }

"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

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.