# Learning in the third week -- preliminary algorithm - bucket sorting

Week 3 - algorithm learning

1. Bucket sorting

Let's take a look at the general bucket sorting

Bucket sorting is to add or subtract the number of buckets of the corresponding number

Example 1

## Title Description

The school is electing members of the student union. There are n(n\le 999)n(n ≤ 999) candidates. The number of each candidate ranges from 1 to nn. Now, m (m < = 2000000) m (m < = 2000000) ballot papers have been collected, and each ballot paper has a candidate number written. Now I want to sort these mountains of votes according to the number of votes from small to large.

## Input format

Enter nn and mm and the number on the MM ballot.

## Output format

Find the sorted ballot number.

## Input and output samples

Enter #1 copy

5 10
2 5 2 2 5 2 2 2 1 2


Output #1 copy

1 2 2 2 2 2 2 2 5 5

#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int m;
scanf("%d",&m);
int arr[200001]={0};
int b[200001]={0};
for(int i=0;i<m;i++){
scanf("%d",&arr[i]);
b[arr[i]]++;
}
for(int j=0;j<=200000;j++){
while(b[j]!=0){
printf("%d ",j);
b[j]--;
}
}
}


What is uncomfortable here is the special bucket consumption, that is, the consumption of memory. Some buckets may not be used, but the operation speed is fast

Example 2

## Title Description

In order to enrich people's lives and support some social public welfare undertakings, Beita city has set up a lottery. The rules of the lottery are:

1. Each lottery ticket is printed with 77 different numbers, and the value range of these numbers is 1\sim331 ∼ 33.
2. Each time before cashing, a winning number composed of seven different numbers will be announced.
3. A total of 77 awards are set up, including special awards and first to sixth prizes.

The cashing rules are as follows:

• Special prize: 77 numbers on the lottery are required to appear in the winning numbers.
• First prize: 66 numbers on the lottery are required to appear in the winning numbers.
• Second prize: 55 numbers on the lottery are required to appear in the winning numbers.
• Third prize: 44 numbers on the lottery are required to appear in the winning numbers.
• Fourth prize: 33 numbers on the lottery are required to appear in the winning numbers.
• Fifth prize: 22 numbers on the lottery are required to appear in the winning numbers.
• Sixth prize: 11 numbers on the lottery are required to appear in the winning numbers.

Note: the number on the lottery and the position of each number in the winning number are not considered when cashing. For example, if the winning number is 23 \ 31 \ 1 \ 14 \ 19 \ 17 \ 1823 31 1 14 19 17 18, the lottery 12 \ 8 \ 9 \ 23 \ 1 \ 16 \ 712 8 9 23 1 16 7 won the fifth prize because two numbers (2323 and 11) appeared in the winning number.

Now we know the winning numbers and the numbers of several lottery tickets Xiao Ming bought. Please write a program to help Xiao Ming judge the winning situation of the lottery he bought.

## Input format

The first line of input has only one natural number nn, which indicates the number of lottery tickets Xiao Ming bought;

The second line stores 77 natural numbers between 11 and 3333, indicating the winning number;

In the subsequent nn lines, each line has 77 natural numbers between 11 and 3333, representing the nn lottery tickets bought by Xiao Ming respectively.

## Output format

Output the winning situation (number of winning tickets) of the lottery tickets bought by Xiao Ming in turn. First, output the number of winning tickets of the special prize, and then output the number of winning tickets from the first prize to the sixth prize in turn.

## Input and output samples

Enter #1 copy

2
23 31 1 14 19 17 18
12 8 9 23 1 16 7
11 7 10 21 2 9 31


Output #1 copy

0 0 0 0 0 1 1


## Description / tips

#### Data scale and agreement

For 100% 100% data, ensure 1 \leq n\lt10001 ≤ n < 1000.

#include<stdio.h>
int main()
{
int n,q,p,cnt=0;
scanf("%d",&n);
int arr[34]={0};
int c[8]={0};
for(int i=1;i<=7;i++){
scanf("%d",&q);
arr[q]=1;
}
for(int i=0;i<n;i++){
for(int j=0;j<7;j++){
scanf("%d",&p);
if(arr[p]==1){
cnt++;
}
}
c[7-cnt+1]++;
cnt=0;
}
for(int i=1;i<=7;i++){
printf("%d ",c[i]);
}
}


