Arranging an array in a descending order is a frequent operation that can be achieved through different techniques in the C++ programming language. This guide will explore two approaches to sorting the array in a descending manner.
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 script generates a collection of 10 integers and employs the sort functionality from the C++ Standard Template Library (STL) to arrange the collection in a descending sequence. The sort operation necessitates three parameters: the initial and second parameters define the segment of elements to be organized, while the third parameter is a comparison function that dictates the sorting order. In this instance, we are utilizing the greater<int> comparison function, which assesses two integers and outputs true if the initial integer surpasses the second one.
2. Method 2:
Selection sort is a sorting algorithm that operates directly on the input array, repeatedly selecting the smallest element and exchanging it with the current element until the array is sorted. Although it may not be the most time-efficient sorting method, it is straightforward to execute and can be beneficial for organizing small arrays or for educational purposes.
Here is the basic framework 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 arrange the array in a descending order, we can invert the comparison operator within the if statement from < to >. By doing this, the selection sort algorithm will now pick the highest element in the unsorted segment of the array during each iteration, as opposed to selecting the lowest element.
Here is the revised selection sort algorithm for arranging an array in a 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 evaluate our sorting method, we can develop a basic script that generates a random integer array, displays it on the screen, arranges it in descending sequence using the selectionSortDescending algorithm, and then showcases the arranged array on the screen.
Here is a sample code snippet that showcases the implementation of the selectionSortDescending function to arrange an array in a 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 software will produce an array containing 10 random integer values, display it on the console, arrange the array in a descending order by employing the selectionSortDescending function, and subsequently exhibit the sorted array on the console.