A case of HDU 1425 illustrates the process of bubble sorting and quick sorting

Posted by kaisellgren on Mon, 03 Jan 2022 09:30:49 +0100

Detailed bubble sort and quick sort

Generally speaking, the questions given by the competition generally have a variety of solutions. It assesses the problem-solving in a limited time and space.
If the conditions are very loose, you can choose the easiest algorithm to program among a variety of solutions; If the given conditions are harsh, there are not many suitable algorithms to choose.
The following uses an example to illustrate how to choose different algorithms for the same problem.

Given n integers, please output the number with the first m in the order from large to small.
Input: each group of test data has two lines, the first line has two numbers N and m (0 < n, m < 1000000), and the second line contains n different numbers, which are all in the zone
Integer between [- 500000].
Output: output the largest number of each group of test data in order from large to small.
Input example:
5 3
3 -35 92 213 -644
Output example:
213 92 3

The idea of this question is to sort 1 million numbers first, and then output the first m large numbers. The title gives the code running time. The time of non Java language is 1s and the memory is 32MB.

The following are programmed with bubble sorting and quick sorting algorithms.

• 1. Bubble sorting

Firstly, the simplest bubbling algorithm is used to solve the above problem.

```#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int n,m;
int s[1000001];
void bubble_sort() {
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n - i - 1; j++)
if (s[j] > s[j + 1]){
int temp = s[j];
s[j] = s[j + 1];
s[j + 1] = temp;
}
}
}
int main() {
while (~scanf("%d %d",&n,&m)){
for (int i = 0; i < n;i++) scanf("%d", &s[i]);
bubble_sort();
for (int i = n-1; i >= n-m;i--) {
printf("%d ", s[i]);
}
}
return 0;
}
```

In the bubble_ After sort () is run, the sorting result from small to large is obtained, and then the first m large number is printed from back to front.
The steps of bubble sorting algorithm are as follows:

(1) In the first round, compare two adjacent s [J] and s [J + 1] one by one from the first number to the nth number. If s [J] > s [J + 1], exchange. This
The result of the round is to "bubble" the maximum number to the nth position, and don't worry about it later.
(2) The second round, from the first number to the n - 1 number, compare each adjacent number. This round, the second largest number "bubble" to the n - 1 position.
(3) Continue the above process until the end.

• 2. Quick sort

Quick sort is an excellent sort algorithm based on divide and conquer.

```#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int n, m;
int s[1000001];
void quick_sort(int s[],int L,int R) {
int left = L, right = R;
int flag = s[left];
if (L > R) return;
while (left < right) {
while (s[right] >= flag && left < right) right--;
if (left < right) {
int temp = s[right];
s[right] = s[left];
s[left] = temp;
};
while (s[left] <= flag && left < right) left ++;
if (left < right) {
int temp = s[right];
s[right] = s[left];
s[left] = temp;
};
if (left >= right) s[left] = flag;
}
quick_sort(s,L,right - 1);
quick_sort(s,right + 1,R);
}
int main() {
while (~scanf("%d %d", &n, &m)) {
for (int i = 0; i < n; i++) scanf("%d", &s[i]);
quick_sort(s,0,n-1);
for (int i = n - 1; i >= n - m; i--) {
printf("%d ", s[i]);
}
}
return 0;
}
```

In quick_ After sort () is run, the sorting result from small to large is obtained, and then the first m large number is printed from back to front.
The steps of quick sort algorithm are as follows:
(1) First select a reference number flag, and generally select the first element as the reference number.
(2) Put the number less than the benchmark number in the array to the left of the benchmark number, and the number greater than the benchmark number in the array to the right of the benchmark number.
(3) Then, taking the reference number as the center, divide the original array into left and right parts, and repeat the above two processes for these two parts respectively until there is only one element in each subset.

Topics: Algorithm data structure