प्रोग्रामिंग भाषा में, सॉर्टिंग एक बुनियादी फंक्शन है जिसे डेटा पर लागू करके डेटा को आरोही या अवरोही क्रम में व्यवस्थित किया जाता है। C++ प्रोग्राम में, ऐरे को सॉर्ट करने के लिए std::sort() फंक्शन होता है।
sort(start address, end address)
यहां,
Start address => तत्व का पहला पता।
Last address => ऐरे के अंतिम तत्व के अगले सन्निहित स्थान का पता।
उदाहरण कोड
#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