Advertisement
nguyenvanquan7826

SelectionSort

May 15th, 2013
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void input(int a[], int &n)
  5. {
  6.     printf("Enter the size of Array : ");
  7.     scanf("%d",&n);
  8.     for (int i=0; i<n; i++)
  9.     {
  10.         printf("Enter the member %d : ", i);
  11.         scanf("%d",&a[i]);
  12.     }
  13. }
  14.  
  15.  
  16. void SelectionSort(int a[], int n)
  17. {
  18.     int min;
  19.     for(int i=0;i<n-1;i++)
  20.     {
  21.         min=i;
  22.         for(int j=i+1;j<n;j++)
  23.             if(a[j]<a[min]) min=j;
  24.        
  25.         int temp = a[i];
  26.         a[i] = a[min];
  27.         a[min] = temp;
  28.     }
  29. }
  30.  
  31. void output(int a[], int n)
  32. {
  33.     for(int i=0; i<n; i++)
  34.         printf("%5d",a[i]);
  35.     printf("\n");
  36. }
  37.  
  38. int main()
  39. {
  40.     int a[1000], n;
  41.     input(a,n);
  42.     printf("Array inputed : \n");
  43.     output(a,n);
  44.    
  45.     SelectionSort(a,n);
  46.     printf("Array sorted: \n");
  47.     output(a,n);
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement