From 2c6110a39813a76dc231c3e743f7dea3e313df4d Mon Sep 17 00:00:00 2001 From: Tanishq Agarwal Date: Fri, 14 Oct 2022 22:53:54 +0530 Subject: [PATCH] Selection Sort by 047pegasus Commit --- Algorithems/Sort/Selection.c | 55 ++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Algorithems/Sort/Selection.c 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