Sorting algorithm-----bubble sort and selection sort

Table of contents

Foreword:

Bubble Sort

Schematic diagram

Code

analysis Summary

 selection sort

Schematic diagram

Code

analysis Summary


Foreword:

        Today we will start learning about sorting algorithms. Sorting algorithms are also one of the important components of data structures and algorithms. Sorting algorithms are the most classic algorithmic knowledge. Because its implementation code is short and wide, sorting algorithms and related questions are often asked in interviews. Generally, basic sorting algorithms such as quick sort and merge sort are most commonly tested in interviews, and basic sorting algorithms are often required to be written by hand on site. If you can't answer these questions well, the interview will probably be disappointing. Therefore, it is crucial to be proficient in the ideas and characteristics of sorting algorithms and to be able to write code proficiently. Okay, let’s start our study today!

Bubble Sort

        Bubble Sort is a relatively simple sorting algorithm in the field of computer science . It repeatedly visits the column of elements to be sorted, compares two adjacent elements in turn, and swaps them if the order (such as from large to small, first letter from Z to A) is wrong. The work of visiting elements is repeated until no adjacent elements need to be exchanged, which means that the element column has been sorted.

        The name of this algorithm comes from the fact that smaller elements will slowly "float" to the top of the sequence through exchange (arranged in ascending or descending order), just like the carbon dioxide bubbles in carbonated drinks will eventually float to the top, hence the name "bubble" Sort".

Schematic diagram

 Code

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>

//冒泡排序
void bubble_sort(int* n,int length) {
	
	for (int i = 0; i < length-1; i++)
	{
		for (int j = 0; j<length - i-1; j++)
		{
			if (n[j] > n[j + 1]) {
				int temp = n[j];
				n[j] = n[j + 1];
				n[j + 1] = temp;
			}
		}
	}
}
int main() {
	int array[10];
	srand((unsigned)time(0));//设置随机数种子
	for (int i = 0; i < 10; i++) {
		array[i] = rand() % 20; //产生10个0~20的随机数
	}
	for (int i = 0; i < sizeof(array) / sizeof(int); i++) {
		printf("%d ", array[i]);
	}
	printf("\n排序后:");
	bubble_sort(array, sizeof(array) / sizeof(int));
	for (int i = 0; i < sizeof(array) / sizeof(int); i++) {
		printf("%d ", array[i]);
	}

}
//输出结果
//5 4 11 8 2 16 16 9 2 15
//排序后:2 2 4 5 8 9 11 15 16 16

analysis Summary

Bubble sorting is a stable sorting algorithm ( relative positions are not exchanged when the same number appears ). The time complexity of bubble sorting in ordered cases is O(n). Of course, if in complete reverse order When, the complexity is O(n^2) . If there are a lot of numbers to be sorted, then the time complexity will be quite scary. Overall, the time complexity of bubble sorting is O(n^2 ). Bubble sort As one of the simplest sorting algorithms, bubble sort gives me the same feeling as Abandon appears in a word book. It is at the first place on the first page every time, so it is the most familiar. There is another optimization algorithm for bubble sorting, which is to set a flag. When the elements are not exchanged during a sequence traversal, it proves that the sequence is in order. But this improvement doesn't do much to improve performance.

 selection sort

        Selection sort is a simple and intuitive sorting algorithm . Its working principle is: first select the smallest (or largest) element from the data elements to be sorted, store it at the beginning of the sequence, and then find the smallest (largest) element from the remaining unsorted elements . elements and place them at the end of the sorted sequence. And so on, until the number of all data elements to be sorted is zero

Schematic diagram

 

Code

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
//选择排序
void select_sort(int* n, int length) {
	for (int i = 0; i < length-1; i++) {
		for (int j = i + 1; j < length; j++) {
			if (n[i] > n[j])
			{
				int temp = n[i];
				n[i] = n[j];
				n[j] = temp;
			}
		}
	}
}

int main() {
	int array[10];
	srand((unsigned)time(0));
	for (int i = 0; i < 10; i++) {
		array[i] = rand() % 20;
	}
	for (int i = 0; i < sizeof(array) / sizeof(int); i++) {
		printf("%d ", array[i]);
	}
	printf("\n排序后:");
	select_sort(array, sizeof(array) / sizeof(int));
	for (int i = 0; i < sizeof(array) / sizeof(int); i++) {
		printf("%d ", array[i]);
	}
}
//输出结果:
//2 12 8 3 13 17 18 12 4 14
//排序后:2 3 4 8 12 12 13 14 17 18

 analysis Summary

Time complexity sharing

The biggest feature of selection sort is that it reduces the time of number exchange. In terms of the number of exchanges, when it is best, it is exchanged 0 times, and when it is worst, it is exchanged n-1 times. The number of exchanges is much less than that of bubble sorting , because Swapping requires more CPU time than comparison. When n is small, selection sort is faster than bubble sort. But based on the overall sorting time (including comparisons and exchange times), its time complexity is O(n^2).

 Stability analysis

Selection sorting selects the smallest current element for each position, for example, selects the smallest for the first position, selects the second smallest for the second element among the remaining elements, and so on, until the n-1th element, the There is no need to select n elements, because only the largest element is left. Then, in a selection, if an element is smaller than the current element, and the small element appears after an element equal to the current element, then the stability will be destroyed after the exchange. For example, in the sequence 5 8 5 2 9, we know that the first element 5 selected in the first pass will be exchanged with 2. Then the relative order of the two 5s in the original sequence will be destroyed, so selection sorting is unstable. sorting algorithm.

 Okay, that’s all for today. We will continue to learn more advanced sorting algorithms in the next issue. See you next time!

 Share a wallpaper:

Guess you like

Origin blog.csdn.net/m0_73633088/article/details/132832095