SELECTION SORT
Implemented using C Language
// SELECTION SORTING using C Language
#include <stdlib.h>
#include <stdio.h>
#include <process.h>
#define NUM_ITEMS 10
void selectionSort(int numbers[], int array_size);
void prnArray(int numbers[], int array_size) ;
int numbers[NUM_ITEMS];
int main()
{
int i;
clrscr() ;
//seed random number generator
srand(getpid());
//fill array with random integers
for (i = 0; i < NUM_ITEMS; i++)
numbers[i] = rand();
printf("\n Before SELECTION Sort.\n");
prnArray(numbers, NUM_ITEMS) ;
//perform selection sort on array
selectionSort(numbers, NUM_ITEMS);
printf("\n After SELECTION Sort.\n");
prnArray(numbers, NUM_ITEMS) ;
}
void selectionSort(int numbers[], int array_size)
{
int i, j;
int min, temp;
for (i = 0; i < array_size-1; i++)
{
min = i;
for (j = i+1; j < array_size; j++)
{
if (numbers[j] < numbers[min])
min = j;
}
temp = numbers[i];
numbers[i] = numbers[min];
numbers[min] = temp;
}
}
void prnArray(int numbers[], int array_size)
{
int x ;
for(x=0; x < array_size; x++)
printf("%d\t", numbers[x]) ;
}
No comments:
Post a Comment