Sorting an array in descending order is a common task that can be accomplished in various ways using the C++ programming language. This tutorial will discuss two methods for sorting the array in descending order.
1. Method 1:
#include <iostream>
#include <algorithm>
using namespace std;
const int ARRAY_SIZE = 10;
int main() {
// Create an array of integers
int arr[ARRAY_SIZE] = {3, 7, 1, 5, 2, 8, 4, 6, 9, 0};
// Print the unsorted array
cout << "Original array: ";
for (int i = 0; i < ARRAY_SIZE; i++) {
cout << arr[i] << " ";
}
cout << endl;
// Sort the array in descending order
sort(arr, arr + ARRAY_SIZE, greater<int>());
// Print the sorted array
cout << "Sorted array: ";
for (int i = 0; i < ARRAY_SIZE; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
Output:
Explanation:
This program creates an array of 10 integers and uses the sort function from the C++ Standard Template Library (STL) to sort the array in descending order. The sort function takes three arguments: the first and second arguments specify the range of elements to be sorted, and the third argument is a comparison function that determines the order in which the elements are sorted. In this case, we are using the greater<int> comparison function, which compares two integers and returns true if the first integer is greater than the second.
2. Method 2:
Selection sort is an in-place sorting algorithm that works by iteratively selecting the next smallest element and swapping it with the current element until the array is sorted. While it is not the most efficient sorting algorithm in terms of time complexity, it is simple to implement and useful for sorting small arrays or as a learning exercise.
Here is the general structure of a selection sort function in C++:
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
// Find the minimum element in the unsorted portion of the array
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap the minimum element with the current element
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
To sort the array in descending order, we can reverse the comparison operator in the if statement from < to >. This will cause the selection sort function to select the maximum element in the unsorted portion of the array at each iteration instead of the minimum element.
Here is the modified selection sort function for sorting an array in descending order:
void selectionSortDescending(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
// Find the maximum element in the unsorted portion of the array
int maxIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] > arr[maxIndex]) {
maxIndex = j;
}
}
// Swap the maximum element with the current element
int temp = arr[i];
arr[i] = arr[maxIndex];
arr[maxIndex] = temp;
}
}
To test our sorting Function, we can create a simple program that generates an array of random integers, prints it to the console, sorts it in descending order using the selectionSortDescending Function, and then prints the sorted array to the console.
Here is an example program that demonstrates how to use the selectionSortDescending Function to sort an array in descending order:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int ARRAY_SIZE = 10;
// Function prototype for the selection sort function
void selectionSortDescending(int arr[], int n);
int main() {
// Seed the random number generator
srand(time(0));
// Create an array of random integers
int arr[ARRAY_SIZE];
for (int i = 0; i < ARRAY_SIZE; i++) {
arr[i] = rand() % 100;
}
// Print the unsorted array
cout << "Original array: ";
for (int i = 0; i < ARRAY_SIZE; i++) {
cout << arr[i] << " ";
}
cout<<endl;
// Sort the array in descending order
selectionSortDescending(arr, ARRAY_SIZE);
// Print the sorted array
cout << "Sorted array: ";
for (int i = 0; i < ARRAY_SIZE; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
// Selection sort function for sorting an array in descending order
void selectionSortDescending(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
// Find the maximum element in the unsorted portion of the array
int maxIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] > arr[maxIndex]) {
maxIndex = j;
}
}
// Swap the maximum element with the current element
int temp = arr[i];
arr[i] = arr[maxIndex];
arr[maxIndex] = temp;
}
}
Output:
Explanation:
This program will generate an array of 10 random integers, print it to the console, sort it in descending order using the selectionSortDescending Function, and then print the sorted array to the console.