C Program for Selection Sort Last Updated : 20 Aug, 2024 Comments Improve Suggest changes Like Article Like Report The selection sort is a simple comparison-based sorting algorithm that sorts a collection by repeatedly finding the minimum (or maximum) element and placing it in its correct position in the list. It is very simple to implement and is preferred when you have to manually implement the sorting algorithm for a small amount of dataset.In this article, we will learn about the selection sort, its working and its implement in C language.How Selection Sort Works in C?Selection sort work by virtually dividing the array into two parts: sorted and unsorted. Initially, whole list is considered unsorted. Starting from the first element in the unsorted part, a minimum element is identified from the unsorted part of the array and placed at the current position. The same is done for the rest of the elements in the unsorted part one by one gradually growing the sorted part till the whole array is sorted.Let's take the example of the array {64, 25, 12, 22, 11} which we want to sort in ascending order and see what happens in each pass:The resultant array is the sorted array.Selection Sort Algorithm in CStart with the entire list as unsorted.Start traversing the list from the first element.For each element:Find the smallest element from the current position to the end of the list.Swap it with the current element to place it in its correct position.Move to the next element and repeat until all elements are sorted.C Program to Implement Selection Sort Algorithm C // C program for implementation of selection sort #include <stdio.h> void selectionSort(int arr[], int N) { // Start with the whole array as unsored and one by // one move boundary of unsorted subarray towards right for (int i = 0; i < N - 1; i++) { // Find the minimum element in unsorted array int min_idx = i; for (int j = i + 1; j < N; j++) { if (arr[j] < arr[min_idx]) { min_idx = j; } } // Swap the found minimum element with the first // element in the unsorted part int temp = arr[min_idx]; arr[min_idx] = arr[i]; arr[i] = temp; } } int main() { int arr[] = {64, 25, 12, 22, 11}; int N = sizeof(arr) / sizeof(arr[0]); printf("Unsorted array: \n"); for (int i = 0; i < N; i++) { printf("%d ", arr[i]); } printf("\n"); // Calling selection sort selectionSort(arr, N); printf("Sorted array: \n"); for (int i = 0; i < N; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; } OutputUnsorted array: 64 25 12 22 11 Sorted array: 11 12 22 25 64 Time Complexity: O(N2), as there are two nested loops.Auxiliary Space: O(1), as the only extra memory used is for temporary variable while swapping two values in Array.Advantages of Selection Sort in CSelection Sort is easy to understand and implement.It requires no additional memory beyond a few variables, making it space efficient.The number of comparisons and swaps is fixed, providing consistent performance regardless of the input order.Selection Sort minimizes the number of swaps, which can be useful in scenarios where writing to memory is costly.Disadvantages of Selection Sort in C The O(N2) time complexity makes Selection Sort inefficient for sorting large datasets, as it can be significantly slower than more advanced algorithms like QuickSort or MergeSort.Without modifications, Selection Sort may change the relative order of equal elements, which can be problematic in some applications.Selection Sort always makes the same number of comparisons, even if the array is already sorted, leading to unnecessary computations. Comment More infoAdvertise with us Next Article Selection Sort - Python kartik Follow Improve Article Tags : C Language Similar Reads Selection Sort Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted.First we find the smallest element an 8 min read Recursive Selection Sort The Selection Sort algorithm sorts maintain two parts. The first part that is already sortedThe second part is yet to be sorted. The algorithm works by repeatedly finding the minimum element (considering ascending order) from the unsorted part and putting it at the end of the sorted part. arr[] = 64 6 min read Time and Space complexity analysis of Selection Sort The Selection sort algorithm has a time complexity of O(n^2) and a space complexity of O(1) since it does not require any additional memory space apart from a temporary variable used for swapping. Time Complexity Analysis of Selection Sort:Best-case: O(n2), best case occurs when the array is already 2 min read Selection sort in different languagesJava Program for Selection SortThe selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from the unsorted part and putting it at the beginning.Algorithm for Selection SortImplementation of Selection Sort in Java is mentioned below:Step 1: Array arr with N sizeStep 2: Init 2 min read C Program for Selection SortThe selection sort is a simple comparison-based sorting algorithm that sorts a collection by repeatedly finding the minimum (or maximum) element and placing it in its correct position in the list. It is very simple to implement and is preferred when you have to manually implement the sorting algorit 4 min read Selection Sort - PythonSelection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted.First we find the smallest element an 2 min read Stable Selection Sort A sorting algorithm is said to be stable if two objects with equal or same keys appear in the same order in sorted output as they appear in the input array to be sorted.Any comparison based sorting algorithm which is not stable by nature can be modified to be stable by changing the key comparison op 6 min read Iterative selection sort for linked list Given a linked list, the task is to sort the linked list in non-decreasing order by using selection sort.Examples: Input : Linked List = 5 ->3 ->4 ->1 ->2Output : 1 ->2 ->3 ->4 ->5Input : Linked List = 5 ->4 ->3 ->2Output : 2 ->3 ->4 ->5Table of Content[Expe 15+ min read A sorting algorithm that slightly improves on selection sort As we know, selection sort algorithm takes the minimum on every pass on the array, and place it at its correct position.The idea is to take also the maximum on every pass and place it at its correct position. So in every pass, we keep track of both maximum and minimum and array becomes sorted from b 6 min read Program to sort an array of strings using Selection Sort Given an array of strings, sort the array using Selection Sort. Examples: Input : paper true soap floppy flower Output : floppy, flower, paper, soap, true Prerequisite : Selection Sort. C++ // C++ program to implement selection sort for // array of strings. #include <bits/stdc++.h> #include 7 min read Selection sort visualizer using PyGame In this article, we will see how to visualize Selection sort using a Python library PyGame. It is easy for the human brain to understand algorithms with the help of visualization. Selection sort is a simple and easy-to-understand algorithm that is used to sort elements of an array by dividing the ar 3 min read Like