🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
In this blog post, we will learn how to write a C++ program to sort an array of integers in Descending Order using the Insertion Sort algorithm.
Insertion Sort is a simple and elegant sorting algorithm often taught in introductory computer science courses. The idea behind it is to continually insert an element from the unsorted section of the array into the sorted section until the entire array is sorted.
2. Program Overview
Our C++ program is structured as:
1. insertionSort(): The central function that applies Insertion Sort to sort the array.
2. printArray(): A utility function to display the array's elements.
3. main(): The main function that triggers the program.
3. Code Program
#include<iostream>
using namespace std;
// Function to perform insertion sort in descending order
void insertionSort(int arr[], int n) {
int key, j;
for (int i = 1; i < n; i++) {
key = arr[i]; // The element to be compared
j = i - 1;
// Move elements of arr[0..i-1] that are less than key
// to one position ahead of their current position
while (j >= 0 && arr[j] < key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key; // Position the key in its correct spot
}
}
// Utility to print the array
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
// Driver function
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Original array: \n";
printArray(arr, n);
insertionSort(arr, n);
cout << "Sorted array in descending order: \n";
printArray(arr, n);
return 0;
}
Output:
Original array: 64 25 12 22 11 Sorted array in descending order: 64 25 22 12 11
4. Step By Step Explanation
1. insertionSort(): Starting from the second element (indexed at 1), we continuously insert elements from the unsorted part to the sorted section. For descending order, if the 'key' element is larger than the preceding ones, we shift those elements to the right to place the 'key' in its rightful position.
2. printArray(): A simple utility to display the array's status. Useful for presenting the pre and post-sort state of the array.
3. main(): This drives the program. It sets up a test array, displays it, runs the insertion sort, and then showcases the sorted array.
Insertion Sort's simplicity can be its advantage when dealing with small arrays or almost sorted data. But for large datasets, more advanced algorithms like QuickSort or MergeSort might be a better fit due to their superior time complexities.
Related C++ Programs on Sorting Algorithms
- Bubble Sort in Ascending Order in C++
- Bubble Sort in Descending Order in C++
- Selection Sort in Ascending Order in C++
- Selection Sort in Descending Order in C++
- Insertion Sort in Ascending Order in C++
- Insertion Sort in Descending Order in C++
- Merge Sort in Ascending Order in C++
- Merge Sort in Descending Order in C++
- Quick Sort in Ascending Order in C++
- Quick Sort in Descending Order in C++
- Heap Sort in Ascending Order in C++
- Heap Sort in Descending Order in C++
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment