Database entry-level algorithms [3]

Source: Internet
Author: User

Previously, we followed the author to review the query algorithm and partial Sorting Algorithm in the data structure. Now we continue to follow the author to learn some basic sorting algorithms.

Select sort

Usage conditions:A set of comparable sizes.

Algorithm idea:Select the smallest or largest element from the data elements to be sorted, and place the elements in sequence at the end of the sorted series until all the data elements to be sorted are arranged.

Example programming:Int B [10] =}

 
 
  1. // Simple selection and sorting
  2. Void SimpleSelect (int B [10])
  3. {
  4. Int temp;
  5. Int I;
  6. For (I = 0; I <9; I ++)
  7. {
  8. For (int j = I + 1; j <9; j ++)
  9. {
  10. If (B [I]> B [j])
  11. {
  12. Temp = B [I];
  13. B [I] = B [j];
  14. B [j] = temp;
  15. }
  16. }
  17. }
  18. Cout <"the sort is :";
  19. For (int I = 0; I <10; I ++)
  20. {
  21. Cout <B [I] <"";
  22. }
  23. Cout <endl;
  24. }

Performance analysis:Time Complexity: On ^ 2)

Heap sorting

Usage conditions:A set of comparable sizes.

Algorithm idea:In fact, heap sorting is an evolution of Simple selection and sorting. It is mainly used to reduce the number of comparisons. What is heap? If the sequence is regarded as a Complete Binary Tree, the values of all non-terminal nodes in the full binary tree are not greater than or less than) The values of left and right child nodes can be called heap. By the nature of the heap, we can know that the top of the heap is the largest keyword (or the smallest keyword ). After the output heap top, the remaining elements are built into a heap and then output to the top. After repeated execution, we can get an ordered sequence, which is a heap sorting process.

Heap sorting involves two steps:

Example programming:Int B [10] =}

 
 
  1. // Heap sorting
  2. Void HeapSort (int B [10])
  3. {
  4. Void HeapAdjuest (int B [10], int min, int max );
  5. Void Sawp (int * a, int * B );
  6. Int I;
  7. // Because the binary tree is complete, heap conversion starts from the last non-leaf node.
  8. For (I = 9/2; I> = 0; I --)
  9. {
  10. HeapAdjuest (B, I, 9 );
  11. }
  12. // Sort the heap top data from the new heap
  13. For (I = 9; I> 0; I --)
  14. {
  15. Sawp (& B [I], & B [0]);
  16. HeapAdjuest (B, 0, I-1 );
  17. }
  18. }
  19. // Heap adjustment (large top heap)
  20. // Adjust the start position of the min data in the array.
  21. // Adjust the end position of the max data in the data
  22. Void HeapAdjuest (int B [10], int min, int max)
  23. {
  24. If (max <= min) return;
  25. Int temp;
  26. Temp = B [min];
  27. Int j;
  28. // Extend its child node cycle
  29. For (j = 2 * min; j <= max; j * = 2)
  30. {
  31. // Select its big child
  32. If (j <max & B [j] <B [j + 1])
  33. {
  34. J ++;
  35. }
  36. // The child with the heap top less than it will not handle it
  37. If (temp> B [j])
  38. {
  39. Break;
  40. }
  41. // Replace a large number with a small one
  42. B [min] = B [j];
  43. Min = j;
  44. }
  45. B [min] = temp;
  46. }
  47. // Exchange functions
  48. Void Sawp (int * a, int * B)
  49. {
  50. Int temp;
  51. Temp = *;
  52. * A = * B;
  53. * B = temp;
  54. }

✓ Performance analysis:Time complexity O (nlogn)

The merge algorithm is also called the 2-way merge algorithm.

Usage conditions:A set of comparable sizes.

Algorithm idea:Assuming that the initial sequence contains n records, it can be considered as n ordered subsequences. The length of each subsequence is 1, and then the two subsequences are merged, we can get [n/2] characters with a length of 2 or 1. Here the length is 1. Maybe the sequence length is an odd number. Then the last sequence is placed in a single order, so the length is 1 ); merge them in pairs, so repeated until an ordered sequence with a length of n is obtained.

Example programming:Int B [10] =}

 
 
  1. // Merge and sort
  2. Void MergeSort (int B [10], int d [10], int min, int max)
  3. {
  4. // Use and store the sequence obtained from the intermediate shard Region
  5. Int c [10];
  6. Void Merge (int c [10], int d [10], int min, int mid, int max );
  7. If (min = max) d [min] = B [min];
  8. Else
  9. {
  10. // Divide the image into two regions.
  11. Int mid = (min + max)/2;
  12. // Merge and sort the region
  13. MergeSort (B, c, min, mid );
  14. // Merge and sort the region
  15. MergeSort (B, c, mid + 1, max );
  16. // Merge two regions
  17. Merge (c, d, min, mid, max );
  18. }
  19. }
  20. // Merge the ordered sequence d [min-mid] And d [mid + 1-max] into the ordered sequence c [min-max]
  21. Void Merge (int c [10], int d [10], int min, int mid, int max)
  22. {
  23. Int I, j, k;
  24. For (I = j = min, k = mid + 1; j <= mid & k <= max; I ++)
  25. {
  26. If (c [j]> c [k])
  27. {
  28. D [I] = c [k];
  29. K ++;
  30. }
  31. Else
  32. {
  33. D [I] = c [j];
  34. J ++;
  35. }
  36. }
  37. If (j <= mid)
  38. {
  39. For (; j <= mid; j ++, I ++)
  40. {
  41. D [I] = c [j];
  42. }
  43. }
  44. If (k <= max)
  45. {
  46. For (; k <= max; k ++, I ++)
  47. {
  48. D [I] = c [k];
  49. }
  50. }
  51. }

Performance analysis:Time complexity O (nlogn)

Summary

Because different sorting methods adapt to different application changes and requirements, the following factors are taken into account when selecting an appropriate sorting method:

So when will so many sorting algorithms be used?

IfN is relatively small, for example, n <= 50 ),AvailableInsert sorting directly or simply select sorting.

IfThe initial sequence state is basically ordered., OptionalInsert sort directly, bubble sort.

IfN price comparisonThe time complexity is O (nlogn:Fast sorting, heap sorting, and Merge Sorting.

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.