Data Structure - I
Data Structure - I
Data Structure - I
SIMPLE COMPLEX
SORTING
Arranging data elements in ascending or descending
order
MERGING
Combining elements of two similar data structure
Index 0 1 2 3 4 5 6
values 10 20 30 40 55 70 45
Elements
Shifted down
from that After
position Insertion
INSERTION USING 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: USING 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:
def DoubleIt(Arr):
for i in range(len(Arr)): #traversing all elements Arr[i]
*= 2
Arr=[10,20,30,40,50]
print("Current List :")
#traversing all elements
for i in Arr:
print(i)
DoubleIt(Arr)
print("After Update List :")
for i in Arr:
print(i)
FEW FUNCTIONS: TRAVERSAL (FUNCTION TO
FIND SUM OF EVERY ALTERNATE ELEMENTS)
#Function to add alternate elements #Function to add element ending with digit 7
def AddAlternate(Arr):
def AddEnding7(Arr):
sum=0
sum=0
for i in range(len(Arr)): if i
for i in range(len(Arr)):
% 2 == 0:
if Arr[i] % 10
sum+=Arr[i]
== 7:
return sum
su
#Function to count how many even
values in list m
+
def CountEven(Arr): =
count=0 A
for i in rr
range(len(Arr)): [i
if Arr[i] % 2 == 0: ]
count+=1 return sum
return count #Function to swap adjacent elements
def SwapAdjacent(Arr):
SORTING A LINEAR LIST
Sorting means arranging the list items in Ascending or Descending
order.
Many Sorting algorithm which we can use for this purpose like:
Bubble, Selection, Insertion, Shell, Quick, Heap, Merge etc.
In Class XI, we have already studied about 2 sorting methods:
Bubble and Insertion (PDF available in Class XI option of website)
Bubble Sorting
Insertion Sorting
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
3x3 3x2
1 2 3
4 5 6
2x3
Matrix addition
mat1=[[1,2,3],
[3,4,5]]
mat2=[[1,2,3],
[3,4,5]]
sum1=[[0,0,0],[0,0,0]]
for i in range(2):
for j in range(3):
sum1[i][j]=mat1[i][j]+mat2[i][j]
for i in range(2):
for j in range(3):
print(sum1[i][j], end=' ')
print()
Matrix Subtraction
mat1=[[4,5,6],[3,4,5]]
mat2=[[1,2,3],[3,4,5]]
sum1=[[0,0,0],[0,0,0]]
for i in range(2):
for j in range(3):
sum1[i][j]=mat1[i][j]-mat2[i][j]
for i in range(2):
for j in range(3):
print(sum1[i][j], end=' ')
print()
Matrix Multiplication
A = [[12, 7, 3],
[4, 5, 6],
[7, 8, 9]]
# take a 3x4 matrix
B = [[5, 8, 1],
[6, 7, 3],
[4, 5, 9]]
result = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
# iterating by row of A
for i in range(len(A)):
# iterating by coloum by B
for j in range(len(B[0])):
# iterating by rows of B
for k in range(len(B)):
result[i][j] += A[i][k] * B[k][j]
for r in result:
print(r)
1.question:
def dispdiagonal(l,m,n):
for I in range(m):
for j in range(n):
if (i==j or i+j==m-1 ) and l[i][j]%2!=0:
print(l[i][j], end=‘ ‘)
print()
l=[[1,2,3],[4,5,6],[7,8,9]]
dispdiagonal(l,3,3) 3.question:
def dispdiagonal(l,m):
l1=[[0,0,0],[0,0,0],[0,0,0]]
for I in range(m):
for j in range(n):
if ( i+j<=m-1 ):
l1[i][j]=l[j]
else:
l1[i][j]=0
l=[1,2,3]
dispdiagonal(l,3)
1. WRITE A FUNCTION IN PYTHON TO ACCEPT A NESTED LIST AS ARGUMENT AND
DISPLAY THE DIAGONAL ELEMENTS
EX. IF THE LIST CONTAINS
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
THEN THE OUTPUT SHOULD BE
LEFT DIAGONAL : 1 2 3 4
RIGHT DIAGONAL : 4 3 2 1
2. WRITE A FUNCTION IN PYTHON TO ACCEPT A NESTED LIST AS ARGUMENT AND
DISPLAY THE ODD NUMBERS THAT LIE IN THE DIAGONAL.
3. WRITE A FUNCTION IN PYTHON WHICH ACCEPTS A LIST OF NUMBERS AS
ARGUMENTS AND CONVERT IT INTO A NESTED LIST AS FOLLOWS
IF THE LIST CONTAINS 1,2,3,4
THEN THE NESTED LIST SHOULD BE CREATED AS
1,2,3,4
1,2,3,0
1,2,0,0
1,0,0,0