Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Ex
- {
- public static void main(String[] args) {
- int a[] = new int[] { 1,-1,2,-2,3,-3,4,-4,5,-5,6,-6 };
- System.out.println("Original array: ");
- for (int f=0;f<a.length;f++)
- System.out.print(a[f] + " ");
- System.out.println();
- System.out.println();
- revOdd(a);
- sort(a);
- }
- public static void revOdd(int[] a) {
- if (a.length%2!=0) {
- for (int i=1;i<a.length/2;i=i+2) {
- int tmp = a[i];
- a[i] = a[a.length-1-i];
- a[a.length-1-i] = tmp;
- }
- } else {
- for (int i=1;i<a.length/2;i=i+2) {
- int tmp = a[i];
- a[i] = a[a.length-i];
- a[a.length-i] = tmp;
- }
- }
- }
- public static void sort(int[] a) {
- boolean done=false; int c=0,c2=0,cl=0;
- while (!done) {
- done=true;
- for (int s=0;s<a.length-1;s++)
- if (a[s]>a[s+1]) {
- c2++;
- int temp = a[s];
- a[s] = a[s+1];
- a[s+1] = temp;
- done=false;
- for (int f=0;f<a.length;f++) {
- System.out.print(a[f] + " ");
- if (f==a.length-1) {
- cl++;
- System.out.print(" " + "."+ cl);
- System.out.println();
- }
- }
- }
- }
- System.out.println("Number of iterations: " + c2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement