Advertisement
AbirAbrar

Selection Sort algorithm

Dec 17th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.64 KB | None | 0 0
  1.  
  2. #include<stdio.h>
  3. #include<limits.h>
  4.  
  5.  
  6. int main()
  7. {
  8.     int arr[100],n,i,j,t;
  9.     arr[0]=INT_MIN; /// we keep minus infinity to 0th position
  10.     printf("Enter the size of array: \n");
  11.     scanf("%d",&n);
  12.     printf("Enter the array elements: \n");
  13.  
  14.     for(i=1;i<=n;++i)
  15.     {
  16.       scanf("%d",&arr[i]);
  17.     }
  18.  
  19.     for(j=2;j<=n;j++)
  20.     {
  21.         i=j-1;
  22.         t=arr[j];
  23.  
  24.         while(t<arr[i])
  25.         {
  26.             arr[i+1]=arr[i];
  27.             i--;
  28.         }
  29.         arr[i+1]=t;
  30.     }
  31.  
  32.  
  33.     printf("After Insertion sort: \n");
  34.  
  35.     for(i=1;i<=n;i++)
  36.     {
  37.         printf("%d ",arr[i]);
  38.     }
  39.  
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement