F How to perform Selection Sorting in an integer array? | CodeTheta

How to perform Selection Sorting in an integer array?

April 16, 2014

/* http://native-code.blogspot.com */

#include<stdio.h>
#include<conio.h>
#define MAX 20
void selection_sort(int [], int);
void display(int [], int);
void main()
{
int a[MAX], i, max;
printf("/* http://native-code.blogspot.com */ \n\nEnter the maximum range:\n");
scanf("%d", &max);
printf("\nEnter elements:\n");
for(i=0; i<max; i++)
scanf("%d", &a[i]);
printf("\nBefore sorting the array is:\n");
display(a, max);
selection_sort(a, max);
printf("\nAfter sorting the array is:\n");
display(a, max);
getch();
}
void selection_sort(int a[], int max)
{
int i, j, temp, min;
i=j=temp=min=0;
for(i=0; i<max; i++)
{
min=i;
for(j=i+1; j<max; j++)
{
if(a[j]<a[min])
{
min=j;
}
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
void display(int a[], int max)
{
int i;
for(i=0; i<max; i++)
printf("%d ", a[i]);
}

/* http://native-code.blogspot.com */

Post a Comment