Advertisement
NenadKocev

[АПС] Непарно парно сортирање

Nov 21st, 2017
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.06 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.     public static boolean odd(int broj){
  8.         return (broj % 2 != 0);
  9.     }
  10.  
  11.     public static boolean even(int broj){
  12.         return (broj % 2 == 0);
  13.     }
  14.  
  15.  
  16.     static void oddEvenSort(int a[], int n)
  17.     {
  18.         // Vasiot kod tuka
  19.         int o = 0, n1 = n;
  20.         //podelba na neparni- parni
  21.         for(int i = 0; i < n; i++){
  22.             if(even(a[i])){
  23.                 int m = n1 - 1;
  24.                 for( m = n - 1; m > i; m--){
  25.                     if(odd(a[m])){
  26.                         int temp = a[i];
  27.                         a[i] = a[m];
  28.                         a[m] = temp;
  29.                         o++;
  30.                         break;
  31.                     }
  32.                 }
  33.                 n1 = m;
  34.             }
  35.             else
  36.                 o++;
  37.         }
  38.         int e = n - o;
  39.         //sortiranje na neparni
  40.         for(int i = 0; i < o-1; i++){
  41.             for(int j = i+1; j < o; j++){
  42.                 if(a[j] < a[i]){
  43.                     int temp = a[i];
  44.                     a[i] = a[j];
  45.                     a[j] = temp;
  46.                 }
  47.             }
  48.         }
  49.         //sortiranje na parni
  50.         for(int i = o; i < n-1; i++){
  51.             for(int j = i+1; j < n; j++)
  52.                 if(a[i] < a[j]){
  53.                     int temp = a[i];
  54.                     a[i] = a[j];
  55.                     a[j] = temp;
  56.                 }
  57.         }
  58.     }
  59.  
  60.     public static void main(String[] args) throws IOException{
  61.         int i;
  62.         BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in));
  63.         String s = stdin.readLine();
  64.         int n = Integer.parseInt(s);
  65.  
  66.         s = stdin.readLine();
  67.         String [] pom = s.split(" ");
  68.         int [] a = new int[n];
  69.         for(i=0;i<n;i++)
  70.             a[i]=Integer.parseInt(pom[i]);
  71.         oddEvenSort(a,n);
  72.         for(i=0;i<n-1;i++)
  73.             System.out.print(a[i]+" ");
  74.         System.out.print(a[i]);
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement