{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## ```Q3.``` ##\n",
"\n",
"## ```Write a Python program to perform Jump Search for a given key and report success or failure. Prompt the user to enter the key and a list of numbers.``` ##"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## -----------------------------------------------------------------------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Jump Search##\n",
"\n",
"Similar to Binary Search, **Jump Search** or **Block Search** is an algorithm only for ordered (sorted) lists. The major idea behind this algorithm is to make less comparisons by skipping a definite amount of elements in between the elements getting compared leading to less time required for the searching process. \n",
"\n",
"It can be classified as an improvement of the linear search algorithm since it depends on linear search to perform the actual comparison when searching for a value. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## -----------------------------------------------------------------------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Jump Search Steps ##\n",
"\n",
"In Jump search, it is not necessary to scan all the elements in the list to find the desired value. We just check an element and if it is less than the desired value, then some of the elements following it are skipped by jumping ahead. \n",
"\n",
"After moving a little forward again, the element is checked. If the checked element is greater than the desired value, then we have a boundary and we are sure that the desired value lies between the previously checked element and the currently checked element. However, if the checked element is less than the value being searched for, then we again make a small jump and repeat the process. \n",
"\n",
"Given a sorted list, instead of searching through the list elements incrementally, we search by ```jump```. The optimal size for ```jump``` is $\\sqrt{N}$ where the ```N``` is the length of the list. \n",
"\n",
"So in our input list ```list_of_numbers,``` if we have a jump size of ```jump``` then our algorithm will consider the elements between ```list_of_numbers[0]``` and ```list_of_numbers[0 + number_of_jumps],``` if the key element is not found then we will consider the other elements between ```list_of_numbers[0 + number_of_jumps]``` and ```list_of_numbers[0 + 2*number_of_jumps],``` again if the key element is not found then we consider the elements between ```list_of_numbers[0 + 2*number_of_jumps],``` and ```list_of_numbers[0 + 3*number_of_jumps]``` and so on. \n",
"\n",
"With each jump, we store the previous value we looked at and its index. When we find a set of values where ```list_of_numbers[i] < key_element < list_of_numbers[i + number_of_jumps],``` we perform a linear search with ```list_of_numbers[i]``` as the left-most element and ```list_of_numbers[i + number_of_jumps]``` as the right-most element in our search block. \n",
"\n",
"For example, consider a list of ```[1, 2, 3, 4, 5, 6, 7, 8, 9].```The length of the list is ```9``` and the size of ```jump``` is ```3```. If we have to find the key element ```8``` then the following steps are performed using the Jump search technique. \n",
"\n",
"```Step 1:``` First three elements are checked. Since 3 is smaller than 8, we will have to make a jump ahead. \n",
"\n",
"\n",
"\n",
"```Step 2:``` Next three elements are checked. Since 6 is smaller than 8, we will have to make a jump ahead. \n",
"\n",
"\n",
"\n",
"```Step 3:``` Next three elements are checked. Since 9 is greater than 8, the desired value lies within the current boundary. \n",
"\n",
"\n",
"\n",
"```Step 4:``` A linear search is now done to find the value in the array. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## -----------------------------------------------------------------------------------------------------------------------------"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter a list of numbers in ascending order with space between each other: 1 2 3 4 5 6 7 8 9\n",
"Enter the Key number to search: 8\n",
"Key number 8 is found in Position 8\n"
]
}
],
"source": [
"import math\n",
"\n",
"\n",
"def jump_search(list_of_numbers, key):\n",
" list_length = len(list_of_numbers)\n",
"\n",
" # Calculate jump size\n",
" jump = int(math.sqrt(list_length))\n",
"\n",
" left, right = 0, 0 # These are the index values\n",
"\n",
" # Find the block where key element is present (if it is present)\n",
" while left < list_length and list_of_numbers[left] <= key:\n",
" right = min(list_length - 1, left + jump)\n",
" if list_of_numbers[left] <= key <= list_of_numbers[right]:\n",
" break\n",
" left += jump\n",
"\n",
" if left >= list_length or list_of_numbers[left] > key:\n",
" return -1\n",
"\n",
" right = min(list_length - 1, right)\n",
" i = left\n",
" # Do linear search for key element in the block\n",
" while i <= right and list_of_numbers[i] <= key:\n",
" if list_of_numbers[i] == key:\n",
" return i # Return the position of the key element\n",
" i += 1\n",
"\n",
" return -1\n",
"\n",
"\n",
"def user_input():\n",
" list_of_numbers = list()\n",
" total_elements = input(\"Enter a list of numbers in ascending order with space between each other: \").split()\n",
" for each_element in range(len(total_elements)):\n",
" list_of_numbers.append(int(total_elements[each_element]))\n",
"\n",
" key = int(input(\"Enter the Key number to search: \"))\n",
"\n",
" index = jump_search(list_of_numbers, key)\n",
" if index == -1:\n",
" print(\"Entered key is not present\")\n",
" else:\n",
" print(f\"Key number {key} is found in Position {index + 1}\")\n",
"\n",
"\n",
"if __name__ == \"__main__\":\n",
" user_input()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## -----------------------------------------------------------------------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step by Step tracing of the above code\n",
"```\n",
"Elements -> 1 2 3 4 5 6 7 8 9\n",
"Index -> 0 1 2 3 4 5 6 7 8\n",
"\n",
"left = 0\n",
"right = 0\n",
"list_length = 9\n",
"jump = 3\n",
"\n",
"while 0 < 9 and 1 <= 8:\n",
" right = min(8, 3)\n",
" right is 3\n",
" if 1 <= 8 <= 4 condition is False\n",
" left = 3\n",
"\n",
"while 3 < 9 and 4 <= 8:\n",
" right = min(8, 6)\n",
" right is 6 \n",
" if 4 <= 8 <= 7: condition is False\n",
" left = 6\n",
"\n",
"while 6 < 9 and 7 <= 8:\n",
" right = min(8, 9)\n",
" right is 8\n",
" if 7 <= 8 <= 9: condition is True\n",
" break\n",
"\n",
"right = min(8, 8)\n",
"right is 8\n",
"i = left\n",
"i is 6\n",
"\n",
"while 6 <= 8 and 7 <= 8:\n",
" if 7 == 8: condition is False\n",
" i += 1\n",
" i is 7\n",
"\n",
"while 7 <= 8 and 8 <= 8:\n",
" if 8 == 8: condition is True\n",
" return 7 here 7 is the index position and not the actual element\n",
"\n",
"print -> i + 1 position\n",
"```"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
},
"pycharm": {
"stem_cell": {
"cell_type": "raw",
"metadata": {
"collapsed": false
},
"source": []
}
}
},
"nbformat": 4,
"nbformat_minor": 1
}