Simple Selection Sort Program using functions in C
Definition
Selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n2) time complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort. Selection sort is noted for its simplicity, and it has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory is limited.
Simple Selection Sort Program using functions
/* Simple Selection Sort Program Using Functions and Array in C*/
/* Data Structure Programs,C Functions and Array Examples */
#include<stdio.h>
#include<conio.h>
#define MAX_SIZE 5
void selection_sort(int[]);
int main() {
int arr_sort[MAX_SIZE], i;
printf("Simple Selection Sort Example - Functions and Array\n");
printf("\nEnter %d Elements for Sorting\n", MAX_SIZE);
for (i = 0; i < MAX_SIZE; i++)
scanf("%d", &arr_sort[i]);
printf("\nYour Data :");
for (i = 0; i < MAX_SIZE; i++) {
printf("\t%d", arr_sort[i]);
}
selection_sort(arr_sort);
getch();
}
void selection_sort(int fn_arr[]) {
int i, j, a, t, p;
for (i = 0; i < MAX_SIZE; i++) {
p = i;
for (j = i; j < MAX_SIZE; j++) {
if (fn_arr[p] > fn_arr[j])
p = j;
}
if (p != 1) {
//Swapping Values
t = fn_arr[i];
fn_arr[i] = fn_arr[p];
fn_arr[p] = t;
}
printf("\nIteration %d : ", i);
for (a = 0; a < MAX_SIZE; a++) {
printf("\t%d", fn_arr[a]);
}
}
printf("\n\nSorted Data :");
for (i = 0; i < MAX_SIZE; i++) {
printf("\t%d", fn_arr[i]);
}
}
Sample Output
Simple Selection Sort Example - Functions and Array
Enter 5 Elements for Sorting
56
12
34
11
2
Your Data : 56 12 34 11 2
Iteration 0 : 2 12 34 11 56
Iteration 1 : 2 11 34 12 56
Iteration 2 : 2 11 12 34 56
Iteration 3 : 2 11 12 34 56
Iteration 4 : 2 11 12 34 56
Sorted Data : 2 11 12 34 56
------------------
(program exited with code: 0)
C Sorting Programs
- Simple Bubble Sort Program in C
- Simple Bubble Sort Program using functions in C
- Simple Insertion Sort Program in C
- Simple Insertion Sort Program using functions in C
- Simple Selection Sort Program in C
- Simple Selection Sort Program using functions in C
- Simple Quick Sort Program in C
- Simple Merge Sort Program in C
- Simple Shell Sort Program in C
- Simple Shell Sort Program using functions in C
- Simple Heap Sort Program in C
Read More Articles
- Use of getch(),getche() and getchar() in C
- Switch Case Statement Example Program In C Programming Language
- C Character Set
- Convert a Floating-point value to an Integer in C
- Data Input and Output gets and puts Example Program In C
- Special Operators In C
- Pointer Representation and Pointer Example Programs
- C Data Input and Data Output
- Simple While Loop Example Program In C Programming Language
- Data Output printf and putchar Example Program In C
- C Introduction
- C Operators
- Storage Classes In C
- C Pointers
- File Management
- C Identifiers
- Loop Control Statements
- Hello World - Simple C Program
- C Array
- Single Character Output Function : putchar()
- C Reserve Words
- C Specific Properties and Implementation
- If else Statement Example Program In C Programming Language
- If Statement Example Program In C Programming Language
- Confusing Array in C ( Array Representation and Initialization )