Algorithm study notes (C++) - sorting and searching
to sort
Sorting is integrated into the algorithm in C++, and it can be called by typing sort. The default is the ascending sort of int type, and the sorting method Compare can be defined according to the needs.
A usage example is given below.
Topic:
Enter the student's name and score, and expect to output the student's name and score in descending or ascending order. When the students' scores are the same, expect the student who entered first to output first.
Input:
the number of students in the first line n
the sorting method of the second line (0 is descending order, 1 is ascending order)
and the subsequent n lines are: name + space + score
Output (n lines):
the contents of each line are: (sorted by the specified method) name + space + score
Note:
sort defaults to the ascending order of int type.
In this question, since the name, grade, and input order need to be saved together, the structure Student is encapsulated, and the Compare function defined by oneself needs to be used.
# include <cstdio>
# include <iostream>
# include <algorithm>
# include <string>
using namespace std;
struct Student{
string name;
int score;
int order;
};
// increasing, type 1
bool AscendingCompare(Student a, Student b){
if (a.score == b.score){
return a.order < b.order;
}
else{
return a.score < b.score;
}
}
// decreasing, type 0
bool DescendingCompare(Student a, Student b){
if (a.score == b.score){
return a.order < b.order;
}
else{
return a.score > b.score;
}
}
int main(){
// total number of student and compare method
int number, type;
cin >> number >> type;
Student arr[number];
// load name, score and order
for (int i=0; i<number; ++i){
cin >> arr[i].name;
cin >> arr[i].score;
arr[i].order = i;
}
// compare with appointed method
if(type){
sort(arr, arr+number, AscendingCompare);
}
else{
sort(arr, arr+number, DescendingCompare);
}
// output
for (int i=0;i<number; ++i){
cout << arr[i].name << " " << arr[i].score << endl;
}
return 0;
}
Example:
look up
One of the meanings of sorting in the previous section is to help people search more efficiently. Ordered sequences can speed up retrieval efficiency without comparing elements one by one. This is very obvious when searching multiple times in the same sequence.
Regarding lookup, we introduce the binary search strategy here.
// binary search
# include <cstdio>
# include <iostream>
# include <algorithm>
using namespace std;
const int MAXN = 100;
int arr[MAXN];
bool binarySearch(int len, int target){
int left = 0;
int right = len-1;
while(left <= right){
int middle = (left + right) / 2;
if (arr[middle] < target){
// drop the left half of arr
left = middle + 1;
}
else if (arr[middle] > target){
// drop the right half of arr
right = middle - 1;
}
else{
// find it
return true;
}
}
return false;
}
int main(){
int len;
int n;
while(scanf("%d", &len) != EOF){
// load data
for (int i=0; i<len; ++i){
cin >> arr[i];
}
// sort in Ascending
sort(arr, arr+len);
// binary search
cin >> n;
int target;
while (n--){
cin >> target;
if (binarySearch(len, target)){
printf("True\n");
}
else{
printf("False\n");
}
}
}
return 0;
}