Database entry-level algorithm [2]

Source: Internet
Author: User

In the previous article "database entry-level algorithm 1", we introduced some data algorithms. Now we will continue to introduce some basic sorting algorithms.

Bubble Sorting

Usage conditions:The element of the set can be compared to the size.

Algorithm idea:Scan the records to be sorted continuously. The minimum record is found every time it is scanned to bring it closer to the top. Because a record is placed in the correct position at the end of each scan, you do not need to re-check the record for the next scan.

Example programming:Int B [10] = {77,1, 65,13, 81,93,} Sort it by bubble (here I am confused about the concept, thanks to zdd)

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

Performance analysis:Time complexity O (n ^ 2)

Hill sorting

Usage conditions:The element of the set can be compared to the size.

Algorithm idea:First, the entire sequence of records to be sorted is divided into several sub-sequences for direct insertion and sorting. When the records in the whole sequence are "basic order", all records are directly inserted and sorted at a time. Subsequences are not simply "segmented by segments", but are separated by a "incremental" record to form a subsequence. Therefore, records with small keywords are not moved forward step by step, but are moved incrementally by step. This increment shows a decreasing trend, and the last increment is always 1, at this time, the sequence is basically ordered, as long as a few comparisons and moves to complete the sorting. Hill sorting is hard to grasp the incremental setting. Generally, we assume that the "increment" value is 4, 2, and 1. This is the general setting of hill sorting ). So here I want to draw up a formula for "increment" h (n + 1) = 3 * h (n) + 1, h> N/9) this formula may not be the most appropriate, but it is applicable to the general "increment" setting. If the number is 8, the increment here is 1.

Example programming:Int B [10] = {,} Sort its hill

// You need to select an appropriate method for self-incremental sorting by hill.

 
 
  1. Void ShellSort (int B [10])
  2. {
  3. Int h, I;
  4. Int n = 10;
  5. // This loop is used to calculate the increment values 1 and 4.
  6. For (h = 1; h <= n/9; h = 3 * h + 1 );
  7. // Incremental Loop
  8. For (; h> 0; h/= 3)
  9. {
  10. For (I = h; I
  11. {
  12. Int j, temp;
  13. Temp = B [I];
  14. // Insert sorting
  15. For (j = I-h; j> = 0; j = j-h)
  16. {
  17. If (B [j]> temp)
  18. {
  19. B [j + h] = B [j];
  20. }
  21. Else
  22. {
  23. Break;
  24. }
  25. }
  26. B [j + h] = temp;
  27. }
  28. }
  29. Cout <"the sort is :";
  30. For (int I = 0; I <10; I ++)
  31. {
  32. Cout <
  33. }
  34. Cout <
  35. }

Performance analysis:Time complexity is a little complicated for hill sorting. It varies according to the specific "increment". Here I use the O (n ^ 3/2) of Yan Weimin's "Data Structure)

Quick sorting

Usage conditions:A set of comparable sizes.

Algorithm idea:Sort the records to be sorted into two separate parts. If the keywords of one part of the records are smaller than those of the other, you can sort these two parts separately, finally, an ordered sequence is reached. The key point here is to select the "benchmark" for segmentation ". It must be more than this "benchmark" into a part, less than this "benchmark" into a part. Here, the first record of this part is taken as the "benchmark" by default ".

Example programming:Int B [10] =}

 
 
  1. // Quick sorting
  2. Void QuickSort (int * B, int low, int high)
  3. {
  4. // Exchange functions
  5. Void Sawp (int * a, int * B );
  6. Int Old_low = low;
  7. Int Old_high = high;
  8. While (low
  9. {
  10. While (* (B + high)> * (B + low) & low
  11. Sawp (B + low, B + high );
  12. While (* (B + low) <* (B + high) & low
  13. Sawp (B + low, B + high );
  14. }
  15. If (Old_low
  16. {
  17. QuickSort (B, Old_low, low-1 );
  18. }
  19. If (high + 1
  20. {
  21. QuickSort (B, high + 1, Old_high );
  22. }
  23. }
  24. // Exchange functions
  25. Void Sawp (int * a, int * B)
  26. {
  27. Int temp;
  28. Temp = *;
  29. * A = * B;
  30. * B = temp;
  31. }

Performance analysis:Time complexity O (nlogn)

At this point, we have introduced common basic data search and sorting algorithms, which are the most basic algorithms that can be extended by them.

Related Article

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.