Advertisement
Waliullah8328

Selection Sort Accending and Decending

Jun 10th, 2021
1,099
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.06 KB | None | 0 0
  1. #include<stdio.h>
  2. int main()
  3. {
  4.     int arr[10],n,i,j,temp,min,max;
  5.     printf("Enter how many Element you want to insert : \n");
  6.     scanf("%d",&n);
  7.     printf("Enter your Element  : \n");
  8.     for(i=0; i<n; i++)
  9.     {
  10.         scanf("%d",&arr[i]);
  11.     }
  12.     // Selection sorting Start
  13.     for(i=0;i<n-1;i++)
  14.     {
  15.         min=i;
  16.         for(j=i+1;j<n;j++)
  17.         {
  18.  
  19.             if(arr[j]<arr[min])
  20.             {
  21.                 min = j;
  22.             }
  23.         }
  24.         temp = arr[i];
  25.         arr[i] = arr[min];
  26.         arr[min]= temp;
  27.     }
  28.         printf("Accending Order : \n");
  29.  
  30.     for(i=0; i<n; i++)
  31.     {
  32.         printf("%d\n",arr[i]);
  33.     }
  34.     for(i=0;i<n-1;i++)
  35.     {
  36.         max=i;
  37.         for(j=i+1;j<n;j++)
  38.         {
  39.  
  40.             if(arr[max]< arr[j])
  41.             {
  42.                 max = j;
  43.             }
  44.         }
  45.         temp = arr[i];
  46.         arr[i] = arr[max];
  47.         arr[max]= temp;
  48.     }
  49.         printf("Decending Order : \n");
  50.  
  51.     for(i=0; i<n; i++)
  52.     {
  53.         printf("%d\n",arr[i]);
  54.     }
  55.     return 0;
  56.  
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement