Time complexity

Keywords: C Algorithm

Time complexity: the time complexity of an algorithm is a function that qualitatively describes the running time of the algorithm. This is a function representing the length of the string of the input value of the algorithm. The time complexity is usually expressed by large O sign, excluding the low-order term and first term coefficient of this function. In this way, the time complexity can be called asymptotic, that is, when the size of the input value approaches infinity. (generally speaking, the speed of running time is relative here. For example, if we use the same code and there are few codes, we don't need to use complex sorting, but if the code is too large, we have to use complex sorting to shorten the time. If we still don't understand it, we can convert it into a mathematical model and regard different algorithms as different powers Function, some power functions have large y value in the early stage, and some power functions have large y value in the later stage).

Example:

Garlic gentleman has an array A with length n. Because the array is too large, you don't know what numbers are in the array, so you often ask whether the integer x is in array A.

Input format
In the first line, enter two integers n and m, representing the length of the array and the number of queries, respectively.

The next line has n integers a1

Next m lines, each line has an integer x, which represents the integer asked by garlic gentleman.

Output format
For each query, if it can be found, output "YES", otherwise output "NO".

Data range
1 ≤ n,m ≤ the 5th power of 10, 0 ≤ x ≤ the 6th power of 10.
I'll just show you the algorithm.

Quick sort

#include <stdio.h>

void quickSort(int left, int right, int a[]) { //Quick sort
	int key, low, high, s, j, temp;

	if (left >= right) { //Returns the main function when the left is greater than the right
		return;
	}

	key = a[left];
	low = left;
	high = right;
	while (low < high) {
		while (low < high && a[high] >= key) {
			high --;
		}

		while (low < high && a[low] <= key) {
			low ++;
		}
		if (low < high) {
			temp = a[low];
			a[low] = a[high];
			a[high] = temp;
		}
	}//Find a number at will, and put the row less than this number on the left and the row greater than this number on the right
	a[left] = a[low];
	a[low] = key;
	s = low - 1;
	j = low + 1;
	quickSort(left, s, a); //Sort once with the number to the left of this number
	quickSort(j, right, a); //Sort once with the number to the right of this number
}

int main() {
	int n, g, x, h = 0, z = 0, i = 0, num, num1, judge, left, right, mid, temp;
	scanf("%d", &n);

	int str1[n];
	while (1) {
		scanf("%d", &num);
		char c = getchar();
		str1[i++] = num;
		if (c == '\n') {
			break;
		}
	}
	x = n - 1;
	quickSort(0, x, str1);

	for (i = 0; i < n; i++) {
		printf("%d ", str1[i]);
	}
}

Bucket sorting:

#include <stdio.h>

int main() {
	int n, t, i, j;
	scanf("%d", &n);
	long long a[n];
	for (i = 0; i < n; i++) {
		a[i] = 0;
	}
	for (i = 0; i < n; i++) {
		scanf("%d", &t);
		a[t]++;
	}
	for (i = 0; i < n; i++) {
		for (j = 0; j < a[i]; j++) {
			printf("%d ", i);
		}
	}
}

Bubble sort:

#include <stdio.h>

int main() {
	int n, t, i, j, temp;;
	scanf("%d", &n);
	long long a[n] = {0};
	for (t = 0; t < n; t++) {
		scanf("%d", &a[t]);
	}
	for (i = 1; i <= n - 1; i++) { //The outer loop is the number of rounds compared. If there are 10 rounds in the array, then 10-1 = 9 rounds should be compared
		for (j = 0; j <= n - 1 - i; j++) { //The inner loop compares the number of comparisons in the current round. For example, the first round of comparison 9-1 = 8 times, and the second round of comparison 9-2 = 7 times
			if (a[j] > a[j + 1]) { //If two adjacent numbers are in reverse order, they exchange positions
				temp = a[j];
				a[j] = a[j + 1];
				a[j + 1] = temp;
			}
		}
	}
	for (i = 0; i < n; i++)
		printf("%-4d", a[i]);
	printf("\n");
}

There are infinite numbers in the array sorted by this question. We should consider whether to exceed the limit and whether to timeout (we should pay attention to the time complexity here). We can copy these codes and try to compare the time when the number is small and the time when the number is many.

Posted by predhtz on Tue, 23 Nov 2021 09:06:17 -0800