0% found this document useful (0 votes)
4 views27 pages

Linear Lists (Python) Part-1

The document provides an overview of linear lists (arrays), including their definition, operations such as searching, insertion, deletion, traversal, and sorting. It details two searching techniques: Linear Search and Binary Search, explaining their methods and providing example code for both. The document is prepared by Naveen Kumar Varshney and includes references to his YouTube channel.

Uploaded by

vaidikthakur1824
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views27 pages

Linear Lists (Python) Part-1

The document provides an overview of linear lists (arrays), including their definition, operations such as searching, insertion, deletion, traversal, and sorting. It details two searching techniques: Linear Search and Binary Search, explaining their methods and providing example code for both. The document is prepared by Naveen Kumar Varshney and includes references to his YouTube channel.

Uploaded by

vaidikthakur1824
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

LINEAR LISTS

(Arrays)
Part-1

Prepared By-
Naveen Kumar Varshney
PGT (Comp. Sc.)
JNV Devrala, Bhiwani

Subscribe my Youtube Channel – Naveen Varshney Official


Outlines

• Linear Lists (Arrays)

• Operations on Linear Lists(Arrays)

• Searching in a Linear List

Subscribe my Youtube Channel – Naveen Varshney Official


LINEAR LISTS (ARRAYS)
• Linear List (Array) is a collection of homogeneous data
types.
AR 21 23 24 9 45 11 56 76 29 86

0 1 2 3 4 5 6 7 8 9

LB UB
• Length(Size) of Linear List(Array)= UB-LB+1
=9-0+1=10
Subscribe my Youtube Channel – Naveen Varshney Official
OPERATIONS ON LINEAR LISTS

• Searching : Find out the location of the data item if it


exists in the Linear List.
• Insertion : Add a new data item in the Linear List.
• Deletion : Delete an existing data item from the Linear
List.
• Traversal : Access each data item exactly once so that it
can be processed.
• Sorting : Arranging the data items in some order.​

Subscribe my Youtube Channel – Naveen Varshney Official


SEARCHING IN A LINEAR LIST
• Two searching techniques :

1) Linear Search
2) Binary Search

Subscribe my Youtube Channel – Naveen Varshney Official


SEARCHING IN A LINEAR LIST
Linear Search :
• This is the simplest searching technique.
• It sequentially checks each element of the list for
the target searching value until a match is found
or until all the elements has been searched.
• This searching technique can be performed on
both type of list, either the list is sorted or
unsorted.

Subscribe my Youtube Channel – Naveen Varshney Official


SEARCHING IN A LINEAR LIST
Linear Search :
23 39 27 44 23 12 5 67

23 23≠39 39 27 44 23 12 5 67

23 39 27 44 23 12 5 67

23 39 27 44 23 12 5 67

23 39 27 44 23 12 5 67

Subscribe my Youtube Channel – Naveen Varshney Official


SEARCHING IN A LINEAR LIST
Linear Search :
23 39 27 44 23 12 5 67

23 23≠39 39 27 44 23 12 5 67

23 23≠27 39 27 44 23 12 5 67

23 39 27 44 23 12 5 67

23 39 27 44 23 12 5 67

Subscribe my Youtube Channel – Naveen Varshney Official


SEARCHING IN A LINEAR LIST
Linear Search :
23 39 27 44 23 12 5 67

23 23≠39 39 27 44 23 12 5 67

23 23≠27 39 27 44 23 12 5 67

23 23≠44 39 27 44 23 12 5 67

23 39 27 44 23 12 5 67

Subscribe my Youtube Channel – Naveen Varshney Official


SEARCHING IN A LINEAR LIST
Linear Search :
23 39 27 44 23 12 5 67

23 23≠39 39 27 44 23 12 5 67

23 23≠27 39 27 44 23 12 5 67

23 23=44 39 27 44 23 12 5 67

23 23=23 39 27 44 23 12 5 67

Subscribe my Youtube Channel – Naveen Varshney Official


SEARCHING IN A LINEAR LIST
# FUNCTION OF LINEAR SEARCH
def lin_search(alist,item):
#Return index of item in alist otherwise return -1.
l=len(alist)
for i in range(l):
if alist[i]==item:
return i LINEAR
return -1
SEARCH
PROGRAM

Subscribe my Youtube Channel – Naveen Varshney Official


SEARCHING IN A LINEAR LIST
# MAIN PROGRAM
alist=eval(input("Enter List:"))
item=int(input("Enter element to be searched :"))
result=lin_search(alist,item)
LINEAR
if result==-1:
print("Element not found in the Linear List.")
SEARCH
else: PROGRAM
print("Element",item,"found at index",result,"and Position",result+1)

Subscribe my Youtube Channel – Naveen Varshney Official


