diff --git a/Algorithems/Sort/Selection.c b/Algorithems/Sort/Selection.c new file mode 100644 index 0000000..6a95898 --- /dev/null +++ b/Algorithems/Sort/Selection.c @@ -0,0 +1,55 @@ +#include +#include +// This program is developed by Tanishq Agarwal(047pegasus) +//This file contains the implementation of Selection sort in C Programming language. +int* selectionSort(int arr[], int n) +{ + int i, j, min,temp; + for (i = 0; i < n - 1; i++) + { + min = i; + for (j = i + 1; j < n; j++) + { + if (arr[j] < arr[min]) + { + min = j; + } + } + temp=arr[min]; + arr[min]=arr[i]; + arr[i]=temp; + // swap(&arr[min], &arr[i]); can be used in c++ to swap elements directly without its implimenattion. + } + return arr; +} + +int main(){ + int n_ele=0; + printf("Enter the no of elements in the array:"); + scanf("%d",&n_ele);//size of the array + int *inp_arr= (int *)(malloc(n_ele*sizeof(int))); //array memory allocation + + //input array + for (int i = 0; i < n_ele; i++) + { + scanf("%d",&inp_arr[i]); + } + + printf("\n Before sorting the array: \n"); + + // printing unsorted array recieved from user + for(int i=0;i