Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class OddEvenSort {
- static void oddEvenSort(int a[], int n)
- {
- int odd [] = new int[n];
- int even[] = new int[n];
- int numberOdd = 0;
- int numberEven = 0;
- for (int k=0; k < a.length; ++k) {
- if (a[k] % 2 == 0) {
- even[numberEven++] = a[k];
- }
- else {
- odd[numberOdd++] = a[k];
- }
- }
- bubbleSort(even,numberEven,'<');
- bubbleSort(odd,numberOdd,'>');
- for (int i=0; i < numberOdd; ++i)
- a[i] = odd[i];
- for (int i=0; i < numberEven; ++i)
- a[numberOdd + i] = even[i];
- }
- static void bubbleSort(int []a, int n, char c) {
- for (int i=0; i < n-1; ++i) {
- boolean done = true;
- for (int j=i+1; j < n; ++j) {
- if (c == '>') {
- if (a[i] > a[j]) {
- int temp = a[i];
- a[i] = a[j];
- a[j] = temp;
- done = false;
- }
- }
- else {
- if (a[i] < a[j]) {
- int temp = a[i];
- a[i] = a[j];
- a[j] = temp;
- done = false;
- }
- }
- }
- if (done)
- break;
- }
- }
- public static void main(String[] args) throws IOException{
- int i;
- BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in));
- String s = stdin.readLine();
- int n = Integer.parseInt(s);
- s = stdin.readLine();
- String [] pom = s.split(" ");
- int [] a = new int[n];
- for(i=0;i<n;i++)
- a[i]=Integer.parseInt(pom[i]);
- oddEvenSort(a,n);
- for(i=0;i<n-1;i++)
- System.out.print(a[i]+" ");
- System.out.print(a[i]);
- }
- }
Add Comment
Please, Sign In to add comment