Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.92 KB | None | 0 0
  1. package jawa;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class jabba {
  6.  
  7.     public static void main(String[] args) {
  8.         // TODO Auto-generated method stub
  9.         int [] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  10.         System.out.println(binarySearch(arr, 9));
  11.         System.out.println(recursiveBinary(arr, 9));
  12.         int [] unsort = {1, 22, 10, 4, 5, 3, 5, 8, 2};
  13.         int [] unsort2 = {1, 22, 10, 3, 5, 8, 5, 12, 2};
  14.         bubbleSort(unsort);
  15.         insertionSort(unsort2);
  16.         System.out.println(Arrays.toString(unsort));
  17.         System.out.println(Arrays.toString(unsort2));
  18.     }
  19.    
  20.     public static int linearSearch(int[] arr, int target){
  21.         for (int i = 0; i < arr.length; i++){
  22.             if (arr[i] == target){
  23.                 return i;
  24.             }
  25.            
  26.         }
  27.         return -1;
  28.     }
  29.    
  30.     public static int binarySearch(int[] arr, int target){
  31.         int low = 0;
  32.         int high = arr.length-1;
  33.         boolean cont = true;
  34.         int guess = (low+high)/2;
  35.         while (cont){
  36.            
  37.             if (arr[guess] == target){
  38.                 return guess;
  39.             }
  40.             else if (arr[guess] > target) {
  41.                 high = guess - 1;
  42.                 guess = (low+high)/2;
  43.             }
  44.                
  45.             else if (arr[guess] < target) {
  46.                 low = guess + 1;
  47.                 guess = (low+high)/2;
  48.             }
  49.             if (high < low){
  50.                 return -1;
  51.             }
  52.         }
  53.         return -1;
  54.        
  55.     }
  56.    
  57.     public static int recursiveBinarySearch(int[] arr, int target, int low, int high){
  58.         if (low > high){
  59.             return -1;
  60.         }  
  61.         if (arr[(low+high)/2] == target){
  62.             return (low+high)/2;
  63.         }
  64.         else if (target < arr[(low+high)/2]){
  65.             return recursiveBinarySearch(arr, target, low, ((low+high)/2)-1 );
  66.         }
  67.         else if (target > arr[(low+high)/2]){
  68.             return recursiveBinarySearch(arr, target, ((low+high)/2)+1, high );
  69.         }
  70.         else
  71.             return -1;
  72.     }
  73.    
  74.     public static int recursiveBinary(int[] arr, int target){
  75.         return recursiveBinarySearch(arr, target, 0, arr.length-1);
  76.     }
  77.    
  78.     public static void bubbleSort(int[] input){
  79.        
  80.         boolean hasSwapped = false;
  81.         do {
  82.             hasSwapped = false;
  83.             for (int i = 0; i < input.length-1; i++){
  84.                
  85.                 if (input[i] > input[i+1]){
  86.                     int temp = input[i];
  87.                     input[i] = input[i+1];
  88.                     input[i+1] = temp;
  89.                     hasSwapped = true;
  90.                 }
  91.             }
  92.         }while (hasSwapped);
  93.     }
  94.    
  95.     public static void insertionSort(int[] arr){
  96.         int n = arr.length;
  97.         for (int i = 1; i < n; ++i) {
  98.             int key = arr[i];
  99.             int j = i - 1;
  100.  
  101.            
  102.             while (j >= 0 && arr[j] > key) {
  103.                 arr[j + 1] = arr[j];
  104.                 j = j - 1;
  105.             }
  106.             arr[j + 1] = key;
  107.         }
  108.     }
  109.    
  110.     public static void selectionSort(int arr[])
  111.     {
  112.         int n = arr.length;
  113.  
  114.      
  115.         for (int i = 0; i < n-1; i++)
  116.         {
  117.            
  118.             int min_idx = i;
  119.             for (int j = i+1; j < n; j++)
  120.                 if (arr[j] < arr[min_idx])
  121.                     min_idx = j;
  122.  
  123.  
  124.             int temp = arr[min_idx];
  125.             arr[min_idx] = arr[i];
  126.             arr[i] = temp;
  127.         }
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement