From 2c1b0d9f778a3c3d1316e61c0a23e8d6e373db28 Mon Sep 17 00:00:00 2001 From: Pratham Sharma Date: Mon, 21 Oct 2019 23:09:15 +0530 Subject: [PATCH 1/4] Create jump_seach.rb --- Searches/jump_search.rb | 48 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Searches/jump_search.rb diff --git a/Searches/jump_search.rb b/Searches/jump_search.rb new file mode 100644 index 0000000..9ad31e7 --- /dev/null +++ b/Searches/jump_search.rb @@ -0,0 +1,48 @@ +# Works only on sorted arrays. +# Finding element by creating step in array and jump ahead by fixed steps and finding element using linear search inside that steped array. +# Time Complexity : O(√n) + +def jumpsearch(arr, x) + n = arr.length; + + # Finding block size to be jumped + step = Math.sqrt(n); + prev = 0; + + # Finding the block where element is + # present (if it is present) + while (arr[[step, n].min - 1] < x) do + prev = step; + step += Math.sqrt(n); + if (prev >= n) + return -1; + end + end + + # Doing a linear search for x in block + # beginning with prev. + while (arr[prev] < x) do + prev = prev + 1; + # If we reached next block or end of + # array, element is not present. + if (prev == [step, n].min) + return -1; + end + end + + # If element is found + if (arr[prev] == x) + return prev; + end + + return -1; +end + +puts "Enter a sorted space-separated list:" +arr = gets.chomp.split(' ').map(&:to_i) +puts "Enter the value to be searched:" +value = gets.chomp.to_i + +index = jumpsearch(arr, value) + +puts index == -1 ? "Element not found" : "Number #{value} is at #{index}" From c3b26bed0ae206c4e78d74523165abb7bd01c017 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 6 Feb 2021 22:48:31 -0800 Subject: [PATCH 2/4] Update Searches/jump_search.rb --- Searches/jump_search.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Searches/jump_search.rb b/Searches/jump_search.rb index 9ad31e7..e4c4353 100644 --- a/Searches/jump_search.rb +++ b/Searches/jump_search.rb @@ -43,6 +43,6 @@ arr = gets.chomp.split(' ').map(&:to_i) puts "Enter the value to be searched:" value = gets.chomp.to_i -index = jumpsearch(arr, value) +index = jump_search(arr, value) puts index == -1 ? "Element not found" : "Number #{value} is at #{index}" From 8b32ea1c2486da1fa9f573f345ec1e0f796f2584 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 6 Feb 2021 22:48:35 -0800 Subject: [PATCH 3/4] Update Searches/jump_search.rb --- Searches/jump_search.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Searches/jump_search.rb b/Searches/jump_search.rb index e4c4353..6df171c 100644 --- a/Searches/jump_search.rb +++ b/Searches/jump_search.rb @@ -2,7 +2,7 @@ # Finding element by creating step in array and jump ahead by fixed steps and finding element using linear search inside that steped array. # Time Complexity : O(√n) -def jumpsearch(arr, x) +def jump_search(arr, x) n = arr.length; # Finding block size to be jumped From 84b4964d8f14f65d29424d1d7432ea5540fd74d1 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 6 Feb 2021 22:48:51 -0800 Subject: [PATCH 4/4] kill space --- Searches/jump_search.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Searches/jump_search.rb b/Searches/jump_search.rb index 6df171c..899b9af 100644 --- a/Searches/jump_search.rb +++ b/Searches/jump_search.rb @@ -1,6 +1,6 @@ # Works only on sorted arrays. # Finding element by creating step in array and jump ahead by fixed steps and finding element using linear search inside that steped array. -# Time Complexity : O(√n) +# Time Complexity: O(√n) def jump_search(arr, x) n = arr.length;