Data structure is one most important study in computer science, it facilitates many complex operations in software development. As computer scientist, our job to perform operations on data such as getting input, processing that and outputting. These steps are happening everything we are surfing on the internet or making any transactions at an ATM machine. I would like to take this opportunity and demonstrate some of the basics in data structures. Today's example i would like to show how to apply binary search for very primitive search mechanism. Maybe we can also demonstrate duplicate values removal in a list. I m going to use python language so it might more convenient.
Binary search is one of the most common algorithm that many developers are using it. I also would like to talk about Big O notation and you may find more on Wikipedia. In computer science efficiency is the most important drive point. Most of the engineer and computer scientists are working on how to make a software or a system more efficient and cost-effective. Therefore data structure becomes high priority. Often engineers are trying to figure out the worse case scenario and that correlates with Time VS Data Size which means efficiency. So lets start with a small example :
You can also find this example on my github ...
How binary search work, here is the pseudo code :
1- get the length of the array
2- find the middle
3- compare the given input if it is bigger , smaller or equal.
4- Don't forget Binary search is recursive algorithm so we need to call back our function inside of our function. and we need to keep going until it matches with our input.
Data structure practice - 1 | |
Binary Search | |
Duplicate removal in python lists | |
data1 = [0, 1, 2,2, 8, 13,13, 17, 19, 32,32, 42] | |
data2 = [0, 1, 24, 38, 113, 127, 19, 32, 42] | |
finaldata = data1 + data2 | |
#Removes the duplicate values | |
def remove_duplicates(test): | |
mylist=[] | |
seen = set() | |
for x in test: | |
if(x not in seen): | |
mylist.append(x) | |
seen.add(x) | |
return mylist | |
# gets user input | |
def get_user_input(search): | |
return search | |
# if our input is empty or we only have a single subscript and that subscript is not the item that we are search for returns false | |
def search_func(arr, item): | |
if (len(arr) == 0 or len(arr) == 1 and arr[0] != item): | |
return False | |
mid = arr[len(arr)/2] | |
if (item == mid): | |
return True | |
if(item < mid): | |
return search_func(arr[:len(arr)/2],item) | |
if(item > mid): | |
return search_func(arr[len(arr)/2+1:],item) | |
#main loop | |
def main(): | |
filtered = remove_duplicates(finaldata) | |
print('please enter a number : \n') | |
search = input() | |
target = get_user_input(search) | |
traverse = search_func(filtered, target) | |
print(traverse ,"Found ! \t", filtered) | |
if __name__ == '__main__': | |
main() |
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.