Kame3

neparnoParnoSortiranje

Nov 17th, 2019 (edited)
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.60 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.         int odd [] = new int[n];
  10.         int even[] = new int[n];
  11.         int numberOdd = 0;
  12.         int numberEven = 0;
  13.        
  14.         for (int k=0; k < a.length; ++k) {
  15.             if (a[k] % 2 == 0) {
  16.                 even[numberEven++] = a[k];
  17.             }
  18.             else {
  19.                 odd[numberOdd++] = a[k];
  20.             }
  21.         }
  22.        
  23.         bubbleSort(even,numberEven,'<');
  24.         bubbleSort(odd,numberOdd,'>');
  25.        
  26.         for (int i=0; i < numberOdd; ++i)
  27.             a[i] = odd[i];
  28.         for (int i=0; i < numberEven; ++i)
  29.             a[numberOdd + i] = even[i];
  30.        
  31.     }
  32.    
  33.     static void bubbleSort(int []a, int n, char c) {
  34.        
  35.         for (int i=0; i < n-1; ++i) {
  36.             boolean done = true;
  37.            
  38.             for (int j=i+1; j < n; ++j) {
  39.                
  40.                 if (c == '>') {
  41.                     if (a[i] > a[j]) {
  42.                         int temp = a[i];
  43.                         a[i] = a[j];
  44.                         a[j] = temp;
  45.                         done = false;
  46.                     }
  47.                 }
  48.                
  49.                 else {
  50.                     if (a[i] < a[j]) {
  51.                         int temp = a[i];
  52.                         a[i] = a[j];
  53.                         a[j] = temp;
  54.                         done = false;
  55.                     }
  56.                 }
  57.                
  58.             }
  59.            
  60.             if (done)
  61.                 break;
  62.            
  63.         }
  64.        
  65.     }
  66.  
  67.     public static void main(String[] args) throws IOException{
  68.         int i;
  69.         BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in));
  70.         String s = stdin.readLine();
  71.         int n = Integer.parseInt(s);
  72.        
  73.         s = stdin.readLine();
  74.         String [] pom = s.split(" ");
  75.         int [] a = new int[n];
  76.         for(i=0;i<n;i++)
  77.             a[i]=Integer.parseInt(pom[i]);
  78.         oddEvenSort(a,n);
  79.         for(i=0;i<n-1;i++)
  80.             System.out.print(a[i]+" ");
  81.         System.out.print(a[i]);
  82.     }
  83. }
Add Comment
Please, Sign In to add comment