Selection Sort Algorithm in C: Esha Gupta
Selection Sort Algorithm in C: Esha Gupta
Esha Gupta
Associat e Senior Execut ive
Updated on Sep 3, 2024 13:54 IST
Ever wondered how your favourite music app arranges songs from least to
most played or how an online store lists products from cheapest to priciest?
At the heart of such operations are sorting algorithms – systematic methods
computers use to order data in specific sequences. In this blog, we will
understand about selection sort in c!
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 4-Sep-20 24.
Table of Content
What is Selection Sort
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 4-Sep-20 24.
Basic concept and working principle
Let’s see the basic working and principles of the selection sort algorithm using your
example array: arr[] = {17, 34, 25, 49, 09}
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 4-Sep-20 24.
Let’s consider this pseudocode snippet
procedure selectionSort(arr: array of integer, n: integer)
declare i, j, minIndex, temp: integer
f or i = 0 to n - 1 do
minIndex = i
f or j = i + 1 to n - 1 do
if arr[j] < arr[minIndex] then
minIndex = j
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 4-Sep-20 24.
end if
end f or
temp = arr[i]
arr[i] = arr[minIndex]
arr[minIndex] = temp
end f or
end procedure
Now, from the above pseudocode, let’s understand the algorithm step by
step.
Dry run
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 4-Sep-20 24.
[9, 34,
[17, 34, 25,
0 4 4 17 > 9 Yes 25, 49,
49, 9]
17]
Find the smallest element in the remaining unsorted portion {34, 25, 49, 17} and
swap it with the second position.
Dry run
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 4-Sep-20 24.
with the third position.
Dry run
Dry run
Now, af ter the 4th iteration, only one element remains 49, which is
inherently in its correct place as it’s the last and largest element in
the list.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 4-Sep-20 24.
So, at the end of the 4th iteration, the entire array is sorted in
ascending order:
arr[] = {09, 17, 25, 34, 49}
That completes the selection sort for the given array.
The n-1 concept for the outer loop in the implementation of the Selection Sort
algorithm has to do with the number of passes/iterations required to sort the array
which in this case is 5-1 = 4 iterations (where, n=5)
Within the outer loop, initially assume that the current element (i-th element) is the
smallest (minIndex = i) .
The inner loop (f or (j = i + 1; j < n; j++)) searches for the smallest element in
the unsorted portion of the array. If a smaller element is found than the assumed
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 4-Sep-20 24.
minimum, the minIndex is updated.
Step 5: Swap
After the inner loop completes for each pass of the outer loop, the element at the
minIndex (smallest in the unsorted portion) is swapped with the first element of
the unsorted portion (i-th element).
Step 6: Repeat
The process continues until the outer loop has traversed the entire array, at which
point the array will be sorted.
The main function initializes the array and its size, then calls the selectionSort
function to sort the array. Before and after the sorting process, the array is printed
to the console for visualization.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 4-Sep-20 24.
Code
Copy code
// Swap
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
int main() {
int arr[] = {17, 34 , 25, 4 9, 9}; // Initial array
int n = sizeof(arr) / sizeof(arr[0]); // Array length
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 4-Sep-20 24.
// Display original array
printf("Original Array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
Output
Original Array: 17 34 25 49 9
Sorted Array: 9 17 25 34 49
Complexity Analysis
Time Complexity
1. Best Case
This occurs when the input array is already sorted. Despite the array being sorted,
the Selection Sort algorithm will still go through all its steps trying to find the
smallest element in each iteration because it doesn’t have a mechanism to detect
an already sorted list. Thus, the best-case time complexity is still: O(n2 )
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 4-Sep-20 24.
2. Average Case
For an average or random list, Selection Sort will have to go through roughly half of
the list for every number. The pattern follows n + (n-1) + (n-2) + … + 2 + 1,
3. Worst Case
This happens when the input list is sorted in reverse. However, the behaviour of the
Selection Sort doesn’t change whether it’s a reverse sorted list or any random list. It
still goes through its entire process of finding the smallest element for each
Space Complexity
Selection Sort is an in-place sorting algorithm, which means it doesn’t require
any additional storage (or “space”) that grows with the input size. It uses a constant
amount of extra space for its variables (i, j, minIndex, temp). Hence, the space
complexity is O(1)
Scenario
You’re an IT intern at a small e-commerce startup called “QuickShop.” The company
is just getting started, and the website has a feature where sellers can list their
products for sale. Each seller has their own dashboard where they can see all the
products they’ve listed. The startup is very new, and the software is basic. Sellers
have been requesting a feature to sort their products by price so that they can
easily identify and modify their lowest-priced products.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 4-Sep-20 24.
items for each seller.
Task
Implement a feature that allows sellers to sort and view their products in ascending
order of price using the Selection Sort algorithm.
Solution
Given the constraints (sorting small lists), Selection Sort can be a reasonable choice
because of its simplicity.
Code
Copy code
#define MAX_PRODUCTS 10
typedef struct {
char name[50];
float price;
} Product;
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 4-Sep-20 24.
// Function to perform Selection Sort based on price
void selectionSort(Product products[], int n) {
int i, j, minIndex;
Product temp;
int main() {
Product sellerProducts[MAX_PRODUCTS] = {
{"Shirt", 1999.0},
{"Hat", 14 99.0},
{"Socks", 999.0},
{"Jacket", 24 99.0},
{"Jeans", 2999.0},
{"Watch", 34 99.0},
{"Belt", 4 99.0},
{"Shoes", 1299.0},
{"Sunglasses", 4 999.0},
{"Tie", 3999.0}
};
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 4-Sep-20 24.
printf("Products before sorting:\n");
for (int i = 0; i < MAX_PRODUCTS; i++) {
printf("%s: %.2f\n", sellerProducts[i].name, sellerProducts[i].price);
}
return 0;
}
Output
Products bef ore sorting:
Shirt: 1999.00
Hat: 1499.00
Socks: 999.00
Jacket: 2499.00
Jeans: 2999.00
Watch: 3499.00
Belt: 499.00
Shoes: 1299.00
Sunglasses: 4999.00
Tie: 3999.00
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 4-Sep-20 24.
Products af ter sorting by price:
Belt: 499.00
Socks: 999.00
Shoes: 1299.00
Hat: 1499.00
Shirt: 1999.00
Jacket: 2499.00
Jeans: 2999.00
Watch: 3499.00
Tie: 3999.00
Sunglasses: 4999.00
This code defines a Product structure with a name and price. It then uses the
Selection Sort algorithm to sort an array of these products based on their price. The
main function initializes a list of sample products, displays them, sorts them by
price, and then displays the sorted list.
Would become
Highly ef f icient f or small
inef f icient if QuickShop
Number of datasets like the given
decides to increase the
Items to Sort maximum of 10 items in
number of items
the seller dashboard.
displayed beyond 10.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 4-Sep-20 24.
Given the early stage of
As the platf orm grows,
the startup and likely
this simplicity might not
Simplicity limited resources, the
cater to more complex
simplicity of the algorithm
sorting needs.
is a boon.
Can be quickly
Might need to be
implemented without a
replaced with a more
Implementation steep learning curve,
ef f icient sort if the data
Time which is crucial f or a
grows, requiring
startup wanting rapid
redevelopment time.
f eatures.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 4-Sep-20 24.
operates by repeatedly selecting the smallest (or largest, depending on the order of
sorting) element from an unsorted portion and swapping it with the first unsorted
element. Keep Learning, Keep Exploring!
FAQs
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 4-Sep-20 24.