Data Structure - I
Data Structure - I
and G R AP H .
D I A G R A M TO R E P R E S E N T A L L DATA S T R U C T U R E
DATA
S T RU C T U R E S
S I M PLE COMPLEX
Array or Linear
Linear Non-Linear
List
Bangles
Computer based examples: Undo Operation,
it.
It will never contains a closed loop
OPERATIONS ON DATA STRUCTURE
O P E R AT I O N S DESCRIPTION
descending order
Index 0 1 2 3 4 5 6
values 10 20 30 40 55 70 45
Arr=[]
n = int(input("Enter how many items in List "))
for i in range(n):
d = int(input("Enter value :"))
Arr.append(d)
key = int(input("Enter Value to Search :"))
pos = linearSearch(Arr,key)
if pos!=None:
print("Found at position
",pos+1)
else:
print("Not Found")
B I NA RY S E A R C H I N G : D I V I D E AND RULE
Elements
Shifted down
from that After
position Insertion
INSERTION U S I N G PYTHON APPROACH
As we can observe Insertion of new item in sorted
array requires shifting of elements to make room and
this is very time consuming when list grows.
It should be avoided using better approach like bisect,
available in bisect module.
⚫ bisect.insort(list,newItem )
The insort() function of bisect module inserts an item
in the sorted array, keeping it sorted.
Bisect module offers another function bisect() which
return the appropriate index where new item can be
inserted to keep the order at it is.
⚫ bisect.bisect(list,element)
Note: bisect module works only on sequence
arranged in Ascending Order only.
PROGRAM: U S I N G BISECT MODULE
import bisect
marks=[10,20,30,40,50,60,70,80]
print("\nCurrent list is :")
print(marks)
Item = int(input("Enter item to insert:"))
pos = bisect.bisect(marks,Item)
bisect.insort(marks,Item) print("\
nItem Inserted at Index :",pos) print("\
nUpdated List is :") print(marks)
BISECT MODULE
As we know bisect module works only on
Ascending order, hence to make it for descending
order:
⚫ First reverse the list arranged in
descending in ascending order using
reverse()
⚫ Perform the insert operation
⚫ Again reverse the list.
DELETION FROM SORTED
LIST:
TRADITIONAL A P P ROAC H
To Delete item from sorted list we have to first
find the element position in List using Binary
Searching
Delete the item from list if search successful
Shift all the items upwards to keep the order of
list undisturbed
Reduce the List size by 1
Arr=[10,20,30,40,50]
print("Current List :")
for i in Arr: #traversing all elements
print(i)
DoubleIt(Arr)
print("After Update List :")
for i in Arr:
print(i)
FEW FUNCTIONS: TRAVERSAL (FUNCTION TO
F I N D S U M O F E V E RY A LT E R NAT E E L E M E N T S )
#Function to add alternate elements #Function to add element ending with digit 7
list comprehensions.
A list comprehension is a shorthand for list
EXAMPLE -
3
lst2 = [i if i%2==0 else '@' for i in range(1,101) ]
print(lst2)
EXAMPLE – 4 : FOR
CLARITY
lst2 = [(i if i%2==0 else '@‘) for i in range(1,101) ]
print(lst2)
LIST COMPREHENSION FOR NESTED FOR
LOOP
Example: Nested Loop
Arr = [ ]
for i in [10,20,30]:
for j in [5,10,15]:
Arr.appen
d(i+j)
print(Arr)
print(List2) # [10,20,[1,2,3]]
print(List2[0]) # 10
print(List2[2][0]) #1
TWO DIMENSIONAL LIST
Is a list having all elements as list of same shape for
e.g.
⚫ Mylist=[[20,40,60],[5,10,15],[9,18,27]]
20 40 60
5 10 15
9 18 27
⚫ It is a 2 – D List with 3 rows and 3 columns
⚫ First value will be at index [0][0] and last will be [2][2]
mylist = [[1,2,3],[4,5,6],[7,8,9]]
print(len(mylist)) # no. of rows
print(len(mylist[0])) # no. of columns
TWO DIMENSIONAL LIST
Ragged List: sometimes list contains another list
of different shapes for e.g.
mylist = [[20,40,60],[4,5]]
Here first row contains 3 columns and 2 row contains 2
columns
CREATING AND PRINTING OF 2-D LIST
S L I C E OF 2-D LIST
mylist=[[20,30,40],[21,22,23],[100,150,200],[19,21,40]]
print(mylist[:2])
print(mylist[2:])
print(mylist[2:][:1])