- C Programming Examples
- C Programming Examples
- C Print Hello World
- C Get Input from User
- C Print Integer
- C Add Two Numbers
- C Check Even or Odd
- C Check Prime or Not
- C Check Alphabet or Not
- C Check Vowel or Not
- C Check Leap Year or Not
- C Check Reverse equal Original
- C Add Subtract Multiply Divide
- C Make Calculator
- C Add Digits of Number
- C Calculate Average, Percentage
- C Calculate Arithmetic Mean
- C Calculate Student Grade
- C Print Table of Number
- C Print Prime Numbers
- C Add n Numbers
- C Interchange Numbers
- C Reverse a Number
- C Swap Two Numbers
- C Count Positive Negative Zero
- C Find Largest of Two Numbers
- C Find Largest of Three Numbers
- C Find Factorial of Number
- C Find LCM & HCF
- C Find LCM of n Numbers
- C Find HCF of n Numbers
- C Area & Perimeter of Square
- C Area & Perimeter of Rectangle
- C Area & Circumference of Circle
- C Convert Fahrenheit to Celsius
- C Convert Celsius to Fahrenheit
- C Print ASCII Value
- C Print Fibonacci Series
- C Check Palindrome or Not
- C Check Armstrong or Not
- C Generate Armstrong Numbers
- C Find nCr and nPr
- C Convert Decimal to Binary
- C Convert Decimal to Octal
- C Convert Decimal to Hexadecimal
- C Convert Binary to Decimal
- C Convert Binary to Octal
- C Convert Binary to Hexadecimal
- C Convert Octal to Decimal
- C Convert Octal to Binary
- C Convert Octal to Hexadecimal
- C Convert Hexadecimal to Decimal
- C Convert Hexadecimal to Binary
- C Convert Hexadecimal to Octal
- C Pattern Printing Programs
- C Print Diamond Pattern
- C Print Floyd's Triangle
- C Print Pascal's Triangle
- C Print Smiling face
- C 1D Array Programs
- C Linear Search
- C Binary Search
- C Add Two Numbers using Pointer
- C Find Largest Element in Array
- C Find Smallest Element in Array
- C Reverse an Array
- C Insert Element in Array
- C Delete Element from Array
- C Merge Two Arrays
- C Bubble Sort
- C Selection Sort
- C Insertion Sort
- C 2D Array Programs
- C Add Two Matrices
- C Subtract Two Matrices
- C Transpose a Matrix
- C Multiply Two Matrices
- C 3D Array Programs
- C Print String
- C Find Length of String
- C Compare Two String
- C Copy a String
- C Concatenate String
- C Reverse a String
- C Delete Vowels from String
- C Delete Word from String
- C Find Frequency of Character
- C Count Word in String
- C Remove Spaces from String
- C Sort a String
- C Uppercase to Lowercase
- C Lowercase to Uppercase
- C Swap Two Strings
- C Check Anagram or Not
- C Generate Random Numbers
- C Read a File
- C Write Content to File
- C Read & Display File
- C Copy a File
- C Merge Two Files
- C List Files in Directory
- C Delete a File
- C Encrypt & Decrypt a File
- C Print Date
- C Get IP Address
- C Shutdown Computer
- C More Programs
- C Check Palindrome String
- C Inches to Centimetres
- C Kilogram to Gram
- C Count Even Odd
- C Count Vowels Consonants
- C Find Profit Loss
- C Sum All Matrix Elements
- C First & Last Digit Sum
- C Largest Element in Matrix
- C Pass Array to Function
- C Address of Variable
- C Array Element at Even
- C Array Element at Odd
- C Print Even Array Elements
- C Print Odd Array Elements
- C Reverse File
- C Print Message with Time
- C Print Number in Words
- C Print Successive Character
- C Sum of their Square
- C Replace Vowel in String
- C Sort Words in Ascending
- C Sort Words in Descending
- C Sort String in Alphabetical
- C Programming Tutorial
- C Tutorial
- C Programming Library
- C Standard Library
- C Programming Test
- C Programming Test
- Give Online Test
- All Test List

