Advertisement
Shailrshah

Selection Sort

Nov 7th, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. void swap(int *a ,int *b){
  4.     int temp = *a;
  5.     *a = *b;
  6.     *b = temp;
  7. }
  8. void selectionSort(int a[], int n){
  9.     int i, j, min;
  10.     for(i = 0; i < n - 1; i++){
  11.         min = i;
  12.         for(j = i + 1; j < n; j++)
  13.             if(a[j] < a[min]) min = j;
  14.         if(min != i) swap(&a[min], &a[i]);
  15.     }  
  16. }
  17. int main(){
  18.     int *a, n, i;
  19.     printf("Enter the number of elements: ");
  20.     scanf("%d", &n);
  21.     a = (int *) malloc(sizeof(int) *n);
  22.     printf("Enter %d elements:-\n", n);
  23.     for(i = 0; i < n; i++) scanf("%d", &a[i]);
  24.     selectionSort(a, n);
  25.     for(i = 0; i < n; i++) printf("\t%d\t", a[i]);
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement