## Definition of Selection sort in C

Selection Sort in C is a very important algorithm for performing sorting on set of elements present in an array. The sorting algorithm performs sorting on set of elements in a way where there are elements present in the array and then traverses to perform sorting by finding the smallest element first. Once the smallest element is found that element will get swapped if the next element at the time of traversal is smaller than the current element or next element. In a similar fashion, it performs the traversal until all the elements present within the array get sorted.

**Syntax:**

There is no specific syntax for representation of selection sort in C but still, there is some pattern which is followed to get the Selection sort implemented and traversed, it is represented as below:

`# include<stdio.h>`

int main()

{

Declaration of array containing elements or numbers []
Print the elements

Get the elements within loop stored in array properly

Apply the logic for selection sorting to sort the algorithm

Apply the selection sorting algorithm in the iteration till all the elements get sorted.

}

### How Selection sort works in C?

Selection sort in C works in a way where the algorithm takes the responsibility to sort all the elements present within the array. The selection sort algorithm sorts an array by performing selection sorting on the array till it gets sorted. Selection sort has a mechanism where the element is searched by traversing from start till the end by replacing the smallest element found within the array.

Once the smallest element is found within the array that gets altered with the next element and it keeps on repeating till all the elements get sorted in a proper manner. Basically, the algorithm maintains two subarrays within the given array where the two probabilities arise in the case of two subarrays defined so far. The probability says that the subarray might be already kept sorted or it might be that the remaining part of the subarray remains unsorted.

In every traversal with iteration, the selection sort algorithm is performed and the smallest element present within the array is picked from the range of unsorted array and then gets substituted to the sorted array. Sorting algorithm works in a way where the elements are sorted in ascending order with the smallest element placed at first. In further, the example will clear the process of performing Selection sorting in the C language.

The algorithmic flow for the Selection sort is as follows :

`Selection_Sort (arr_0, size_0)`

Repeat (size-1) times

Set the first unsorted element as smallest

For each elements in un_sorted format representation comes like

If element in arr_0 < current_element in arr_0

Set element as smallest

Swap the smallest element found with the new smallest element in un_sorted arr_0

End Selection_Sort

There are some performance metrics that exist to get an effective performance analysis and get the complexity of the entire program in execution.

The entire cycle in the program performs iterations that have their own computational values like Cycle starts from the 1st iteration till the last where the 1st iteration has (n-1) comparison followed by 2nd iterative cycle where the number of comparisons comes out to be (n-2) and 3rd iterative cycle having the number of comparative value as (n-3) and so on therefore if in case there exist n elements for an iterative cycle then there the last element after repeated iterations and sorting comes out to be 1.

Therefore, after a number of comparisons performed will have the time complexity nearly equal to O(n2).

Followed by this there are scenarios where the time complexity of selection sort is calculated with respect to worst time complexity where the complexity comes out to be square of n for sorting either in ascending or descending order.

3 Online Courses | 5 Hands-on Projects| 34+ Hours| Verifiable Certificate of Completion

4.5

View Course

The best-case scenario also includes the same where the array is already sorted order then also, in that case, it comes out to be O of n square only.

If in case the sorting needs to be applied on elements of an array that are jumbled then in that average case also it comes out to be O of n square only.

### Example

This program demonstrates the Selection Sorting algorithm performed on series of elements as shown in the output below.

`#include <stdio.h>`

void swap_1 (int *r_0, int *z_9)

{

int tm_str = *r_0;

*r_0 = *z_9;

*z_9 = tm_str;

}

void selecn_Sort (int arr_0[], int sz)

{

for (int st = 0; st < sz - 1; st++)

{

int mn_indx = st;

for (int j = st + 1; j < sz; j++)

{

if (arr_0[j] < arr_0[mn_indx])

mn_indx = j;

}

swap_1(&arr_0[mn_indx], &arr_0[st]);

}

}

void print_arr (int arr_1[], int sz_1)

{

for (int k = 0; k < sz_1; ++k) {

printf("%d ", arr_1[k]);

}

printf("\n");

}

int main()

{

int data_0[] = {18, 40, 25, 05, 12};

int sz = sizeof(data_0) / sizeof(data_0[0]);

selecn_Sort(data_0, sz);

printf("Asccending_Order_applying_Selection_Sort:\n");

print_arr(data_0, sz);

}

**Output:**

**Explanation:**

At First, the system asks for the input of elements within the array and once stored then applies the selection sort algorithm. Then a function called void swap_1 is written in order to swap the two elements from their respective positions. Then Write the selection sort algorithm with the required logic for implementation and swapping of elements.

Perform sorting in descending order and change to whether less than or more than by selecting the smallest element in each loop. Put the smallest element in the proper location as it will be referred to later at some point in time. Then a function pointing to an array and is printing the respective data is used. The final driver function is calling for making the code execute and carry forward to give the final output.

### Conclusion

Selection Sort is quite an important sorting algorithm in the C programming language. It gets used most often at the time of implementation by the developers as per requirement. The time complexity it holds is used for verities of logic implementation where the sorting requires quite frequently. It finds the smallest element from the entire series of array by comparison performed on every iteration.

### Recommended Articles

This is a guide to Selection sort in C. Here we discuss the definition, syntax, and How Selection sort works in C? with examples. You may also have a look at the following articles to learn more –