programming-examples/c/Search_Sorting/C Program to Implement Selection Sort.c
2019-11-15 12:59:38 +01:00

38 lines
864 B
C

/*
* C Program to Implement Selection Sort
*/
#include <stdio.h>
void selectionSort(int arr[], int size)
{
int i, j;
for (i = 0 ; i < size; i++)
{
for (j = i ; j < size; j++)
{
if (arr[i] > arr[j])
swap(&arr[i], &arr[j]);
}
}
}
//fucntion to swap to variables
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int array[10], i, size;
printf("How many numbers you want to sort: ");
scanf("%d", &size);
printf("\nEnter %d number", size);
for (i = 0; i < size; i++)
scanf("%d", &array[i]);
selectionSort(array, size);
printf("\nSorted array is ");
for (i = 0; i < size; i++)
printf(" %d ", array[i]);
return 0;
}