« Previous Program Next Program »

In this tutorial, we will learn about how to create a program in C that sorts an array using Selection sort technique. At last we have also created a function that can be used to sort any given array in ascending order using selection sort technique.

But before, going through the program, if you are not aware about how selection sort works, then refer to the step by step working of Selection Sort. Now let's move on and implement it in a C program.

To sort an array in ascending order using selection sort technique in C programming, you have to ask from user to enter the array size and its elements. Then apply the selection sort mechanism and print the sorted array as output just like the program given below:

// Selection Sort Program in C // ----codescracker.com---- #include<stdio.h> #include<conio.h> int main() { int size, arr[50], i, j, temp, small, count=0, index; printf("Enter size for Array: "); scanf("%d", &size); printf("Enter %d array elements: ", size); for(i=0; i<size; i++) scanf("%d", &arr[i]); for(i=0; i<(size-1); i++) { small = arr[i]; for(j=(i+1); j<size; j++) { if(small>arr[j]) { small = arr[j]; count++; index = j; } } if(count!=0) { temp = arr[i]; arr[i] = small; arr[index] = temp; } count=0; } printf("\nNow the Array after sorting is:\n"); for(i=0; i<size; i++) printf("%d ", arr[i]); getch(); return 0; }

As the above program was written under **Code::Blocks** IDE, therefore here is the sample run after successful build and run.
This is the first snapshot of sample run:

Now supply size for array say **5** and its element as **54 21 8 18 3** and press ENTER key to see the sorted array
as shown in the second snapshot of sample run given here:

Here is another sample run. This is the final snapshot:

