Home >Backend Development >C++ >How to compare using sorting algorithms in C++
Comparison using sorting algorithms in C
The sorting algorithm is one of the most basic and commonly used algorithms in computer science. In programming, we often need to sort a set of data in order to better organize and process the data. C provides a variety of sorting algorithm library functions, such as std::sort and std::stable_sort. This article describes how to use sorting algorithms in C for comparison and provides specific code examples.
1. Use of std::sort function
std::sort function is a commonly used sorting function in the C standard library. It can sort data in an array or container. The following is the function prototype of the std::sort function:
template
void sort (RandomAccessIterator first, RandomAccessIterator last);
The following is the use of the std::sort function Sample code for sorting:
#include <iostream> #include <algorithm> #include <vector> int main() { // 创建一个整数数组 std::vector<int> arr = {5, 2, 9, 1, 8}; // 使用std::sort函数对数组进行排序 std::sort(arr.begin(), arr.end()); // 输出排序后的数组 for (int num : arr) { std::cout << num << " "; } return 0; }
In the above code, first we create an integer array arr and then use the std::sort function to sort the array. Finally, we loop through the array and output the sorted results.
2. Use of std::stable_sort function
std::stable_sort function is also a sorting function in the C standard library. The difference from std::sort function is that std: The :stable_sort function can maintain the relative order of two equal elements. The following is the function prototype of the std::stable_sort function:
template
void stable_sort (RandomAccessIterator first, RandomAccessIterator last);
The following is the use of the std::stable_sort function Sample code for sorting:
#include <iostream> #include <algorithm> #include <vector> int main() { // 创建一个结构体数组 struct Person { std::string name; int age; }; std::vector<Person> people = { {"Alice", 20}, {"Bob", 18}, {"Carol", 22}, {"David", 20} }; // 使用std::stable_sort函数对结构体数组按照年龄进行排序 std::stable_sort(people.begin(), people.end(), [](const Person& a, const Person& b) { return a.age < b.age; }); // 输出排序后的结果 for (const Person& p : people) { std::cout << p.name << " " << p.age << std::endl; } return 0; }
In the above code, we create a structure array people, where each element contains the name and age of a person. Then, we use the std::stable_sort function to sort the structure array by age. When sorting, we used a lambda expression to specify the comparison based on the age member of the Person structure. Finally, we loop through the array and output the sorted results.
Summary:
In this article, we introduced how to use sorting algorithms in C for comparison and provided specific code examples. By mastering and flexibly applying the sorting algorithm in C, you can better organize and process data and improve the efficiency and performance of the program. I hope this article will be helpful to your study and practice.
The above is the detailed content of How to compare using sorting algorithms in C++. For more information, please follow other related articles on the PHP Chinese website!