ในภาษาโปรแกรม การเรียงลำดับเป็นฟังก์ชันพื้นฐานที่ใช้กับข้อมูลเพื่อจัดเรียงข้อมูลเหล่านี้เป็นข้อมูลจากน้อยไปมากหรือมากไปหาน้อย ในโปรแกรม 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