PPS Lab · Experiment 45
Selection sort in descending order
CSorting and searchingJNTUH R22 · CS108ES
Aim
To write a C program that sorts an array of integers in descending order (largest first) using selection sort, and shows the array after each pass.
Algorithm
- Read n (1 to 100) and then n integers into an array.
- Repeat for i = 0 to n − 2:
- Assume the largest element of the unsorted part a[i..n−1] is at index i. Call this index max_index.
- Compare a[max_index] with every element from i + 1 to n − 1. If an element is larger, make its index the new max_index.
- If max_index is not i, swap a[i] and a[max_index]. Now a[i] holds the largest of the remaining elements.
- Print the array after the pass.
- Print the sorted array.
Program
#include <stdio.h>
#define MAX 100
void print_array(const int a[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");
}
/* Sorts a[0..n-1] in descending order, printing the array after each pass */
void selection_sort_desc(int a[], int n) {
for (int i = 0; i < n - 1; i++) {
int max_index = i;
for (int j = i + 1; j < n; j++) {
if (a[j] > a[max_index]) {
max_index = j;
}
}
if (max_index != i) {
int temp = a[i];
a[i] = a[max_index];
a[max_index] = temp;
}
printf("After pass %d: ", i + 1);
print_array(a, n);
}
}
int main(void) {
int a[MAX], n;
printf("Enter the number of elements (1 to %d): ", MAX);
if (scanf("%d", &n) != 1 || n < 1 || n > MAX) {
printf("Invalid number of elements\n");
return 1;
}
printf("Enter %d integers: ", n);
for (int i = 0; i < n; i++) {
if (scanf("%d", &a[i]) != 1) {
printf("Invalid input\n");
return 1;
}
}
selection_sort_desc(a, n);
printf("Sorted in descending order: ");
print_array(a, n);
return 0;
}Output
Enter the number of elements (1 to 100): 6
Enter 6 integers: 45 12 78 34 90 23
After pass 1: 90 12 78 34 45 23
After pass 2: 90 78 12 34 45 23
After pass 3: 90 78 45 34 12 23
After pass 4: 90 78 45 34 12 23
After pass 5: 90 78 45 34 23 12
Sorted in descending order: 90 78 45 34 23 12 Recorded by compiling and running this exact program. Typed input appears after each prompt.
How it works
Each pass selects the largest number left in the unsorted part and puts it at the front of that part. For ascending order, look for the smallest instead: change a[j] > a[max_index] to a[j] < a[min_index].
In pass 4 of the run, 34 is already the largest of the remaining numbers (34, 12, 23), so no swap is made and the array does not change.
Common mistakes
Swapping inside the inner loop every time a bigger number is found. That turns it into a different, slower sort. Only remember the index in the inner loop, and swap once after it.
Starting
max_indexat 0 in every pass instead of ati. The part beforeiis already sorted and must not be touched again.Using
<in the comparison. That finds the smallest and sorts in ascending order, not descending.Running the outer loop to
ninstead ofn - 1. It is not wrong, but the last pass does nothing, because one element left on its own is already in place.
Viva questions
Try to answer each one aloud before you open it.
How does selection sort work?
In each pass it finds the largest (or smallest) element in the unsorted part and swaps it into the first position of that part. The sorted part grows by one element each pass.
What is the time complexity of selection sort?
O(n²) in the best, average and worst cases, because it always scans the whole unsorted part to find the maximum, even if the list is already sorted.
How many swaps does selection sort make at most?
At most n − 1, one per pass. This is fewer than bubble sort, which can swap many times in each pass.
What change makes this program sort in ascending order?
Find the smallest element instead of the largest: use
a[j] < a[min_index]in the comparison.Is selection sort stable?
The usual version is not stable. The long-distance swap can move an element past another equal element, changing their order.
Learn the concepts
- Arrays in C: Declaration, Initialisation, Indexing and Common Operations
- Functions in C: Declaration, Definition, Prototypes and Calling
- for Loop in C: Syntax, Examples, Counting Down and Looping Over Arrays
- Bubble Sort, Selection Sort and Insertion Sort in Python
- How to Analyse Time Complexity of Code (Step by Step)