"Aha Algorithm" Chapter 1 - Sorting

insert image description here


foreword

This year, the Lanqiao Cup did not get the provincial first, so I decided to settle down to learn the algorithm, in order to make the learning of the algorithm easierstable, so I picked it up, mostbasic and classicAn algorithm book "Aha Algorithm", and decided to write my learning process as a blog to record and share.


1. Sorting algorithm

Bubble Sort

The slowest, and it is used when there is less data, the time complexity is O(n^2)

bucket sort

The time complexity is the lowest, that is, the speed is fast, it is O(N + C), N is the number of cycles, C is N*(logN - logM), and M is the number of buckets. Here you can see the end of the first section of this book

quick sort

The time complexity is O(N*logN), similar to the function sort() sorting, which is also the sorting that is often used in future algorithm competitions. The main idea is to divide and conquer

2. Bucket Sort

Bucket sorting, as the name implies, needs to set up many buckets or containers. Obviously, it can be an array in the computer, so when we are sorting 0 ~ 2e9 (2 10 9), we need 2*10 9 buckets, that is, apply for 2 10^ 9 variables , even if only 3 numbers are sorted, such a large space is still needed, which is a waste.

insert image description here
eg: The numbers on the flags obtained by the five children are 5 3 5 2 8 respectively, and then you are asked to sort them from big to small

#include<iostream>
using namespace std;
int book[10];//全局变量 自动 初始化为0 
int main()
{
    
    
	int t;
	for(int i=0;i<5;i++)//输入五个数字 
	{
    
    
		cin>>t;//输入的数字 
		book[t]++;//出现的数字次数++ 
	}
	for(int i=8;i>=0;i--)//i定义为分数 
		for(int j=0;j<book[i];j++)//分数出现的次数 
			cout<<i<<" ";
	return 0;
}

Data are as follows

输入:5 3 5 2 8
输出:8 5 5 3 2

3. Bubble sort

bubble sort akaBubble Sort, the principle is to compare two adjacent elements each time, and exchange them if the order is wrong.
insert image description here
I wrote the code about bubble sorting many times when I was learning C language. For details, you can go to the blog C language—array—summary

eg: Now there are 5 students whose scores need to be sorted from big to small. The scores are 89 90 69 100 67

#include<iostream>
using namespace std;
int score[6];//全局变量自动初始化为0
int i,j;
int main()
{
    
    
	for(i=0;i<5;i++)//五次输入
		cin>>score[i];
		
	for(i=0;i<5-1;i++)//冒泡排序的趟数
		for(j=0;j<5-1-i;j++)
		{
    
    
			if(score[j]<score[j+1])//交换
			{
    
    
				int tmp=score[j];
				score[j]=score[j+1];
				score[j+1]=tmp;
			}
		}
	//输出
	for(i=0;i<5;i++) cout<<score[i]<<" ";
	cout<<endl;
	
	return 0;
}

Data are as follows

输入:89 90 69 100 67
输出:100 90 89 69 67

3. Quick Sort

Quick sort knowledge explanation:

The core idea of ​​quick sort :Based on divide and conquer
The main content of quick sort is:

  • Determine the cutoff point: q[L],q[(L+R)/2],q[R]其实就是分为左边界,中间值和右边界,甚至随机一个数

  • adjust interval,其实就是把元素放到x的两侧

  • Recursively process the left and right paragraphs
    insert image description here
    The following is the specificExplain the adjustment interval of step 2
    insert image description here

  • Two pointers, i, j are constantly moving here

  • when pointer iWhen the pointed element <= x, the pointer moves to the rightSimilarly, when the pointer j encounters >=x, it willpointer moves left

  • when i pointerStop when the pointed element >= x, wait until the j pointerStop when the pointed element <= x

  • Finally made thisTwo elements are swapped and exchanged

insert image description here
The above is the basic knowledge of quick sort., some exercises will be explained below.Consolidate and practiceWhat we know

Quick row thought map:
insert image description here

For details, please see the acw algorithm notes I wrote [algorithm] Algorithm Basic Course - Sorting Algorithm (with notes | recommended collection)


Summarize

Algorithms are indeed difficult, but not everyone is very good at the beginning, but only when they start! ! !

Guess you like

Origin blog.csdn.net/congfen214/article/details/131647684