Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selection sort implemented in C #82

Merged
merged 1 commit into from
Oct 13, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions Algorithms/C/Selection_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Name: Ayus Das
//Email: [email protected]
/*This is a program of sorting. The sorting algorithm
*used here is insertion sorting. Array elements are taken
*by user as input
*/
#include <stdio.h>

//Function to swap the elements
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

//Function for selection sort
void selectionSort(int arr[], int n)
{
int i, j, min_idx;

// One by one move boundary of unsorted subarray
for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;

// Swap the found minimum element with the first element
swap(&arr[min_idx], &arr[i]);
}
}



// Driver program to test above functions
int main()
{
int number_of_elements;
printf("Enter the size of the array: \n");
scanf("%d",&number_of_elements);
int array_unsorted[number_of_elements];
int i;
printf("Enter the array elements: \n");
for(i=0;i<number_of_elements;i++)
{
scanf("%d",&array_unsorted[i]);
}
selectionSort(array_unsorted, number_of_elements);
printf("Sorted array is : \n");
for(i=0;i<number_of_elements;i++)
{
printf("%d ",array_unsorted[i]);
}
return 0;
}