# Review Sorting algorithms Selection Sort Insertion Sort Bubble

Review Sorting algorithms Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort

List Using Array Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example 2

Introduction Suppose we wish to arrange the percentage marks obtained by 100 students in ascending order In such a case we have two options to store these marks in memory: (a) Construct 100 variables to store percentage marks obtained by 100 different students, i. e. each variable containing one student’s marks (b) Construct one variable (called array or subscripted variable) capable of storing or holding all the hundred values 3

Obviously, the second alternative is better. A simple reason for this is, it would be much easier to handle one variable than handling 100 different variables Moreover, there are certain logics that cannot be dealt with, without the use of an array Based on the above facts, we can define array as: “A collective name given to a group of ‘similar quantities’” 4

These similar quantities could be percentage marks of 100 students, or salaries of 300 employees, or ages of 50 employees What is important is that the quantities must be ‘similar’ These similar elements could be all int, or all float, or all char Each member in the group is referred to by its position in the group 5

For Example Assume the following group of numbers, which represent percentage marks obtained by five students per = { 48, 88, 34, 23, 96 } In C, the fourth number is referred as per[3] Because in C the counting of elements begins with 0 and not with 1 Thus, in this example per[3] refers to 23 and per[4] refers to 96 In general, the notation would be per[i], where, i can take a value 0, 1, 2, 3, or 4, depending on the position of the element being referred 6

Representation of Linear Array In Memory An array occupies a continuous block of memory Each element of the array also has unique memory address Starting address of the array called base address If base address of the array is known , the address of any element of the array can be found 7 L(X[K])= L 0 + C * (K-1)

Array X has 5 elements Each of 2 bytes length To find out the address of third element of X when the base address of the array is L 0 =200 L(X[3]) = 200 + 2 * (3 -1) = 200 + 4 = 204 8

Operations on linear Arrays A linear array is a list of a finite number n of homogenous data elements (data elements of the same type) Traversal: Processing each element in the list Search: Finding the location of the element with a given value or the record with a given key Insertion: Adding a new element from the list Sorting: Arranging the elements in some type of order Merging: Combining two lists into a single list 9

Traversing Operation Each element of an array is accessed exactly once for processing. Also called visiting of the array Example Sum of values of each element of an array Print value of each element of array 10

Algorithm to compute the sum of values of a linear array having ten elements Input values in array ABC SUM = 0 [Compute sum of array ABC] REPEAT FOR C = 0 to 9 SUM = SUM + ABC[C] [End of the loop] [Print the calculated sum] PRINT SUM EXIT 11

Inserting Operation In inserting operation, new items are added into an array At the end of an array without disturbing other elements of the array if there is an empty element. At some specified location within the array 12

Write an algorithm to add 6 values into elements at the end of the array ‘temp’ that has only four items stored in its first four elements 1 - REPEAT Step-2 TO 3 FOR I = 4 to 9 2 -INPUT value in N 3 -Temp [I] = N [End of Step-2 Loop] 4 -EXIST 13

Inserting value at specified location To insert new value, the value of all the existing elements are moved one step forward or backward to make space for the new value Write an algorithm to insert a value M at location pos in array ABC having N element. 14

1 - Input value in M 2 - Input position in POS [Move elements one step forward] 3 - REPEAT step – 4 TO 5 WHILE N>=POS 4 - ABC[N+1]=ABC[N] // from where you need to insert value 5 - N=N-1 [End of step-3 Loop] [Insert value M at Position POS] 6 - ABC[POS]=M 7 - Exit 15

Deleting operations One Like insertion , deletion can be performed at the end or at any position in the array. To remove the last element from the array, the last element is accessed and deleted by placing null value or by decreasing the size of array by one element if it is the last element of the array. 16

Write an algorithm to delete the value at location K of an array ‘country’ having N elements 1 - Input location K 2 - IF K > N then PRINT “Invalid location” return END IF [Move each element from the specified location one step towards the beginning. The item at the specified location is automatically deleted] 3 - REPEAT FOR C = K TO N-1 Country[C]= Country[C+1] [End of the loop] 4 - EXIT 17

Summary Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example 18

- Slides: 18