Data of XYZ company is stored in sorted list. Write a program for searching specific data from that list.

+1 vote
5 days ago in Python by Ramasubramanian
• 130 points
89 views

2 answers to this question.

+1 vote

https://www.youtube.com/watch?v=mqaf7vj1AdA

This link has the program. It uses a module called bisect in python

https://docs.python.org/2/library/bisect.html

Regards,

RRR

PS: if this answer helped you, please up-vote it. If you have a suggestion, please share the same

answered 4 days ago by Raveendiran
• 860 points
+1 vote

Hello @Ramasubrananiam! You can use the binary search algorithm in such situations. When you say the data is stored in a sorted list, the best approach wrt time complexity would be binary search. You can use the following code:

def binarySearch (arr, l, r, x): 
# Check base case 
    if r >= l: 
        mid = l + (r - l)/2
# If element is present at the middle itself 
    if arr[mid] == x: 
        return mid 
# If element is smaller than mid, then it can only be present in left subarray 
    elif arr[mid] > x: 
        return binarySearch(arr, l, mid-1, x) 
# Else the element can only be present in right subarray 
    else: 
        return binarySearch(arr, mid+1, r, x) 
    else: 
# Element is not present in the array 
        return -1
# Test array 
arr = [ 2, 3, 4, 10, 40 ] 
x = 10
# Function call 
result = binarySearch(arr, 0, len(arr)-1, x) 
if result != -1: 
    print "Element is present at index %d" % result 
else: 
    print "Element is not present in array"
answered 4 days ago by Priyanka

Related Questions In Python

0 votes
2 answers

Write a for loop that prints all elements of a list and their position in the list. a = [4,7,3,2,5,9]

a = [4,7,3,2,5,9] b = enumerate(a) for i in ...READ MORE

answered Jul 15 in Python by Kei Tee

edited Jul 22 by Kalgi 580 views
+1 vote
1 answer
0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
842 views
0 votes
0 answers

Storing a list of arrays into a CSV file and retrieving it back in a different program

This is the code that I am ...READ MORE

Jun 6, 2018 in Python by ariaholic
• 7,340 points
50 views
0 votes
1 answer

Is there a way to list out in-built variables and functions of Python?

The in-built variables and functions are defined ...READ MORE

answered May 14 in Python by Junaid
50 views
0 votes
1 answer

Is there a list of timezones in python?

import pytz list(pytz.common_timezones) READ MORE

answered Aug 2 in Python by Mohammad
• 3,170 points
28 views
+2 votes
2 answers

In a list of dictionaries, how can I find the minimum calue in a common dictionary field.

There are several options. Here is a ...READ MORE

answered Apr 10, 2018 in Python by charlie_brown
• 7,720 points
87 views
+1 vote
4 answers

Count the frequency of an item in a python list

To count the number of appearances: from collections ...READ MORE

answered Oct 18, 2018 in Python by tinitales
2,005 views