Selection Sort

 avatar
shebom640
c_cpp
a year ago
700 B
5
Indexable
Never
#include <stdio.h>

void selectionSort (int a[], int n)
{
  int i, j, min;
  for (i = 0; i < n - 1; i++)
    {
      min = i;
      for (j = i + 1; j < n; j++)
	{
	  if (a[j] < a[min])
	    {
	      min = j;
	    }
	}
    
  int temp = a[min];
  a[min] = a[i];
  a[i] = temp;
    }
}

int main ()
{
    int n, i, ele;
    printf("Enter Number of elements \n");
    scanf("%d", &n);
    int a[n];
    printf("Enter elements of an array \n");
    for(i = 0; i < n; i++) {
        printf("Enter %d number:\t", i+1);
        scanf("%d", &a[i]);
    }
    selectionSort(a, n);
    printf("Sorted Array: \n");
    for(i =0; i<n; i++) {
        printf("%d \t", a[i]);
    }
    printf("\n");
    return 0;
}