An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array).

The following are different operations that can be performed using arrays

1. Matrix operations like addition, subtraction, and multiplication ext

2. Searching and sorting operations

3. Finding min and max element in an given array.

Here we implement all the above operations

1. Matrix operations like addition, subtraction, and multiplication ext

X = [[12,7,3], [4 ,5,6], [7 ,8,9]] Y = [[5,8,1], [6,7,3], [4,5,9]] result = [[0,0,0], [0,0,0], [0,0,0]] # iterate through rows for i in range(len(X)): # iterate through columns for j in range(len(X[0])): result[i][j] = X[i][j] + Y[i][j] print("---addition----") for r in result: print(r) # iterate through rows for i in range(len(X)): # iterate through columns for j in range(len(X[0])): result[i][j] = X[i][j] - Y[i][j] print("---Subtraction----") for r in result: print(r) # iterate through rows of X for i in range(len(X)): # iterate through columns of Y for j in range(len(Y[0])): # iterate through rows of Y for k in range(len(Y)): result[i][j] += X[i][k] * Y[k][j] print("---Multiplication----") for r in result: print(r) # iterate through rows for i in range(len(X)): # iterate through columns for j in range(len(X[0])): result[j][i] = X[i][j] print("---Transpose of matrix-X----") for r in result: print(r)

The following is the output

---addition---- [17, 15, 4] [10, 12, 9] [11, 13, 18] ---Subtraction---- [7, -1, 2] [-2, -2, 3] [3, 3, 0] ---Multiplication---- [121, 159, 62] [72, 95, 76] [122, 160, 112] ---Transpose of matrix-X---- [12, 4, 7] [7, 5, 8] [3, 6, 9]

2. Searching and sorting operations

Searching is the process of finding required element in the given array. If element is found then the search is said to be successful otherwise search is unsuccessful. There are two main types of search techniques

1. Linear search : Used to search in unsorted arrays

2. Binary search: Used to search in sorted arrays.

Sorting is the process of arranging elements in the order. Sorting may be internal sort or external sort.

We will discuss these search and sorting techniues separately in Data structures using Python.

## 0 comments:

## Post a Comment

Note: only a member of this blog may post a comment.