Advertisement
Crazy

Непарно парно сортирање

Nov 19th, 2017
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class OddEvenSort {
  6.    
  7.     static void oddEvenSort(int a[], int n)
  8.     {
  9.         for(int i=0; i<n; i++){
  10.            
  11.             for(int j=0; j<i; j++){
  12.            
  13.                 if(a[i] % 2 !=0&&a[j] %2 == 0){
  14.                     int tmp = a[i];
  15.                     a[i] = a[j];
  16.                     a[j] = tmp;
  17.                
  18.                 }
  19.                 if(a[i] > a[j] && a[i] % 2 == 0 && a[j] % 2 == 0){
  20.                     int tmp = a[i];
  21.                     a[i] = a[j];
  22.                     a[j] = tmp;
  23.                 }
  24.                 if(a[i] < a[j] && a[i] % 2 != 0 && a[j] % 2 != 0){
  25.                     int tmp = a[i];
  26.                     a[i] = a[j];
  27.                     a[j] = tmp;
  28.                 }
  29.             }
  30.            
  31.         }
  32.     }
  33.  
  34.     public static void main(String[] args) throws IOException{
  35.         int i;
  36.         BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in));
  37.         String s = stdin.readLine();
  38.         int n = Integer.parseInt(s);
  39.        
  40.         s = stdin.readLine();
  41.         String [] pom = s.split(" ");
  42.         int [] a = new int[n];
  43.         for(i=0;i<n;i++)
  44.             a[i]=Integer.parseInt(pom[i]);
  45.         oddEvenSort(a,n);
  46.         for(i=0;i<n-1;i++)
  47.             System.out.print(a[i]+" ");
  48.         System.out.print(a[i]);
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement