Skip to content

Instantly share code, notes, and snippets.

@Kinjalrk2k
Created April 13, 2019 09:26
Show Gist options
  • Save Kinjalrk2k/64b80bd36294403014322840ef0dc04e to your computer and use it in GitHub Desktop.
Save Kinjalrk2k/64b80bd36294403014322840ef0dc04e to your computer and use it in GitHub Desktop.
#include <stdio.h>
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void selectionSort(int arr[], int n)
{
int i, j, min_idx;
for (i = 0; i < n-1; i++)
{
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
swap(&arr[min_idx], &arr[i]);
}
}
void printarr(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int arr[] = {45, 98, 2, 87, 14, 7, 25, 45};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array: \n");
printarr(arr, n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment