C Program For Selection Sort by Generating Random Numbers and Using time.h Function
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
void selection_sort(int[],int);
int main()
{
int list[100],n,i;
time_t t;
printf("Enter the max number \n");
scanf("%d",&n);
srand((unsigned) time(&t));
for(i=0;i<n;i++)
{
list[i]=rand()%100;
}
for(i=0;i<n;i++)
{
printf("%d\t",list[i]);
}
selection_sort(list,n);
printf("\nTime taken to complete the selectionsort %u",clock()/CLOCKS_PER_SEC);
printf("\nThe sorted list is:\n");
for(i=0;i<n;i++)
printf("%d\t",list[i]);
return 0;
}
void selection_sort(int list[],int n)
{
int temp,loc,j;
for(loc=0;loc<n-1;loc++)
{
for(j=loc+1;j<n;j++)
{
if(list[loc]>list[j])
{
temp=list[loc];
list[loc]=list[j];
list[j]=temp;
}
}
}
}
Related Posts
Bubble SortInsertion Sort
Merge Sort
Selection Sort
Quick Sort
C Program For Quick Sort | C Programming
C Program For Merge Sort | C Programming
C Program For Bubble Sort | C Programming
C Program for Insertion Sort | C Programming
Post A Comment:
0 comments so far,add yours