在编程语言中,排序是一种基本功能,它应用于数据,将这些数据是升序还是降序排列。在 C++ 程序中,有一个函数 std::sort()用于对数组进行排序。
sort(start address, end address)
这里,
Start address => The first address of the element. Last address => The address of the next contiguous location of the last element of the array.
#include <iostream> #include <algorithm> using namespace std; void display(int a[]) { for(int i = 0; i < 5; ++i) cout << a[i] << " "; } int main() { int a[5]= {4, 2, 7, 9, 6}; cout << "\n The array before sorting is : "; display(a); sort(a, a+5); cout << "\n\n The array after sorting is : "; display(a); return 0; }输出结果
The array before sorting is : 4 2 7 9 6 The array after sorting is : 2 4 6 7 9