how to sort array in reverse order in c++ code example
Example 1: how to sort in descending order c++
int arr[10];
int length = sizeof(arr)/sizeof(arr[0]);
sort(arr, arr+length, greater<int>());
Example 2: reverse sort cpp
int main(){
int arr[5] = {1,3,2,4,5};
sort(arr, arr+5, greater<int>());
// arr == {5,4,3,2,1}
return 0;
}
Example 3: c++ reverse array
int arr[5] = {1, 2, 3, 4, 5}; //Initialize array
for(int i = 0; i < size(arr); i++) {
//Create temporary variable to hold current value in array
int temp = arr[i];
//Set the current value in the array to the mirrored value in array
arr[i] = arr[size(arr) - 1 - i];
//Set mirrored value in array to temp, swapping the two numbers
arr[size(arr) - 1 - i] = temp;
}