- Receive size of array by user at run-time
- Receive elements of that size for array by user at run-time
- For example, if user has supplied 5 as array size, then ask him/her to enter 5 array elements
- Create a for loop that runs from 0 to one less than the (size-1) of array.
That is, if array's size is
**5**then run the loop from 0 to one less than**5-1**or**4** - Inside the loop, initialize element present at current index to a variable say
**small** - And using another
**for**loop (runs from 0 to one less than the size of array), check whether the value in**small**variable is greater than any element (present at index equals to value of**i+1**to (size-1 or 4)) or not - If it is, then initialize that element to
**small**variable and increment the**count**variable, then initialize the current value of**j**(inner**for**loop variable) to**index**variable (holds index number where smallest element presents) - Never forgot to initialize 0 to
**count**variable at start of the program - Now after exiting from the inner
**for**loop, check whether**count**holds any value other than 0 or not - If it holds any value other than 0, then program flow gone inside the
if statement of inner
**for**loop. And it means, any element was initialized to**small**variable - Therefore, we have to perform swapping operation, that is place element at beginning to any variable say
**temp**, and then initialize value of**small**at beginning, and at last value of**temp**is initialized at that index from where the element was initialized to**small**variable before swapping - After swapping, initialize 0 to
**count**variable and continue - For example, if user has entered 5 as array size and
**54 21 8 18 3**as array elements - Then at first run of
**for**loop,**i**holds 0. And**i<(size-1)**or**0<0(5-1)**or**0<4**evaluates to be true. Therefore program flow goes inside the loop - The value at
**arr[0]**or**54**gets initialized to**small**. Therefore**small**holds 54 - Now in inner
**for**loop, and value of**i+1**(0+1) or**1**gets initialized to**j**(inner loop's variable). Therefore**j**holds 1. And 1 is less than**size**or**5**. Therefore program flow goes inside inner**for**loop - Now the statement
**small>arr[j]**or**54>arr[1]**or**54>21**gets evaluated to be true, therefore**arr[j]**or**arr[1]**or**21**gets initialized to**small**. The value of**count**gets incremented and becomes 1, and value of**j**or**1**gets initialized to**index** - Again value of
**j**gets incremented and compared that whether**j**is less than**size**or not, that is**j (2)**is less than**size(5)**, therefore again program flow goes inside the loop - And again
**small>arr[j]**or**21>arr[2]**or**21>8**evaluates to be true, therefore after swapping - The variable
**small**holds**8**,**count**holds**2**,**index**holds 2 - Again value of
**j**gets incremented and compared that whether**j**is less than**size**or not, that is**j (3)**is less than**size(5)**, therefore again program flow goes inside the loop - And again
**small>arr[j]**or**8>arr[3]**or**8>18**evaluates to be false, therefore no swapping gets performed - The variable
**small**holds**8**,**count**holds**2**,**index**holds 2 - Again value of
**j**gets incremented and compared that whether**j**is less than**size**or not, that is**j (4)**is less than**size(5)**, therefore again program flow goes inside the loop - And again
**small>arr[j]**or**8>arr[4]**or**8>3**evaluates to be true, therefore after swapping - The variable
**small**holds**3**,**count**holds**3**,**index**holds 4 - Again value of
**j**gets incremented and compared that whether**j**is less than**size**or not, that is**j (5)**is less than**size(5)**or not. Here the condition evaluates to be false and program flow goes back at outer**for**loop and increments the value of**i**. Then checks the condition, that whether**i**is less than**size-1**or**5-1**or**4**or not. It evaluates to be true, therefore program flow again goes inside the loop and do the similar steps as told above until the condition of outer**for**loop gets evaluated as false

If you want to print array elements as step by step sorting of array elements shown on output screen along with its final array. Then you can modify the above code as shown below to print array elements after each sorting step:

// Selection Sort Program with all Steps in C // ----codescracker.com---- #include<stdio.h> #include<conio.h> int main() { int size, arr[50], i, j, temp, small, count=0, index; printf("Enter size for Array: "); scanf("%d", &size); printf("Enter %d array elements: ", size); for(i=0; i<size; i++) scanf("%d", &arr[i]); for(i=0; i<(size-1); i++) { small = arr[i]; for(j=(i+1); j<size; j++) { if(small>arr[j]) { small = arr[j]; count++; index = j; } } if(count!=0) { temp = arr[i]; arr[i] = small; arr[index] = temp; } printf("\nStep %d: ", i+1); for(j=0; j<size; j++) printf("%d ", arr[j]); printf("\n"); count=0; } printf("\nNow the Array after sorting is:\n"); for(i=0; i<size; i++) printf("%d ", arr[i]); getch(); return 0; }

Here is the final snapshot of the sample run:

Let's create the same program, that is to sort an array in ascending order using selection sort technique, but this time, with the
help of function. Here selection sort is implemeted inside a function named **selsort()** that
uses two arguments, one is array and second is its size as shown in the program given below:

// Selection Sort Program using Function // ----codescracker.com---- #include<stdio.h> #include<conio.h> void selsort(int arr[], int size); int main() { int size, arr[50], i; printf("Enter size for Array: "); scanf("%d", &size); printf("Enter %d array elements: ", size); for(i=0; i<size; i++) scanf("%d", &arr[i]); selsort(arr, size); printf("\nThe sorted Array is:\n"); for(i=0; i<size; i++) printf("%d ", arr[i]); getch(); return 0; } void selsort(int arr[], int size) { int i, j, temp, small, count=0, index; for(i=0; i<(size-1); i++) { small = arr[i]; for(j=(i+1); j<size; j++) { if(small>arr[j]) { small = arr[j]; count++; index = j; } } if(count!=0) { temp = arr[i]; arr[i] = small; arr[index] = temp; } count=0; } }

Here is the final snapshot of the above program:

The concept used in above program is similar as used in first program except we have implemented a function **selsort()** here.
All the selection sort technique is implemented inside this function. We only have to pass any array and its size to this function
as its argument. And all the elements inside the array gets sorted in ascending order.

« Previous Program Next Program »

© Copyright 2021. All Rights Reserved.