Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.01 KB | None | 0 0
  1.     public static int unsortedAt(int[] a) {
  2.         for (int i = 1; i < a.length; i++)
  3.             if (a[i] < a[i - 1])
  4.                 return i - 1;
  5.         return -1;
  6.     }
  7.  
  8.     public static void swap(int[] a, int i) {
  9.         int temp = a[i + 1];
  10.         a[i + 1] = a[i];
  11.         a[i] = temp;
  12.     }
  13.  
  14.     public static void sort(int[] a) {
  15.         int i = unsortedAt(a);
  16.         while (i != -1) {
  17.             swap(a, i);
  18.             i = unsortedAt(a);
  19.         }
  20.     }
  21.  
  22.     public static int spiderman(int n) {
  23.         if (n == 0 || n == 1)
  24.             return 1;
  25.         return spiderman(n - 1) + spiderman(n - 2);
  26.     }
  27.  
  28.     public static void printBinary(int n) {
  29.         String s = new String();
  30.         s = getBinary(n, s);
  31.         for (int i = s.length() - 1; i >= 0; i--) {
  32.             System.out.print(s.charAt(i));
  33.         }
  34.     }
  35.  
  36.     public static String getBinary(int n, String s) {
  37.         if (n != 0) {
  38.             s += n % 2;
  39.             return getBinary(n / 2, s);
  40.         } else
  41.             return s;
  42.     }
  43.  
  44.     public static boolean isPalindrome(String s) {
  45.         for (int i = 0; i < s.length() / 2; i++)
  46.             if (s.charAt(i) != s.charAt(s.length() - 1 - i))
  47.                 return false;
  48.         return true;
  49.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement