Advertisement
Lisaveta777

sort very easy

Apr 22nd, 2022
1,036
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.43 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3.                             Online C Compiler.
  4.                 Code, Compile, Run and Debug C program online.
  5. Write your code in this editor and press "Run" button to compile and execute it.
  6.  
  7. *******************************************************************************/
  8.  
  9. #include <stdio.h>
  10. #define SIZE 10
  11.  
  12. int main()
  13. {
  14.     int i,j, temp,arr[SIZE]={2,7,8,1,4,3,6,9,10,5};
  15.    
  16.      printf("ARRAY BEFORE SORTING:\n");  
  17.     for(i=0;i<SIZE;i++)//print an array
  18.         printf("%d\t",arr[i]);
  19.         printf("\n");
  20.    
  21.        
  22.     for(i=0;i<SIZE;i+=2)//sort array of elements i = 0, 2, 4, 6, 8
  23.     {
  24.        
  25.         for(j=i+2;j<SIZE;j+=2)
  26.         {
  27.             if(arr[i]<arr[j])//noneven
  28.             {
  29.                
  30.                 temp = arr[i];
  31.                 arr[i] = arr[j];
  32.                 arr[j]= temp;
  33.             }
  34.         }
  35.     }
  36.    
  37.     for(i=1;i<SIZE;i+=2)//sort array of elements i=1, 3, 5, 7
  38.     {
  39.        
  40.         for(j=i+2;j<SIZE;j+=2)
  41.         {
  42.             if(arr[i]>arr[j])//noneven
  43.             {
  44.                
  45.                 temp = arr[i];
  46.                 arr[i] = arr[j];
  47.                 arr[j]= temp;
  48.             }
  49.         }
  50.     }
  51.    
  52.     printf("ARRAY AFTER SORTING:\n");  
  53.     for(i=0;i<SIZE;i++)//print an array
  54.         printf("%d\t",arr[i]);
  55.         printf("\n");
  56.    
  57.  
  58.     return 0;
  59. }
  60.  
  61.  
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement