Write a Program to Sort an Array using Selection Sort in C with a practical example of for loop, while loop, and functions.
C Program for Selection Sort using For Loop
This program uses Nested For Loop to sort the array elements using selection sort.
// using nested for loop #include <stdio.h> int main() { int a[100], number, i, j, temp; printf("\n Please Enter the total Number of Elements : "); scanf("%d", &number); printf("\n Please Enter the Array Elements : "); for(i = 0; i < number; i++) scanf("%d", &a[i]); for(i = 0; i < number; i++) { for(j = i + 1; j < number; j++) { if(a[i] > a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } printf("\n Selection Sort Result : "); for(i = 0; i < number; i++) { printf(" %d \t", a[i]); } printf("\n"); return 0; }
Here, in this C selection sort program, the For Loop will make sure that the number is between 1 and maximum size – 1.
First For Loop – First Iteration: for(i = 0; 0 < 4; 0++)
The condition is True so, it will enter into the second for loop.
Second For Loop – First Iteration: for(j = 1; 1 < 4; 1++) – This condition is True
if(a[0] > a[1]) => (10 > 9) – This condition is True
temp = 10
a[i] = a[j] => a[0] = 9
a[j] = temp => a[1] = 10
Now the array will be 9 10 0 11.
Second For Loop – Second Iteration: for(j = 2; 2 < 4; 2++) – True
if(a[0] > a[2]) => (9 > 0) – Condition is True
temp = a[0] => temp = 9
a[0] = a[2] => a[0] = 0
a[2] = temp => a[2] = 9
Now the array will be 0 10 9 11.
Do the same for the remaining Iterations of the second for loop. Next, repeat the same with the first C Programming for loop.
C Program for Selection Sort using for loop
In this selection sort program, we used a slightly different approach to demonstrate this.
#include <stdio.h> int main() { int a[100], num, min, i, j, temp; printf("\n Please Enter the total Elements : "); scanf("%d", &num); printf("\n Please Enter the Array Elements : "); for(i = 0; i < num; i++) scanf("%d", &a[i]); for(i = 0; i < num - 1; i++) { min = i; for(j = i + 1; j < num; j++) { if(a[min] > a[j]) { min = j; } } if(min != i) { temp = a[i]; a[i] = a[min]; a[min] = temp; } } printf("\n Result : "); for(i = 0; i < num; i++) { printf(" %d \t", a[i]); } printf("\n"); return 0; }
Please Enter the total Elements : 5
Please Enter the Array Elements : 10 2 19 0 10
Result : 0 2 10 10 19
C Selection Sort Program using While Loop
In this program, we replaced the for loop with While Loop.
#include <stdio.h> int main() { int a[100], val, i, j, temp; printf("\n Please Enter the total No. of Elements : "); scanf("%d", &val); printf("\n Please Enter the Array Elements : "); for(i = 0; i < val; i++) scanf("%d", &a[i]); i = 0; while( i < val) { j = i + 1; while( j < val) { if(a[i] > a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } j++; } i++; } printf("\n Result : "); for(i = 0; i < val; i++) { printf(" %d \t", a[i]); } printf("\n"); return 0; }
Please Enter the total No. of Elements : 7
Please Enter the Array Elements : 20 0 25 19 4 22 16
Result : 0 4 16 19 20 22 25
C Selection Sort Program using Functions
This program uses Functions to separate the logic and Pointers to swap and rearrange the array elements by using this selection sort.
#include <stdio.h> void Swap(int *x, int *y) { int Temp; Temp = *x; *x = *y; *y = Temp; } void selFunc(int a[], int number) { int i, j; for(i = 0; i < number; i++) { for(j = i + 1; j < number; j++) { if(a[i] > a[j]) { Swap(&a[i], &a[j]); } } } } int main() { int a[100], number, i; printf("\n Please Enter the total Number of Elements : "); scanf("%d", &number); printf("\n Please Enter the Array Elements : "); for(i = 0; i < number; i++) scanf("%d", &a[i]); selFuc(a, number); printf("\n Result : "); for(i = 0; i < number; i++) { printf(" %d \t", a[i]); } printf("\n"); return 0; }
Please Enter the total Number of Elements : 6
Please Enter the Array Elements : 12 3 0 22 19 -8
Result : -8 0 3 12 19 22