🎓 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
Insertion Sort, a simple and intuitive comparison-based sorting algorithm, works by building the final sorted array one item at a time. It is very similar to how we sort playing cards in our hands. When sorting in descending order, the algorithm compares each element and places it in its correct position among the already sorted elements, ensuring that the highest values appear first.
2. Program Overview
The structure of the program is:
1. insertionSortDesc: The primary function that implements the Insertion Sort algorithm for descending order.
2. printArray: A utility function to display the contents of the array.
3. main: The main function where the program starts execution.
3. Code Program
#include <stdio.h>
// Implementation of insertionSortDesc to sort in descending order
void insertionSortDesc(int arr[], int n) {
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i]; // Extract the current element
j = i - 1;
/* Move elements of arr[0..i-1] that are smaller 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 at its correct position
}
}
// Utility function to display the array's content
void printArray(int arr[], int size) {
int i;
for (i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
// Main function to test the insertionSortDesc function
int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr)/sizeof(arr[0]);
insertionSortDesc(arr, n);
printf("Sorted array in descending order: \n");
printArray(arr, n);
return 0;
}
Output:
Sorted array in descending order: 13 12 11 6 5
4. Step By Step Explanation
The insertionSortDesc function works as follows:
1. Beginning with the second element (assuming the first element is sorted), we pick each element in the array.
2. This element, referred to as key in the code, is compared with the elements before it.
3. If the key is larger than the preceding element, it is then compared with the elements before that element. This continues until the start of the array or until we find an element larger than the key.
4. During this comparison process, we shift each of the compared elements down to create space for the key.
5. Once the right position for the key has been determined, it's placed in its appropriate spot within the sorted section.
6. This process is repeated for all the elements in the array, ensuring that the array gets sorted in descending order.
In scenarios where the list is nearly sorted but in reverse order, Insertion Sort can be particularly efficient, as it only needs to reorder elements as necessary.
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