Given an array arr[] of integers, sort the array in descending order.
Examples :
Input: arr = [324, 5, 2, 2]
Output: 324 5 2 2
Explanation: The array is sorted in descending order. Since 324 is the largest element, followed by 5 and then the two occurrences of 2, the resulting array is [324, 5, 2, 2].
Input: arr = [54, 43, 2, 1, 5]
Output: 54 43 5 2 1
Explanation: After sorting the array in descending order, the elements are arranged from largest to smallest, resulting in [54, 43, 5, 2, 1].
Table of Content
Sorting in Ascending Order and Reverse - O(n log n) Time and O(1) Space
The idea is to first sort the array in ascending order using the default sorting algorithm and then reverse the entire array. After reversing, the elements appear in descending order.
#include <bits/stdc++.h>
using namespace std;
void sortInDesc(vector<int> &arr)
{
// Sort the array in ascending order
sort(arr.begin(), arr.end());
// Reverse the array
reverse(arr.begin(), arr.end());
}
// Driver Code
int main()
{
vector<int> arr = {54, 43, 2, 1, 5};
sortInDesc(arr);
for (int x : arr)
{
cout << x << " ";
}
cout << endl;
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sortInDesc(int* arr, int n)
{
// Sort the array in ascending order
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
// Reverse the array
for (int i = 0, j = n-1; i < j; i++, j--)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int main()
{
int arr[] = {54, 43, 2, 1, 5};
int n = sizeof(arr)/sizeof(arr[0]);
sortInDesc(arr, n);
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
import java.util.Arrays;
import java.util.Collections;
public class GFG {
public static void sortInDesc(Integer[] arr)
{
// Sort the array in ascending order
Arrays.sort(arr);
// Reverse the array
Collections.reverse(Arrays.asList(arr));
}
public static void main(String[] args)
{
Integer[] arr = { 54, 43, 2, 1, 5 };
sortInDesc(arr);
for (int x : arr) {
System.out.print(x + " ");
}
System.out.println();
}
}
def sortInDesc(arr):
# Sort the array in ascending order
arr.sort()
# Reverse the array
arr.reverse()
# Driver Code
if __name__ == "__main__":
arr = [54, 43, 2, 1, 5]
sortInDesc(arr)
for x in arr:
print(x, end=' ')
print()
using System;
public class GFG {
// Function to sort array in descending order
public static void sortInDesc(int[] arr)
{
// Sort the array in ascending order
Array.Sort(arr);
// Reverse the array
Array.Reverse(arr);
}
public static void Main()
{
int[] arr = { 54, 43, 2, 1, 5 };
sortInDesc(arr);
foreach(int x in arr) { Console.Write(x + " "); }
Console.WriteLine();
}
}
function sortInDesc(arr) {
// Sort the array in ascending order
arr.sort((a, b) => a - b);
// Reverse the array
arr.reverse();
}
// Driver Code
let arr = [54, 43, 2, 1, 5];
sortInDesc(arr);
for (let x of arr) {
process.stdout.write(x + ' ');
}
console.log();
Output
54 43 5 2 1
Using In-Built Sort with Reverse Iterators - O(n log n) Time and O(1) Space
The idea is to use the built-in sorting function provided by the language and arrange the elements in descending order. This places larger elements before smaller ones, resulting in a descendingly sorted array.
Language-specific Implementations:
Different programming languages provide built-in functions or comparators to directly sort an array in descending order. Some commonly used implementations are:
- C++: Uses STL sort with reverse iterators.
- Java: Uses Arrays.sort() with Collections.reverseOrder().
- Python: Uses the reverse=True parameter in sort().
- JavaScript: Uses a custom comparator in sort().
- C#: Uses Array.Sort() with a custom comparison delegate.
- C: Uses qsort() with a custom comparator function.
#include <bits/stdc++.h>
using namespace std;
void sortInDesc(vector<int> &arr)
{
// Sort the array in descending order
sort(arr.rbegin(), arr.rend());
}
// Driver Code
int main()
{
vector<int> arr = {54, 43, 2, 1, 5};
sortInDesc(arr);
for (int x : arr)
{
cout << x << " ";
}
cout << endl;
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// Comparator for ascending order
int compare(const void *a, const void *b)
{
return (*(int *)a - *(int *)b);
}
// Function to sort array in descending order
void sortInDesc(int arr[], int n)
{
// Sort the array in ascending order
qsort(arr, n, sizeof(int), compare);
// Reverse the array
int i = 0, j = n - 1;
while (i < j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
// Driver Code
int main()
{
int arr[] = {54, 43, 2, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
sortInDesc(arr, n);
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
import java.util.Arrays;
public class GFG {
// Function to sort array in descending order
public static void sortInDesc(int[] arr)
{
// Sort the array in ascending order
Arrays.sort(arr);
// Reverse the array
int i = 0, j = arr.length - 1;
while (i < j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
// Driver Code
public static void main(String[] args)
{
int[] arr = {54, 43, 2, 1, 5};
sortInDesc(arr);
for (int x : arr)
{
System.out.print(x + " ");
}
System.out.println();
}
}
# Function to sort array in descending order
def sortInDesc(arr):
# Sort the array in ascending order
arr.sort()
# Reverse the array
arr.reverse()
# Driver Code
if __name__ == "__main__":
arr = [54, 43, 2, 1, 5]
sortInDesc(arr)
for x in arr:
print(x, end=' ')
print()
using System;
public class GFG {
// Function to sort array in descending order
public static void sortInDesc(int[] arr)
{
// Sort the array in ascending order
Array.Sort(arr);
// Reverse the array
Array.Reverse(arr);
}
// Driver Code
public static void Main()
{
int[] arr = { 54, 43, 2, 1, 5 };
sortInDesc(arr);
foreach(int x in arr) { Console.Write(x + " "); }
Console.WriteLine();
}
}
// Function to sort array in descending order
function sortInDesc(arr)
{
// Sort the array in ascending order
arr.sort((a, b) => a - b);
// Reverse the array
arr.reverse();
}
// Driver Code
let arr = [ 54, 43, 2, 1, 5 ];
sortInDesc(arr);
console.log(arr.join(" "));
Output
54 43 5 2 1