Easily master the basic programming algorithms (Part 2)

Easily master the basic programming algorithms (Part 2)

[[121970]]

Before writing this article, I want to talk about the basic knowledge of programmers. Many people are talking about their work experience or giving advice to graduates. I agree with their advice to students to lay a good foundation in computer in school. How can you build a building without a good foundation? With some basic knowledge, it will be twice as effective to learn deep theories. If you encounter deep theories first and then learn related foundations, it will be twice as effective. Maybe many students will say that many companies now recruit people who can get started directly. First of all, I want to say that such companies must be small companies with short-sightedness. They can't find very good talents. Even if they go, such talents will not stay for a long time, because such companies have no vision for development, and technical talents may change jobs because of no development prospects. Secondly, I want to say that if you have a good computer foundation, I believe you can successfully learn and adapt to meet the technical requirements of the company within three months.

In fact, what I want to say is that you should never ignore the basics. Without further ado, let's go straight to the basic sorting algorithm.

Basic Programming Algorithms (I)

Basic Programming Algorithms (II)

Basic Programming Algorithms (III)

Bubble Sort

Conditions of use: The elements of the collection can be compared in size

Algorithm idea: Continuously scan the records to be sorted. Each scan will find the minimum record and bring it closer to the top. Since each scan will place a record in its final and most correct position, the next scan does not need to recheck this record.

Example programming: int b[10]={77,1,65,13,81,93,10,5,23,17} will be bubble sorted (I have confused the concepts here, thanks to zdd for pointing it out)

  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<i;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<<b[i]<< " " ;
  22. }
  23. cout<<endl;
  24. }

Performance analysis: time complexity O(n^2)

Shell sort

Conditions of use: The elements of the collection can be compared in size

Algorithm idea: First, divide the entire sequence of records to be sorted into several subsequences and perform direct insertion sorting on each of them. When the records in the entire sequence are "basically ordered", perform direct insertion sorting on all records. The subsequence is not simply "divided piece by piece", but a subsequence is composed of records separated by a certain "increment". Therefore, when comparing and sorting, the records with smaller keywords do not move forward step by step, but move at a certain increment. The "increment" shows a decreasing trend. In the end, this "increment" is always 1. At this time, the sequence is basically ordered, and only a small number of comparisons and moves are required to complete the sorting. It is difficult to grasp the setting of increments in Shell sorting. Generally, for 8 numbers, we think that the "increment" is set to: 4, 2, 1. (This is the general setting of Shell sorting). So the author here is going to formulate a formula for "increment" h(n+1)=3*h(n)+1, (stop when h>N/9) This formula may not choose the most appropriate increment, but it is applicable to the general "increment" setting. If it is 8 numbers, then the increment here is 1.

Example programming: int b[10]={77,1,65,13,81,93,10,5,23,17} sorts its shell

  1. //Hill sort auto-increment needs to be selected by yourself  
  2. void ShellSort( int b[10])
  3. {
  4. int h,i;
  5. int n=10;
  6. //Through this loop, we calculate the increments to 1 and 4  
  7. for (h=1;h<=n/9;h=3*h+1);
  8.  
  9. //Increment loop  
  10. for (;h>0;h/=3)
  11. {
  12. for (i=h;i<n;i++)
  13. {
  14. int j,temp;
  15. temp=b[i];
  16. //Insert sort  
  17. for (j=ih;j>=0;j=jh)
  18. {
  19. if (b[j]>temp)
  20. {
  21. b[j+h]=b[j];
  22. }
  23. else  
  24. {
  25. break ;
  26. }
  27. }
  28. b[j+h]=temp;
  29. }
  30. }
  31. cout<< "the sort is:" ;
  32. for ( int i=0;i<10;i++)
  33. {
  34. cout<<b[i]<< " " ;
  35. }
  36. cout<<endl;
  37. }

Performance analysis: The time complexity for Hill sort is a bit complicated. It varies according to the specific "increment". Here, the author uses O(n^3/2) from Yan Weimin's "Data Structure"

Quick Sort

Conditions of use: Collections of comparable sizes.

Algorithm idea: Split the records to be sorted into two independent parts through a sorting process. If the keywords of one part of the records are smaller than the keywords of the other part, the two parts of the records can be sorted separately to finally reach an ordered sequence. There is a key point here, which is to select the "benchmark" for splitting. It must be that the records greater than this "benchmark" are divided into one part, and the records less than this "benchmark" are divided into another part. Here, the author defaults to taking the first record of the part as the "benchmark".

Example programming: int b[10]={77,1,65,13,81,93,10,5,23,17}

  1. //Quick sort  
  2. void QuickSort( int *b, int low, int high)
  3. {
  4. //Swap function  
  5. void Sawp( int *a, int *b);
  6. int Old_low=low;
  7. int Old_high=high;
  8. while (low<high)
  9. {
  10. while (*(b+high)>=*(b+low)&&low<high)high--;
  11. Sawp(b+low,b+high);
  12. while (*(b+low)=<*(b+high)&&low<high)low++;
  13. Sawp(b+low,b+high);
  14. }
  15. if (Old_low<low-1)
  16. {
  17. QuickSort(b,Old_low,low-1);
  18. }
  19. if (high+1<Old_high)
  20. {
  21. QuickSort(b,high+1,Old_high);
  22. }
  23. }
  24.  
  25. //Swap function  
  26. void Sawp( int *a, int *b)
  27. {
  28. int temp;
  29. temp=*a;
  30. *a=*b;
  31. *b=temp;
  32. }

Performance analysis: time complexity O(nlogn)

Original text: http://www.cnblogs.com/couhujia/archive/2011/03/24/1993373.html

<<:  Easily master the basic programming algorithms (I)

>>:  Easily master the basic programming algorithms (Part 3)

Recommend

Xiaomi, on the eve of a breakout or on the brink of falling?

"Good student" Xiaomi handed in a finan...

Top 5 tools for cross-platform mobile app development

[51CTO Translation] This article introduces five ...

How to understand user growth? Here are 4 cases for you!

During the Spring Festival, I finished reading Hu...

Facebook user growth strategy!

Whenever we talk about growth, we have to mention...

This kind of employee will either be a shareholder or a boss in the future!

1. Loyal people are not fired The company may fir...

2022 Brand Online Marketing Traffic Observation

Looking back at 2021, the boundaries between e-co...

The “Rio Adventure” has begun, how does Olympic marketing work?

The Rio Olympics may be the most worrying Olympic...

Dealing with two common problems in iOS development

[[134199]] (I) “Unknown class XXViewController in...

From 0 to 1, interpreting Android ASO optimization!

Hello everyone, I am an ASO operations specialist...