pastebin

Paste Search Dynamic
Recent pastes
jumpSearch
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import java.util.Random;
  5.  
  6. public class Main {
  7.         public static int jumpSearch(int[] arr, int x) {
  8.         int n = arr.length;
  9.         int step = (int) math.sqrt(n);
  10.         int prev = 0;
  11.         while (arr[math.min(step, n) - 1] < x) {
  12.             prev = step;
  13.             step += (int) math.sqrt(n);
  14.             if (prev >= n)
  15.                 return -1;
  16.         }
  17.         while (arr[prev] < x) {
  18.             prev++;
  19.             if (prev == math.min(step, n))
  20.                 return -1;
  21.         }
  22.         if (arr[prev] == x)
  23.             return prev;
  24.         return -1;
  25.     }
  26.     public static void main(string[] args) {
  27.      int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
  28.      int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100};
  29.      int[] arr = new int[1000];
  30. for(int i=0;i<arr.length;i++)
  31.   arr[i]=i;
  32.  
  33.    
  34.         int x = 999;
  35.        
  36.         int result = jumpSearch(arr, x);
  37.         if (result == -1)
  38.             system.out.print(
  39.                 "Element is not present in array");
  40.         else
  41.             system.out.print("Element is present at index "
  42.                              + result);
  43.     }
  44. }
  45.  
Parsed in 0.048 seconds