Example 3. Use characters, which can be ASCII code tables of a,b,c,d, etc. as buckets

## Topic background

Wood has a background

## Title Description

A string of PASCAL language codes with a length of no more than 255. There are only three variables a, b and c, and there are only assignment statements. The assignment can only be a one digit number or a variable. The format of each assignment statement is [variable]: = [variable or one digit integer];. The unassigned variable value is 0, and the values of a, b and c are output.

## Input format

A string (< 255) PASCAL language has only three variables a, B and C, and only assignment statements. The assignment can only be a one digit number or a variable, and the value of the unassigned variable is 0.

## Output format

Output the final values of a, B and C.

## Input and output samples

Enter #1 copy

a:=3;b:=4;c:=5;


Output #1 copy

3 4 5

#include<stdio.h>
#include<string.h>
int main()
{
char arr[100];
int b[4]={0};
scanf("%s",&arr);
int lenth=strlen(arr);
for(int i=0;i<lenth;i++){
if(arr[i]==':'&arr[i+1]=='='&&i-1>=0&&arr[i+3]==';'){
if(arr[i+2]>='0'&&arr[i+2]<='9'){
b[arr[i-1]]=arr[i+2]-48;
}
if(arr[i+2]>='a'&&arr[i+2]<='z'){
b[arr[i-1]]=b[arr[i+2]];
}

}
}
printf("%d %d %d",b['a'],b['b'],b['c']);
}
This bucket sorting method is very good;
There are two cases: number and character


2. Quick sort

Library functions can be called directly

## Title Description

Enter nn (1 \ Le n < 50000001 ≤ n < 5000000 and nn is odd) digits a_ia**i (1 \ Le a_i < {10} ^ 91 ≤ a * * I < 109), output the number smaller than kk of these numbers. The smallest number is the 00th smallest.

Please try not to use nth_element to write this question, because the focus of this question is to practice the divide and conquer algorithm.

nothing

nothing

## Input and output samples

Enter #1 copy

5 1
4 3 2 1 5


Output #1 copy

2

#include<stdio.h>
#include<stdlib.h>
int tmp(const void*p1,const void*p2){
return *(int*)p1-*(int*)p2;
}
int main()
{
int n,m;
scanf("%d",&n);
scanf("%d",&m);
int arr[5000000]={0};
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
qsort(arr,n,4,tmp);
printf("%d",arr[m]);
}


2. Of course, quick sort can also be used for sorting structures

Such as sorting by students' grades

## Title Description

At present, N(N\le 1000)N(N ≤ 1000) students have participated in the final exam and obtained the information of each student: name (string with only English lowercase letters of no more than 8 characters), Chinese, mathematics and English scores (all natural numbers of no more than 150). The student with the highest total score is the most powerful. Please output the information of the most powerful student (name, grades of each subject). If there are multiple students with the same total score, the one with the top output.

nothing

nothing

## Input and output samples

Enter #1 copy

3
senpai 114 51 4
lxl 114 10 23
fafa 51 42 60


Output #1 copy

senpai 114 51 4

#include<stdio.h>
#include<stdlib.h>
struct stu{
char name[10];
int yuwen;
int shuxue;
int yinyu;
int sum;
};
int tmp( const void*p1,const void* p2){
stu c= *(stu*)p1;
stu d= *(stu*)p2;
return c.sum-d.sum;
}
int main()
{
int n;
scanf("%d",&n);
struct stu s[1001];
for(int i=0;i<n;i++){
scanf("%s%d%d%d",&s[i].name,&s[i].yuwen,&s[i].shuxue,&s[i].yinyu);
s[i].sum=s[i].yuwen+s[i].shuxue+s[i].yinyu;
}
qsort(s,n,sizeof(s[0]),tmp);
for(int j=0;j<n;j++){
if(s[j].sum==s[n-1].sum){
printf("%s %d %d %d",s[j].name,s[j].yuwen,s[j].shuxue,s[j].yinyu);
return 0;
}
}
printf("%s %d %d %d",s[n-1].name,s[n-1].yuwen,s[n-1].shuxue,s[n-1].yinyu);

return 0;
}



It is worth mentioning that the second parameter in qsort is the number of elements to be sorted. This number is easy to write wrong, such as some starting from 1 instead of 0

Tags: Algorithm

Posted on Sun, 28 Nov 2021 16:36:39 -0500 by yoda69