Home Page class 12 @ Python

CBSE Class 12 : Data Structures

Introduction to Data Structures

CBSE Class 12 : Searching

Creation And Traversal
What is Searching?
Linear/Sequential Search
Binary Search
Insertion Of Element
Deletion Of Element

CBSE Class 12 : Sorting

What is sorting?
Bubble Sort
Selection Sort
Insertion Sort

CBSE Class 12 : Stacks

Stacks
Applications of Stacks
Implementation of Stacks

CBSE Class 12 : Queues

Queues
Applications Of Queues
Implementation of Queues

Class 12: Data Structures Searching 3

Sequential Search / Linear Search Program

Example:1

Python program to assume a list of elements. Further, search for an element from the list. (Without Using Functions)

n=[10,20,50,32,156,57,243,122,615,129]
print("List is ")
print(n)
#item=156
item=int(input("Enter the element to search "))
pos = 0
found=False
while pos < len(n) and found==False:
    if(n[pos] == item):
        found = True
    else:
        pos = pos + 1
 if(found==True):
    print("element found at position ",(pos+1))
else:
    print("element is not found")

Output:

Case:1

List is
[10, 20, 50, 32, 156, 57, 243, 122, 615, 129]
Enter the element to search 32
element found at position 4
>>>

Case:2

List is
[10, 20, 50, 32, 156, 57, 243, 122, 615, 129]
Enter the element to search 125
element is not found
>>>

Example:2

Write a python program to assume a list of elements Further, search an element from the list. (Using Functions)

n=[10,20,50,32,156,57,243,122,615,129]
print("List is ")
print(n)
#item=156
item=int(input("Enter the element to search "))
pos = 0
found=False
while pos < len(n) and found==False:
    if(n[pos] == item):
        found = True
    else:
        pos = pos + 1
 if(found==True):
    print("element found at position ",(pos+1))
else:
    print("element is not found")

Output:

Case:1

List is
[10, 20, 50, 32, 156, 57, 243, 122, 615, 129]
Enter the element to search 32
element found at position 4
>>>

Case:2

List is
[10, 20, 50, 32, 156, 57, 243, 122, 615, 129]
Enter the element to search 125
element is not found
>>>