Three simple sorting algorithms: bubble sort, selection sort, insertion sort (implemented in c++)

0 Introduction

The sorting problem is a basic and important problem, which is often encountered in job interviews or exams. I learned a series of sorting algorithms before, but I haven't read it for a long time and forgot, hereby summarize. First summarize the three simple sorting algorithms, and then summarize the more advanced algorithms, such as merge, fast sorting, etc. The following algorithms are all sorted in ascending order.

1 Bubble sort

Bubble sort is the first sorting algorithm that most people come into contact with, and its algorithm takes the idea of gradually narrowing the unsorted sequence . For an array of length N, the specific algorithm steps are: first determine that the length of the unsorted sequence is N, then let the largest value in the unsorted sequence float to the end (the origin of the bubble name), and the second step is to establish unsorted The sequence length of is N-1, the largest value is found in the unsorted sequence, and it floats to the end... This loop continues, the average time complexity of the algorithm is O( n 2 n^2n2 ).
The specific implementation code is as follows. The second for loop embodies the idea of ​​gradually reducing the unsorted sequence. When a large element floats to the end, ignore it and continue to process the unsorted sequence before it. An optimization here is that if no exchange occurs, indicating that the sequence is already in a sorted state, you can directly break.

vector<int> bubbleSort(vector<int>& nums) 
{
    
    
	for(int i = 0; i < nums.size(); ++i)
 	{
    
    
		int flag = 1;
        for(int j = 0; j < nums.size() - i - 1; ++j)
        {
    
    
        	if(nums[j] > nums[j+1])
            {
    
    
            	std::swap(nums[j],nums[j+1]);
                flag = 0;
            }
        }
        if(flag)
			break;
	}
    return nums;
}

2 Select sort

Selective sorting is a very natural algorithm, which adopts the idea of gradually expanding the ordered sequence . First determine that the ordered sequence is empty, and then we select the smallest value in the unsorted sequence through sequential comparison (the origin of the sort name is selected), and the unsorted sequence takes this value and puts it into the ordered sequence, and then repeats this operation until The unsorted sequence is empty. The average time complexity of the algorithm is O( n 2 n^2n2 ). code show as below:

vector<int> selectSort(vector<int>& nums) 
{
    
    
	for(int i = 0; i < nums.size(); ++i)
    {
    
    
    	int minId = i;
        for(int j = minId + 1; j < nums.size(); ++j)
        {
    
    
        	if(nums[minId] > nums[j])
            {
    
    
        		minId = j;
            }
        }
        std::swap(nums[i],nums[minId]);
    }
    return nums;
}

3 Insertion sort

Insertion sort also adopts the idea of gradually expanding the ordered sequence . It considers that the first element of an unordered sequence is an ordered sequence. Starting from the second element, the ordered sequence is added in sequence, and the appropriate position is inserted in the ordered sequence (the name of the insertion sort is derived). Its insertion process is like this, starting from the end element of the ordered sequence and starting to compare, if the current element is greater than the element to be inserted, the current element is moved back by one position, if the current element is strictly smaller than the element to be inserted, it is The element to be inserted is placed behind the current element (the placed position has been moved). The average time complexity of the algorithm is O( n 2 n^2n2 ).
The following is the code implementation.

vector<int> insertSort(vector<int>& nums) 
{
    
    
	int j;
    for(int i = 1; i < nums.size(); ++i)
    {
    
    
    	int tem = nums[i];
        for(j = i; j> 0 && tem < nums[j-1]; --j)
        	nums[j] = nums[j-1];
        nums[j] = tem;            
    }
    return nums;
}

Guess you like

Origin blog.csdn.net/MoonWisher_liang/article/details/107794426