This code is about sorting the array
#include <bits/stdc .h>
using namespace std;
int main()
{
int arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
int n = sizeof(arr) / sizeof(arr[0]);
Here we take two parameters, the beginning of the array and the length n upto which we want the array to be sorted
//I am unable to understand this line how arr n specifies end position here?
**sort(arr, arr n);**
.
cout << "\nArray after sorting using "
"default sort is : \n";
//Here I started printing the array
for (int i = 0; i < n; i)
cout << arr[i] << " ";
return 0;
}
CodePudding user response:
For arrays, array name arr indicates iterator pointing to first element of array and n would increment that iterator by n elements. In your case, the sort algorithm should take beginning iterator and iterator pointing to one beyond last element.
arr: beginning iterator arr n: ending iterator (one beyond last element)
Typically, algorithms don't count ending iterator in their range, so it's like this.
CodePudding user response:
The convention on the standard libraries is that the ranges must be provided as an iterator pointing to the first element and an iterator pointing to one past the last element.
So your sequence has 10 elements
array = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
index => 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
so arr[0]
points to the first element and arr[10]
points to one past the last element.