Today we will discuss comparison algorithms in C++. The most basic comparison is between two numbers, as shown in the following problem.
Problem 1: Output the Larger Number
Input two numbers and output the larger one. This problem is very simple and can be solved with the most basic comparison statement:
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
if(a<b)cout<<b;
else cout<<a;
return 0;
}
However, in C++, there is a function called max():
max(a,b);//Find the larger number between a and b (only two numbers can be compared)
This built-in function requires the cmath toolbox, so you should add the line #include<cmath> below. Thus, the above program can also be modified as follows:
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
cout<<max(a,b);
return 0;
}
Problem 2: Output the Larger Number 2
Input three numbers and output the largest one. This problem is also very easy, and we can still use the built-in max() function:
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int a,b,c;
cin>>a>>b>>c;
cout<<max(a,max(b,c));
return 0;
}
Since the max() function can only compare two numbers, we can nest functions to compare the largest of the two with the third number. Now, let’s consider how we would handle the case if we were given 5, 10, or even 1000 numbers. We must use an array to solve this problem. The simplest method is to conduct a tournament to find the maximum number.
Problem 3: Output the Larger Number 3
Given 100 numbers, output the largest one.
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int a[100],bigest=-1;
for(int i=0;i<100;i++){
cin>>a[i];
bigest=max(bigest,a[i]);
}
cout<<bigest;
return 0;
}
The above code uses bigest as the champion, allowing 100 numbers to compare with bigest, which is very simple. There is also a second method:
#include<bits/stdc++.h>
using namespace std;
int main(){
int a[100];
for(int i=0;i<100;i++){
cin>>a[i];
}
cout<<*max_element(a,a+100);
return 0;
}
*max_element(array_name+starting_index, array_name+ending_index); // Find the largest number in the array
*min_element(array_name+starting_index, array_name+ending_index); // Find the smallest number in the array
Problem 4: Sequential Output
Given 100 numbers, please output them from largest to smallest. For this problem, we need to apply the concept of “sorting”. Of course, C++ also has a built-in function sort() that can help with sorting, and its usage is similar to *max_element():
sort(array_name+starting_index, array_name+ending_index+1);
Thus, the program can be modified as follows:
#include<bits/stdc++.h>
using namespace std;
int main(){
int a[100];
for(int i=0;i<100;i++){
cin>>a[i];
}
sort(a,a+100);
for(int i=0;i<100;i++){
cout<<a[i]<<" ";
}
return 0;
}
The above sort() function is built-in, but we can also write our own sorting rules. Today, we will focus on a few methods.
Method 1: Bubble Sort
This code implements the standard bubble sort algorithm, which sorts the array in ascending order. Each round of the loop moves the largest element of the current unsorted portion to its correct position:
#include<bits/stdc++.h>
using namespace std;
int main(){
int a[109];
for(int i=0;i<100;i++){
cin>>a[i];
}
for(int i=0;i<99;i++){
for(int j=0;j<99-i;j++){
if(a[j]>a[j+1]){
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(int i=0;i<100;i++){
cout<<a[i]<<" ";
}
return 0;
}
Method 2: Selection Sort
Selection sort involves picking the largest or smallest number from the 100 numbers and placing it at the end:
#include<bits/stdc++.h>
using namespace std;
int main(){
int a[100],b[100]={0},count1=0,maxa=-10000;
for(int i=0;i<100;i++){
cin>>a[i];
}
for(int i=0;i<100;i++){
for(int j=0;j<100;j++){
if(a[j]>maxa){
maxa=a[j];
count1=j;
}
}
cout<<a[count1]<<" ";
a[count1]=-10000;
maxa=-10000;
}
return 0;
}
Selection sort is essentially looping through *max_element() 100 times. This means that the order is already sorted during insertion, unlike bubble sort which requires internal sorting.
Method 3: Insertion Sort
Insertion sort involves randomly taking a number from the array and comparing it with the sorted part of the array. If it is larger (or smaller), it shifts back one position and continues comparing:
#include<bits/stdc++.h>
using namespace std;
int main(){
int a[10],b[10]={0},top=0,temporary=0;
for(int i=0;i<10;i++){
cin>>a[i];
b[i]=-10000;
}
for(int i=0;i<10;i++){
temporary=a[i];
int topping=0;
while(topping<=top){
if(temporary>b[topping]){
for(int j=top+1;j>=topping;j--){
b[j+1]=b[j];
}
b[topping]=temporary;
top++;
break;
}
topping++;
}
}
for(int i=0;i<10;i++){
cout<<b[i]<<" ";
}
return 0;
}
The essence of insertion sort is searching, comparing, shifting, and repositioning. The shifting step is crucial; it must create space for repositioning. That concludes our discussion for today. If you have any questions, feel free to message me. See you next time! 👋👋👋ヾ( ̄▽ ̄)Bye~Bye~.