SEARCHING IN A LINEAR LIST
Binary Search :
• This is the fastest searching technique.
• It is based on Divide and Conquer method.
• This searching technique can be performed on only
sorted list.
• Binary Search begins by comparing the middle element
of the list with the searching value.
• If the searching value matches the middle element, its
position in the list is returned.
Subscribe my Youtube Channel – Naveen Varshney Official
SEARCHING IN A LINEAR LIST
Binary Search :
• And if the searching value is less than the middle
element, the search continues in the lower half of the
list.
• If the searching value is greater than the middle
element, the search continues in the upper half of the
list.
• The same process is repeated for new segment until the
searching value is not found.
Subscribe my Youtube Channel – Naveen Varshney Official
SEARCHING IN A LINEAR LIST
Binary Search : AR

19 3 7 11 14 19 22 43 67
item

• Item to be searched : 19

Subscribe my Youtube Channel – Naveen Varshney Official


SEARCHING IN A LINEAR LIST
Binary Search : AR

19 3 7 11 14 19 22 43 67
item 0 1 2 3 4 5 6 7

beg mid last


• Calculate mid
mid=int(beg+last)/2
=3

Subscribe my Youtube Channel – Naveen Varshney Official


SEARCHING IN A LINEAR LIST
Binary Search : AR

19 3 7 11 14 19 22 43 67
item 0 1 2 3 4 5 6 7

beg mid last

• AR[mid] is compared with 19

Subscribe my Youtube Channel – Naveen Varshney Official


SEARCHING IN A LINEAR LIST
Binary Search : AR

19 3 7 11 14 19 22 43 67
item 0 1 2 3 4 5 6 7

beg mid last

• Now, AR[mid] is less than 19 then set the beg at (mid+1) position
i.e. 4

Subscribe my Youtube Channel – Naveen Varshney Official


SEARCHING IN A LINEAR LIST
Binary Search : AR

19 3 7 11 14 19 22 43 67
item 0 1 2 3 4 5 6 7

mid beg last


• Again mid is calculated:
mid= int(beg+last)/2 = 5

Subscribe my Youtube Channel – Naveen Varshney Official


SEARCHING IN A LINEAR LIST
Binary Search : AR

19 3 7 11 14 19 22 43 67
item 0 1 2 3 4 5 6 7

beg mid last

• Now again AR[mid] is compared with 19

Subscribe my Youtube Channel – Naveen Varshney Official


SEARCHING IN A LINEAR LIST
Binary Search : AR

19 3 7 11 14 19 22 43 67
item 0 1 2 3 4 5 6 7

beg mid last

• AR[mid] is greater than 19 then set the last at (mid-1) position i.e. 4

Subscribe my Youtube Channel – Naveen Varshney Official


SEARCHING IN A LINEAR LIST
Binary Search : AR

19 3 7 11 14 19 22 43 67
item 0 1 2 3 4 5 6 7

beg mid
last
• Again mid is calculated
mid = int(beg+last)/2 = 4

Subscribe my Youtube Channel – Naveen Varshney Official


SEARCHING IN A LINEAR LIST
Binary Search : AR

19 3 7 11 14 19 22 43 67
item 0 1 2 3 4 5 6 7

beg
last
• Again AR[mid] is compared with 19
mid

Subscribe my Youtube Channel – Naveen Varshney Official


SEARCHING IN A LINEAR LIST
Binary Search : AR

19 3 7 11 14 19 22 43 67
item 0 1 2 3 4 5 6 7

beg
last
• Now, AR[mid] is equal to 19.
mid

Subscribe my Youtube Channel – Naveen Varshney Official


SEARCHING IN A LINEAR LIST
Binary Search : AR

19 3 7 11 14 19 22 43 67
item 0 1 2 3 4 5 6 7

beg
last
• Search is successful. Item is found
mid
at index 4 and position 5.

Subscribe my Youtube Channel – Naveen Varshney Official


SEARCHING IN A LINEAR LIST
# FUNCTION OF BINARY SEARCH
def bin_search(alist,item):
#Return index of item in alist otherwise return -1.
beg=0
last=len(alist)-1
while beg<=last:
mid=int((beg+last)/2)
if (alist[mid]==item):
return mid BINARY
elif (item>alist[mid]):
beg=mid+1
SEARCH
else: PROGRAM
last=mid-1
return -1
Subscribe my Youtube Channel – Naveen Varshney Official
SEARCHING IN A LINEAR LIST
# MAIN PROGRAM
alist=eval(input("Enter List in Ascending Order:"))
item=int(input("Enter element to be searched :"))
result=bin_search(alist,item)
if result==-1:
print("Element not found in the Linear List.")
else:
print("Element",item,"found at index",result,"and Position",result+1)

BINARY
SEARCH
PROGRAM
Subscribe my Youtube Channel – Naveen Varshney Official

You might also like