Advertisement
oshrinuri

Untitled

Jun 10th, 2015
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 KB | None | 0 0
  1. public class Ex
  2. {
  3.    
  4.     public static void main(String[] args) {
  5.                     int a[] = new int[] { 1,-1,2,-2,3,-3,4,-4,5,-5,6,-6 };
  6.                     System.out.println("Original array: ");
  7.                       for (int f=0;f<a.length;f++)
  8.                                 System.out.print(a[f] + " ");
  9.                                 System.out.println();
  10.                                 System.out.println();
  11.                     revOdd(a);
  12.                     sort(a);
  13.     }
  14.    
  15.     public static void revOdd(int[] a) {
  16.         if (a.length%2!=0) {
  17.         for (int i=1;i<a.length/2;i=i+2) {
  18.             int tmp = a[i];
  19.             a[i] = a[a.length-1-i];
  20.             a[a.length-1-i] = tmp;
  21.         }
  22.     } else {
  23.               for (int i=1;i<a.length/2;i=i+2) {
  24.             int tmp = a[i];
  25.             a[i] = a[a.length-i];
  26.             a[a.length-i] = tmp;
  27.         }
  28.     }
  29.     }
  30.    
  31.     public static void sort(int[] a) {
  32.         boolean done=false; int c=0,c2=0,cl=0;
  33.             while (!done) {
  34.             done=true;
  35.                 for (int s=0;s<a.length-1;s++)
  36.             if (a[s]>a[s+1]) {
  37.                 c2++;
  38.                 int temp = a[s];
  39.                 a[s] = a[s+1];
  40.                 a[s+1] = temp;
  41.                 done=false;
  42.             for (int f=0;f<a.length;f++) {
  43.                 System.out.print(a[f] + " ");
  44.                     if (f==a.length-1) {
  45.                         cl++;
  46.                         System.out.print(" " + "."+ cl);
  47.                         System.out.println();
  48.                     }
  49.             }
  50.             }
  51.         }
  52.         System.out.println("Number of iterations: " + c2);
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement