Preliminary algorithm (1): Understand the time complexity through a simple sorting algorithm

Table of Contents of Series Articles 

  Chapter 1    Preliminary Algorithm (1): Understand the time complexity through a simple sorting algorithm

  Chapter 2    Preliminary Algorithm (2): Introduce the details and time complexity of insertion sort in detail

  Chapter 3    Preliminary Algorithm (3): Explanation and implementation of the bisection method (C language), and the application of bisection light in ordered arrays 


Table of contents

Table of Contents of Series Articles

Preface

1. What is time complexity?

Constant time operation:

time complexity:

Sort by:

2. What is the additional space complexity?

Summarize


Preface

       As we all know, algorithms are something that programmers must learn , and the editor is a rookie, so I will let the stupid bird fly first. In this series, I will describe my personal experience of learning algorithms and record everything I have learned. , I hope friends who read this article will cheer together!

       When studying search algorithms on the Internet, there is a big guy (where does the hero come from) who also starts with sorting, and I will also learn from the big guy's notes appropriately.


1. What is time complexity?

       Before understanding the time complexity, we first introduce a piece of knowledge: constant time operations .

1.1 Constant time operations:

       When we write code, we will write some instructions. These instructions have nothing to do with the amount of data and are a fixed-time operation. This is a constant-time operation.

Here are some examples:

       Some expression operations , array addressing , taking a number from an array , etc.

       After understanding the constant time operations, let's understand the time complexity through sorting .

1.2 Time complexity:

1.2.1 Sorting:

       First, let’s explain the selection sort algorithm. Selection sorting should be the simplest sorting. Below is an animation, (it is based on where the heroes come from). ( Where do heroes come from )

        When calculating time complexity, the worst step ( all steps are performed ) is generally considered . In selection sort,

int main()
{
	int n = 0;
	scanf("%d", &n);
	int arr[10000];
	int minIndex = 0;
	int temp = 0;
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &arr[i]);
	}
	for (int i = 0; i < n; i++)
	{
        minIndex = i;
		for (int j = i + 1; j < n; j++)
		{
			if (arr[minIndex] > arr[j])
			{
				minIndex = j;
			}
		}
		temp = arr[i];
		arr[i] = arr[minIndex];
		arr[minIndex] = temp;
	}
	for (int i = 0; i < n; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
	return 0;
}

      To judge the time quality of an algorithm, (1) first look at the time complexity indicators of the two algorithms: If the time complexity of the two algorithm processes is the same, then (2) run it with a sample with a large amount of data and see That time is less. There is also a gap in fixed time for constant operations.

2. What is the additional space complexity?

       Additional space complexity is the amount of additional space requested by the process during execution .


Summarize

       The above is what I will talk about today. This article only briefly introduces the time complexity and additional space complexity. I hope you will leave comments after reading it, thank you all!

Guess you like

Origin blog.csdn.net/2301_77868664/article/details/131860027