selection_Sort

 #include<stdio.h>

#include<stdlib.h>
#include<time.h>

void swap(int *x, int *y){
  int temp = *x;
  *x = *y;
  *y = temp;
}

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

int main(){
  clock_t start_time = clock();
  srand(time(NULL));
  int n;
  printf("Enter the no. of element: ");
  scanf("%d",&n);
  int a[n];
  for(int i=0;i<n;i++){
    a[i] = rand() % 100;
  }

  selectionSort(a,n);
  clock_t stop_time = clock();

  printf("sorted elements are: ");
  for(int i=0;i<n;i++){
    printf("%d ",a[i]);
  }
  printf("\n");

  double timetake = (double)(stop_time-start_time)/CLOCKS_PER_SEC;
  printf("%lf",timetake);
  return 0;
}

Comments