DATA STRUCTURES MODULE 1 OVERVIEW, SEARCHING,SORTING DR. CHITRA RAVI DIRECTOR, SOSS,CMRU SYLLABUS Module 1: Overview, Searching and Sorting Duration : 9 Hours Introduction: Data Structures – Definition, Operations, Classification, Abstract Data Type ADT Algorithms: Algorithmic Notation, Time & Space Complexity, Time-Space tradeoff, Applications of Data structures Arrays: Introduction, Linear Arrays: Memory Representation, Traversal, Insertion and Deletion operations, Multidimensional arrays: Definition, Initialization, Sparse Matrix representation Searching: Linear Search and Binary Search algorithms Sorting: Bubble sort, Merge sort, Quick sort Overview of Data structures, Searching, Sorting 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. Introduction: Definition , Classification Abstract Data Type ADT, Operations – Common, Basic, Special operations Algorithms: Algorithmic Notation, Time & Space Complexity, Time-Space tradeoff, Applications of Data structures Arrays: Introduction, Linear Arrays: Memory Representation, Traversal, Insertion and Deletion operations Multidimensional arrays: Definition, Initialization, Example Sparse Matrix: Memory representation, Sparse matrix Addition Searching: Linear Search and Binary Search algorithms Sorting: Bubble sort Sorting: Merge sort Sorting: Quick sort OVERVIEW OF DATA STRUCTURES • Data: Data are simply values or sets of values. • Data Item: A data item refers to a single unit of values. • Group Items: Data items that are divided into sub items are called Group items. Ex: First Name,Middle Name and Last Name • Elementary items: Data items that cannot be divided are called Elementary items. Ex: Name, Age • Entity: An entity is something that has certain attributes or properties which may be assigned values. The values may be numeric or non-numeric. Ex:Employee, item • Entity Set: Entities with similar attributes form an Entity Set. Ex: All the employees of an organization • Domain(Range) : Each attribute of an entity set has a range of values, the set of all possible values that could be assigned to the particular attribute called Domain. OVERVIEW OF DATA STRUCTURES • Field: It is a single elementary unit of information representing an attribute of an entity. • Record: it is the collection of field values of a given entity. Based on length, records can be classified as fixed-length records and variable-length records. (i) Fixed-length records: All records contain the same data items with the same amount of space assigned to each data item. (ii) Variable-length records: Records may contain different lengths usually with a minimum and a maximum length. • File: It is a collection of records of the entities in a given entity set. • Primary Key: It is the value of a certain field that uniquely determines the record in a file. • Key values(Keys) : The values in the primary key field are called Key values. OVERVIEW OF DATA STRUCTURES • Data • It is represented in the form of text or numbers or in the form of figures, tables, graphs, pictures etc. • Data can be stored in physical devices such as on paper or in memory in the form of numbers, strings, characters etc. • Information • It is meaningful or processed data. • Information is generated in every walk of life. • The basic unit of information is a bit which is a 0 or a 1. OVERVIEW OF DATA STRUCTURES • The organization of data into fields, records and files may not be complex enough to maintain and efficiently process certain collections of data. • The choice of representation of data for computer solution depends on the following factors. (i) the kind of operations to be performed on data (ii) efficient processing of data (iii) easy access of data (iv) simple memory representation for storing data (v) expression of relationships between various real world entities. OVERVIEW OF DATA STRUCTURES The study of such data structures, includes the following three steps: (i) Logical or mathematical description of the structure. (algorithm) (ii) Implementation of the structure on a computer. (operations) (ii) Quantitative analysis of the structure, which includes determining the amount of memory needed to store the structure and the time required to process the structure. (Time-space complexity) DEFINITION- DATA STRUCTURE • Data structure is defined as a logical or mathematical model of a particular organization of data. • It is defined as an organized collection of data, their relationship and allowed operations. CLASSIFICATION OF DATA STRUCTURES PRIMITIVE AND NON PRIMITIVE DATA STRUCTURES PRIMITIVE DATA STRUCTURES • These are structures which are directly operated upon by machine level instructions. • The storage structure (memory representation) of these data structures vary from one machine to another machine. • Examples of primitive data structures are integer, real, character, boolean and pointer. NON-PRIMITIVE DATA STRUCTURES • Data Structures which are not primitive are called Non-primitive data structures. • This means that they cannot be operated upon directly by machine level instructions. • Depending on the type of relationships between data elements in a data structure, they can be classified as (i) Linear data structure (ii) Non-linear data structure LINEAR AND NON-LINEAR DATA STRUCTURES LINEAR DATA STRUCTURE A data structure is said to be linear if its elements form a sequence, or, in other words, a linear list. This type of data structure establishes the relationship of adjacency between the elements. Linear structures can be represented in two ways in memory. One way is to have the linear relationship between the elements by means of sequential memory locations. (i) Array (ii) Linked List (iii) Stack (iv) Queue NON-LINEAR DATA STRUCTURES The data structures in which data items are not arranged in order are called Non-linear data structures. They establish relationship other than the adjacency relationship. Examples: Trees, Graphs, Tables, Sets. HOMOGENEOUS AND NON-HOMOGENEOUS DATA STRUCTURE HOMOGENEOUS DATA STRUCTURE • In a homogeneous data structure, all elements are of the same type. • Example: Arrays. NON-HOMOGENEOUS DATA STRUCTURE • In a non-homogeneous data structure, the elements may or may not be of the same type. • Data values of different types are grouped together. Example: records, structures, classes. STATIC AND DYNAMIC DATA STRUCTURE STATIC DATA STRUCTURE • In a static data structure, size and structure associated with memory location are fixed at compile time. • In C programming, the keyword ‘static’ can be used with variables while declaring them. • The value of static variable remains in the memory throughout the program. DYNAMIC DATA STRUCTURE • A dynamic data structure can shrink or expand during program execution as required by the program. • Here in data structures such as references and pointers, the size of memory locations can be changed during program execution. ABSTRACT DATA TYPE ADT • Abstract Data type (ADT) is a type (or class) for objects whose behaviour is defined by a set of values and a set of operations. • The definition of ADT only mentions what operations are to be performed but not how these operations will be implemented. • This forms a mathematical construct that may be implemented using a particular hardware or software data structure. • It does not specify how data will be organized in memory and what algorithms will be used for implementing the operations. • It is called “abstract” because it gives an implementation-independent view. • The process of providing only the essentials and hiding the details is known as abstraction. • Instances of abstract objects include mathematical objects such as polynomials, integrals, vectors. • Some of the computer objects are arrays, linked list, trees, stack, queue, graphs etc. DATA STRUCTURE OPERATIONS • The basic operations performed on data structures are (i) organizing data (ii) accessing or retrieving data (iii) processing data Data Structure operations can be broadly classified as (i) Common operations - performed on primitive data structures (ii) Basic operations - performed on non-primitive data structures (iii) Special operations - performed on non-primitive data structures COMMON OPERATIONS Create This operation is used to create a storage representation for a particular data structure. It reserves memory for the program elements. It can be carried out at compile-time or at run-time. In a programming language, this operation can be performed by the variable declaration statement. For example, int x=10 ; This causes memory to be allocated for variable ‘x’ and it allows integer value 10 to be stored. Select This operation is frequently used to access data within a data structure. It is more important while accessing complex structures. In files, the method of access may be sequential or random. For example, printf(“%d”,a[5]); The array element a[5] is selected for printing. COMMON OPERATIONS • Update • This operation is used to change or modify the data in a data structure. • For example, int x=3 ; /* Declaration and initialization of x */ : x=10 ; /* Assignment statement */ • Here, initially value 3 is assigned to x. Later with the assignment statement, a value of 10 is assigned to x. This modifies the value of x from 3 to 10. • Consider n=n+5; • This expression n=n+5 modifies 10, the earlier value of n to the new value • of ‘n’ as 15. COMMON OPERATIONS • Destroy • This operation is used to destroy the data structure. • It is not an essential operation. This is because, when the program execution ends, the data structure is automatically destroyed and the memory allocated is freed. • In C programming, destroy operation, can be done using the function free(). • This helps in efficient use of memory and prevents memory wastage. C++ allows the destructor member function to destroy the object. • In Java, there is a built-in mechanism called Garbage collection, in order to free memory. BASIC OPERATIONS • Some basic operations performed on non-primitive data structures are i) Traversal ii) Searching iii) Insertion iv) Deletion • Traversal (Traversing) • It is the process of accessing(visiting) each element exactly once so as to perform certain operations on it. • For example, consider the traversal of a linear array LA with Lower Bound LB and Upper Bound UB. BASIC OPERATIONS The algorithm for traversal is Step 1: Repeat for I = LB to UB step 2 Step 2: Apply PROCESS to LA[I] [End of Step 1 loop] Step 3: Exit Enter size of array: 5 Enter elements of array 12345 ARRAY TRAVERSAL 1 2 3 4 5 // Online C compiler to run C program online //Program for traversal of linear array #include <stdio.h> int main() { int a[10],i,n; printf("\nEnter size of array: "); scanf("%d",&n); printf("\nEnter elements of array\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\nARRAY TRAVERSAL\n"); for(i=0;i<n;i++) printf("%4d",a[i]); return 0; } BASIC OPERATIONS • Searching • It is the process of finding the location of the element with a given key value in a particular data structure or finding the location of elements which satisfy one or more given conditions. • Let DATA be a collection of data elements in memory, and suppose a specific ITEM of information is given. • Searching refers to the operation of finding the location LOC of ITEM in DATA, or printing some message that ITEM does not appear there. • The search is said to be successful if ITEM does appear in DATA and unsuccessful otherwise. BASIC OPERATIONS • Let A be a linear array with n elements. • To search for a given ITEM in A , we compare ITEM with each element of A one by one. • First, we test whether A[1] = ITEM, and then we test whether A[2] = ITEM and so on. • This method which traverses A sequentially to locate ITEM is called Linear Search or Sequential Search. BASIC OPERATIONS Algorithm: Let A be a linear array with N elements. We have to search for a given ITEM of information in A i.e. specify the location LOC in A. Step 1: Initialize LOC = 0 Step 2: Repeat step 3 for I = 1 to N Step 3 : If A[I] = ITEM then LOC = I Endif [End of step 2 loop] Step 4: If LOC = 0 then Print “Search unsuccessful” Else Print “Element found in location”,LOC Endif Step 5: Exit. // Online C compiler to run C program online for(i=0;i<n;i++) { if(a[i] == item) //Program for linear search #include <stdio.h> int main() { int a[10],loc=-1,i,n,item; printf("\nEnter size of array: "); scanf("%d",&n); printf("\nEnter elements of array\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\nEnter the item to be searched"); scanf("%d",&item); { loc=i; break; } } if (loc==-1) printf("\nSearch is unsuccessful\n"); else printf("\nItem %d is found in location %d",item,loc); return 0; } Enter size of array: 5 Enter elements of array 1 8 4 9 3 Enter the item to be searched 9 Item 9 is found in location 3 ALGORITHMIC NOTATION • The algorithm name is followed by the brief description of task the algorithm performs. The description gives names and types of the variable used in the algorithm. • Actual algorithm is made up of sequence of numbered steps beginning with the phrase enclosed in the square brackets that describes that steps. • Following this is an ordered sequence of statements which describe action to be executed or tasks to be performed. Statements are executed in a left to right order. • Algorithm steps terminates with a comment enclosed in parenthesis which helps better understanding of the step. They specify no action BASIC OPERATIONS Insertion • It is the process of adding a new element to the structure. • For this, the position should first be identified, and only then the new element can be inserted. Ex: Let A be a collection of data elements in the memory of the computer. • Inserting an element at the “end” of a linear array can be easily done provided the memory space allocated for the array is large enough to accommodate the additional element. • If an element is to be inserted in the middle of the array, then, on average, half of the elements must be moved downward to new locations to accommodate the new element and keep the order of the other elements. INSERTION ALGORITHM: • Let LA be a linear array with N elements. • An element ITEM is to be inserted into the Kth position in LA. 1. [Initialize counter] Set J= N 2. Repeat steps 3 & 4 while J >=K 3. [Move Jth element downward] Set LA[J+1] = LA[J] 4. [Decrease counter] Set J = J-1 [ End of step 2 loop] 5. [Insert element] Set LA[K] = ITEM 6. [Reset N] Set N= N + 1 7. Exit //Insertion of element into array #include <stdio.h> int main() { int a[10],pos,i,item,n; printf("\nEnter size of array: "); scanf("%d",&n); printf("\nEnter elements of array:\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\nEnter the item to be inserted and its position"); scanf("%d %d",&item,&pos); printf("\nARRAY BEFORE INSERTION\n"); for(i=0;i<n;i++) printf("%3d",a[i]); for(i=n-1;i>=pos; i--) //pushing elements downwards a[i+1]=a[i]; a[pos] = item; //insertion of element in position //Display of one extra element printf("\nARRAY AFTER INSERTION\n"); for(i=0;i<=n;i++) printf("%3d",a[i]); return 0; } Enter size of array: 5 Enter elements of array: 12 34 56 78 90 Enter the item to be inserted and its position40 2 ARRAY BEFORE INSERTION 12 34 56 78 90 ARRAY AFTER INSERTION 12 34 40 56 78 90 BASIC OPERATIONS DELETION • Deletion refers to the operation of removing one of the elements from the collection of data elements. • Deleting an element at the end of the array is not difficult. • Deleting an element somewhere in the middle of the array would require that subsequent element be moved one location upward in order to fill up the array, and not leave any memory location free. ARRAY - DELETION ALGORITHM Let LA be a linear array with N elements. The Kth element of LA is to be deleted. Step 1: Set ITEM = LA[K] Step 2: Repeat Step 3 for J = K to N-1 Step 3: [Move J+1st element upward] Set LA[J] = LA[J+1] [End of Step 2 Loop] Step 4: [Reset the number N of elements in LA] Set N = N-1 Step 5: Exit. #include <stdio.h> int main() { int a[10],pos,i,item,n; printf("\nEnter size of array: "); scanf("%d",&n); printf("\nEnter elements of array:\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\nEnter the position of item to be deleted : "); scanf("%d",&pos); printf("\nARRAY BEFORE DELETION\n"); for(i=0;i<n;i++) printf("%3d",a[i]); //deletion of element in position pos item=a[pos]; printf(“\nDeleted item is %d”,item); for(i=pos;i<n;i++) a[i]=a[i+1]; //Display of one less element printf("\nARRAY AFTER DELETION\n"); for(i=0;i<n-1;i++) printf("%4d",a[i]); return 0; } Enter size of array: 5 Enter elements of array: 12 34 56 78 90 Enter the position of item to be deleted : 2 ARRAY BEFORE DELETION 12 34 56 78 90 Deleted item is 56 ARRAY AFTER DELETION 12 34 78 90 SPECIAL OPERATIONS The special operations on data structures are i) Sorting ii) Merging Sorting • It is the process of arranging elements of a particular data structure in some logical order. • The order may be either ascending or descending or alphabetical order depending on the type of data present. BUBBLE SORT ALGORITHM Let LA be a linear array with N elements BUBBLESORT(INT LA[],INT N) [NUMBER OF PASSES] STEP 1: FOR I = 1 TO N-1 [NUMBER OF COMPARISONS] STEP 2: FOR J = 1 TO N-I-1 STEP 3: IF LA[J] > LA[J+1] STEP 4: TEMP =LA[J] STEP 5: LA[J] = LA[J+1] STEP 6: LA[J+1] = TEMP [END OF STEP 2 LOOP] [END OF STEP 1 LOOP] BUBBLE SORT A A[0] A[1] A[2] A[3] A[4] 28 20 30 15 05 Number of elements n= 5 Number of passes = n-1 = 4 Pass 0: Compare A[0] and A[1]. Since 28 >20, interchange them i=0 Number of comparisons: n-i-1=4 Compare A[1] and A[2]. Since 28 <30, no change Compare A[2] and A[3]. Since 30 >15, interchange them Compare A[3] and A[4]. Since 30 >05, interchange them At the end of Pass 0, the largest element of array, 30 is bubbled upto the last position in the array 20 28 15 05 30 i=1 Pass 1: Compare A[0] and A[1]. Since 20 <28, no change Number of comparisons: n-i-1=3 Compare A[1] and A[2]. Since 28 > 15, no change Compare A[2] and A[3]. Since 28 >05, interchange them At the end of Pass 1, the second largest element of array 28 is bubbled upto the second last position in the array 20 15 05 28 30 Pass 2: Compare A[0] and A[1]. Since 20 >15, interchange them Compare A[1] and A[2]. Since 20 > 05, interchange them i=2 Number of comparisons: n-i-1=2 At the end of Pass 2, the third largest element 20 is bubbled upto the second position in the array 15 05 20 28 30 Pass 3: Compare A[0] and A[1]. Since 15 >05, interchange them At the end of Pass 3, the third largest element 20 is bubbled upto the second i=3 position in the array Number of comparisons: n-i-1=1 05 15 20 28 30 #include <stdio.h> int main() { int a[10],i,j,n,temp; printf("\nEnter size of array: "); scanf("%d",&n); printf("\nEnter elements of array:\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\nARRAY BEFORE SORTING\n"); for(i=0;i<n;i++) printf("%3d",a[i]); printf("\nBUBBLE SORT\n"); for(i=0;i<n-1;i++) //passes for(j=0;j<n-i-1;j++) //comparisons { if (a[j] >a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } printf("\nARRAY AFTER SORTING\n"); for(i=0;i<n;i++) printf("%3d",a[i]); return 0; } Enter size of array: 5 Enter elements of array: 12 56 33 78 22 ARRAY BEFORE SORTING 12 56 33 78 22 BUBBLE SORT ARRAY AFTER SORTING 12 22 33 56 78 SPECIAL OPERATIONS • Merging • It is the process of combining the elements in two different structures into a single structure. • Merging is the process of combining the elements in two different sorted lists into a single sorted list. printf("\nARRAY1\n"); #include <stdio.h> for(i=0;i<n1;i++) int merge(int[],int[],int,int); //function prototype printf("%3d",a[i]); int main() printf("\nARRAY 2\n"); { for(i=0;i<n2;i++) int a[10],b[10],i,n1,n2; printf("%3d",b[i]); printf("\nEnter size of first array: "); printf("\nMERGED ARRAY\n"); scanf("%d",&n1); merge(a,b,n1,n2); //Function call printf("\nEnter elements of first sorted array:\n"); } for(i=0;i<n1;i++) //function declaration merge(int a[], int b[], int n1, int n2) scanf("%d",&a[i]); { printf("\nEnter size of second array: "); int i,j,k,n3,m[20]; scanf("%d",&n2); n3=n1+n2; //size of merged array printf("\nEnter elements of second sorted for(i=0,j=0,k=0;k<=n3;k++) array:\n"); { if (a[i] < b[j]) for(i=0;i<n2;i++) { scanf("%d",&b[i]); m[k] =a[i]; i++; if (i>=n1) { for(k++;j<n2;j++,k++) m[k] = b[j]; } } else { m[k] =b[j]; j++; if (j>=n2) //end of list2 { for(k++;i<n1; k++,i++) m[k] = a[i]; } } } for(k=0;k<n3;k++) printf("%3d",m[k]); return 0; } MERGE SORT Enter size of first array: 3 Enter elements of first sorted array: 11 22 55 Enter size of second array: 5 Enter elements of second sorted array: 10 25 33 44 50 ARRAY1 n1 11 22 55 I = 0 I=1 I=2 I=3 …REMAINING ELEMENTS OF LIST 1/End of list1 ARRAY 2 n2 10 25 33 44 50 J=0 J=1 J=2 J=3 J=4 J=5 ---END OF LIST 2 MERGED ARRAY n3=n1+n2 10 11 22 25 33 44 50 55 K=0 K=1 K=2 K=3 K =4 k= 5 k=6 k=7 k=8 ALGORITHM • An algorithm is a well-defined list of steps for solving a particular problem. • Every problem can be solved using various different methods. • Each method can be represented by an algorithm. • The choice of the best method or the best algorithm depends on the following factors: i. An algorithm should be easy to understand, code and debug(Design of algorithms). ii. An algorithm should make use of the computer resources efficiently (Analysis of algorithms). ALGORITHM ANALYSIS •Analyzing an algorithm refers to calculating or guessing resources (computer memory, processing time) needed for the algorithm •Analysis can also be made by reading the algorithm for •logical accuracy •tracing the algorithm •implementing it •checking it with some data and with a mathematical technique to confirm its accuracy. EFFICIENCY OF AN ALGORITHM •The two major measures of the efficiency of an algorithm is Time and Space. •The space or memory requirement of a program is the memory space required to execute the program. •From the total allocated memory, a fixed portion occupies code, variable and data. •The space analysis is made only for the memory space to store data values and not that is required for the algorithm itself. TIME AND SPACE COMPLEXITY OF AN ALGORITHM • The time requirement indicates the time required for the execution of the program. • The complete time T(p) is the total of time required for compiling and executing the program. • The complexity of an algorithm is the function which gives the running time and/or space in terms of input size. • Each algorithm will involve a particular data structure. • Choice of data structure depends on type of data and frequency with which various data operations are applied. • Sometimes there is a time-space tradeoff. TIME-SPACE TRADEOFF • It refers to a choice between algorithmic solutions of a data processing problem that allows one to decrease the running time of an algorithmic solution by increasing the space to store the data and vice versa. Examples: Compressed vs. uncompressed data • A space–time tradeoff can be applied to the problem of data storage. • If data is stored uncompressed, it takes more space but less time than if the data were stored compressed (since compressing the data reduces the amount of space it takes, but it takes time to run the decompression algorithm). Lookup tables vs. recalculation • The most common situation is an algorithm involving a lookup table: an implementation can include the entire table, which reduces computing time, but increases the amount of memory needed, or it can compute table entries as needed, increasing computing time, but reducing memory requirements. APPLICATIONS OF DATA STRUCTURES 1. Provides method of representing data elements in memory of computers efficiently 2. Enables to solve relationships between the data elements that are relevant to the solution of the problem 3. Helps to describe various operations that can be performed on data such as creating, displaying, inserting and deleting 4. Describes the physical and logical relationship between the data items 5. Provides a method of accessing each element in the data structure 6. Provides a method of retrieving individual data elements 7. Provides freedom to the programmer to choose any programming language best suited to the problem MULTI DIMENSIONAL ARRAYS Declaration • type name[size1][size2]...[sizeN]; • For 3 d array, int a3d[5][10][4]; • The simplest form of multidimensional array is the two-dimensional array. • A two-dimensional array is, in essence, a list of one-dimensional arrays. • To declare a two-dimensional integer array of size [x][y], type arrayName [ x ][ y ]; MULTI DIMENSIONAL ARRAYS • Multidimensional arrays may be initialized by specifying bracketed values for each row. Following is an array with 3 rows and each row has 4 columns. • Initializing Two-Dimensional Arrays int a[3][4] = { {0, 1, 2, 3} , /* initializers for row indexed by 0 */ {4, 5, 6, 7} , /* initializers for row indexed by 1 */ {8, 9, 10, 11} /* initializers for row indexed by 2 */ }; • The nested braces, which indicate the intended row, are optional. The following initialization is equivalent to the previous example − • int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11}; MULTI DIMENSIONAL ARRAYS • Accessing Two-Dimensional Array Elements • An element in a two-dimensional array is accessed by using the subscripts, i.e., row index and column index of the array. • A[2][3] means element from second row and third column or third row and fourth column depending on whether starting index is 1 or 0 • 3d array • float y[2][4][3]; • The array y can hold 24 elements. Initialization of a 3d array int test[2][3][4] = { {{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2}}, {{13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9}}}; MULTI DIMENSIONAL ARRAYS Example 1: int table[5][5][20]; int designates the array type integer. table is the name of our 3D array. Our array can hold 500 integer-type elements. This number is reached by multiplying the value of each dimension. In this case: 5x5x20=500. Example 2: float arr[5][6][5][6][5]; Array arr is a five-dimensional array It can hold 4500 floating-point elements (5x6x5x6x5=4500). // C Program to store and print 12 values entered by the user // Printing values with proper index #include <stdio.h> int main() { int a[2][3][2]; printf("Enter the values: \n"); for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { for (int k = 0; k < 2;k++) { scanf("%d", &a[i][j][k]); } } } printf("\nDisplaying values:\n"); for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { for (int k = 0; k < 2; k++) { printf("test[%d][%d][%d] = %d\n", i, j, k, test[i][j][k]); } } } return 0; } SPARSE MATRICES •Definition: If most of the elements of the matrix have 0 value, then it is called a sparse matrix. Advantages of Sparse Matrix •Storage: There are lesser non-zero elements than zeros and thus lesser memory can be used to store only those elements. It evaluates only the non-zero elements. •Computing time: In the case of searching in sparse matrix, we need to traverse only the non-zero elements rather than traversing all the sparse matrix elements. It saves computing time by logically designing a data structure traversing non-zero elements. SPARSE MATRICES 00304 00570 00000 02600 • Representing a sparse matrix by a 2D array leads to wastage of lots of memory as zeroes in the matrix are of no use in most of the cases. • So, instead of storing zeroes with non-zero elements, we only store non-zero elements. • This means storing non-zero elements with triples- (Row, Column, value). • Sparse Matrix Representation can be done in many ways following are two common representations: 1. Array representation 2. Linked list representation SPARSE MATRICES • 2D array is used to represent a sparse matrix in which there are three rows named as • Row: Index of row, where non-zero element is located • Column: Index of column, where non-zero element is located • Value: Value of the non zero element located at index – (row,column) #include <stdio.h> int main() { int sparse_matrix[4][5] = { {0 , 0 , 6 , 0 , 9 }, {0 , 0 , 4 , 6 , 0 }, {0 , 0 , 0 , 0 , 0 }, {0 , 1 , 2 , 0 , 0 } }; int size = 0; for(int i=0; i<4; i++) 0 0 6 0 9 { 0 0 4 6 0 for(int j=0; j<5; j++) 0 0 0 0 0 0 1 2 0 0 { if(sparse_matrix[i][j]!=0) { 0 0 1 1 3 3 size++; 2 4 2 3 1 2 } 6 9 4 6 1 2 } } int matrix[3][size]; int k=0; // Computing final matrix for(int i=0; i<4; i++) { for(int j=0; j<5; j++) { if(sparse_matrix[i][j]!=0) { matrix[0][k] = i; matrix[1][k] = j; matrix[2][k] = sparse_matrix[i][j]; k++; } } } // Displaying the final matrix for(int i=0 ;i<3; i++) { for(int j=0; j<size; j++) { printf("%d ", matrix[i][j]); printf("\t"); } printf("\n"); } return 0; } BINARY SEARCH • Binary Search method is very fast and efficient. • This method requires the list of elements to be in sorted order. • The binary search method can be done in two ways: • 1. Iterative method • 2. Recursive method BINARY SEARCH - ITERATIVE METHOD • To search an element, it is compared with middle element present in the the list. If it matches, then the search is successful. • Otherwise, the list is divided into two halves one from the first element to the middle element(first half) and another from the middle element to the last element(second half). • All elements in the first half are smaller than middle element and all elements in second half are greater than the middle element. • The searching will proceed in either of the two halves depending upon whether the element is greater or smaller than the middle element. • The same procedure is repeated within each half until the element is found or dividing part gives one element. BINARY SEARCH ALGORITHM ALGORITHM: DATA is a sorted array with lower bound LB and upper bound UB and ITEM is a given item of information. The variables BEG, END, MID denote respectively, the beginning, end and middle locations of a segment of elements of DATA. Step 1: [Initialize segment variables] Set BEG LB, END UB and MID INT((BEG + END)/2) Step 2 : Repeat steps 3 & 4 while BEG <= END and DATA[MID] <>ITEM Step 3: If ITEM < DATA[MID] Then Set END MID – 1 Else Set BEG MID + 1 Endif Step 4: Set MID INT ((BEG + END)/2) [End of Step 2 loop] BINARY SEARCH ALGORITHM Step 5: If DATA[MID] = ITEM then Set LOC MID Else Set LOC NULL Endif Step 6: Exit SAMPLE DATA: List of elements :11 22 30 33 40 n = 5 ITEM to be searched = 33 BEG = 1 END = 5 MID = 3 Item > mid point element, so search in upper half segment So BEG = MID + 1 = 4 END = 5 MID = 4 ITEM = MID . Therefore, location LOC is 4. Binary Search –Iterative method #include <stdio.h> #include <conio.h> binsearch(int,int,int); main() { int a[10], i,item, n; clrscr(); printf(“\n Enter number of elements in list”); scanf(“%d”,&n); printf(“\n Enter the elements of list”); for (i=1;i<=n;i++) scanf(“%d”,&a[i]); printf(“\n Enter the item to be searched”); scanf(“%d”, &item); binsearch (a,n,item); return; } binsearch(int a[], int n, int item) { int beg= 1, end = n, mid; while (beg <= end) { mid = (beg + end)/2; if (item == a[mid]) { printf(“\n Item %d is found at location %d”, item, mid); return; } if (item > a[mid]) beg = mid + 1; else end = mid – 1; } printf(“\n Item %d is not found”, item); return; } Binary Search – Recursive method /* A C function for recursive solution for binary search */ BinarySearch(int a[], int item, int lb, int ub) { if (ub < lb) return 0; mid = (lb + ub) / 2; if (item < a[mid]) return BinarySearch(a, item, lb,mid-1); else if (item > a[mid]) return BinarySearch(a, item,mid+1,ub); else return mid; } #include <stdio.h> /*Program for Binary Search – Recursive solution */ binsearch(int,int,int [],int); binsearch(int beg, int end, int printf(“\nEnter the item void main() a[], int item) to be searched : “); { { scanf(“%d”,&item); int a[10],i,item,n,loc; loc=binsearch(1,n,a,item); int mid; mid = (beg+end)/2; if(loc==0) clrscr(); if(end<beg) printf(“\nItem %d not printf(“\nEnter the size of the return 0; found”,item); array : “); if(item == a[mid]) else scanf(“%d”,&n); return mid; printf(“\nItem %d found printf(“\nEnter elements of the else at position %d”,item,loc); array\n”); if(item > a[mid] ) getch(); for(i=1;i<=n;i++) return } { binsearch(beg,mid-1,a,item); else printf(“\n Element[%d] : “,i); return scanf(“%d”,&a[i]); binsearch(mid+1,end,a,item); } } COMPLEXITY OF BINARY SEARCH ALGORITHM • Complexity is measured by the number f(n) of comparisons to locate ITEM in DATA where DATA contains ‘n’ elements. • The best case of Binary Search occurs when the element to be search is in the middle of the list. In this case, the element is found in the first step itself and this involves 1 comparison. • Therefore, Best Case Time Complexity of Binary Search is O(1). • The worst case of Binary Search occurs when the element is to search is in the first index or last index. In this case, the total number of comparisons required is logN comparisons. • Therefore, Worst Case Time Complexity of Binary Search is O(logN). • Average Case Time Complexity of Binary Search is O(logN). • Space Complexity of Binary Search: O(1) for iterative, O(logN) for recursive. Binary Search ADVANTAGES 1. It is the most efficient method of searching a sequential table without the use of any auxiliary index table. 2. It reduces the number of comparisons considerably. The worst case comparison is log2n. 3. It can be used for fast accessing and searching. DISADVANTAGES 1. It can be only used if the table is stored as an array whereas other search techniques can be used for linked list storage. 2. It requires that the list be sorted and one must have direct access to the middle element in any sub-list. Keeping data in a sorted array is normally very expensive when there are many insertions and deletions. Binary Search Merge Sort • Merge sort involves technique of divide and conquer • A given set of inputs are split into distinct subset and the required method is applied on each subset separately using recursion. • List has ‘n’ elements. It is split into 2 subsets. Each set is individually sorted. • The resultant sequence is then combined to produce a single sorted sequence of ‘n’ elements. • The subset is further recursively divided into smaller subsets until each subset is small enough to be solved independently without splitting. • The sorted subsets are combined to get a single list. Merge sort algorithm 1. Divide the list of elements into two equal parts 2. Sort the elements on the left part of the list recursively 3. Sort the elements on the right part of the list recursively 4. Merge the sorted left and right lists into a single sorted list • The list is divided into two parts based on the midvalue. If low is the index of first elements and high is the index of the last element, then Mid= (low+high)/2 is calculated. • The left part of the list consists of elements from the index low to mid and the right part of the list contains the elements with the index mid+1 to high Merge sort algorithm Step 1: If (low<high) then Step 2: mid=(low+high)/2 Step 3: Call mergesort(a,low,mid) Step 4: call mergesort(a,mid+1,high) Step 5: call merge(a,low,mid,high) [Endif] Step 6: Return Merge function Step 1: I = low Step 2: j = mid+1 Step 3: k = low Step 4: While (i<=mid) && (j<=high) do Step 5: if a[i]<a[j] c[k]=a[i] k=k+1 i=i+1 else c[k]=a[j] k=k+1 j=j+1 [Endif] [End of while Loop] Merge function contd…. Step 6: While i<=mid c[k]=a[i] k=k+1 i=i+1 [End of While loop] Step 7: While j<=high c[k]=a[j] k=k+1 j=j+1 [End of While loop] Step 8: for I = low to k-1 a[i]=c[i] [End of for loop] Step 9: Return C program for Merge sort #include <stdio.h> mergesort(int *,int,int); merge(int *,int,int,int); void main() { int a[20],n,i; clrscr(); printf(“\nEnter the number of elements:”); scanf(“%d”,&n); printf(“\nEnter the elements\n”); for(i=0;i<n;i++) scanf(“%d”,&a[i]); printf(“\nOriginal list\n”); for(i=0;i<n;i++) printf(“%d\t”,a[i]); mergesort(a,0,n-1); printf(“\nSorted list\n”); for(i=0;i<n;i++) printf(“%d\t”,a[i]); return; } C program for Merge sort mergesort(int a[],int lb,int ub) { Int mid; If (lb<ub) { mid = (lb+ub)/2; mergesort(a,lb,mid); mergesort(a,mid+1,ub); merge(a,lb,mid,ub); } return 0; } Merge (int a[],int lb, int mid,int ub) { Int I,j,k,c[20]; i=lb; K=lb; J=mid+1; While((i<=mid) && (j<=ub)) {if(a[i] <a[j]) { C[k] =a[i]; K++; i++; } Else { C[k]=a[j]; K++; J++; } } While(i<=mid) { c[k] =a[i]) K++; i++; } While(j<=ub) { c[k] =a[j]) k++; i++; } for (i=lb;i<=k-1;i++) a[i]=c[i]; } Output: Enter the number of elements : 7 Enter the elements: 25 57 48 37 12 92 86 Original List 25 57 48 37 12 92 86 Sorted List 12 25 37 48 57 86 92 Merge sort • The time complexity of MergeSort is O(n*Log n) in all the 3 cases (worst, average and best) as the mergesort always divides the array into two halves and takes linear time to merge two halves. • Dividing the input into two halves - logarithmic time complexity ie. log(N) where N is the number of elements. Quick Sort • It is used when large amount of data is to be sorted. • It uses moderate amount of resources. • Divide and conquer approach is used. • It is easy to implement. • It is also known as Partition Exchange sort • Partition is created at a position such that the elements to the left of the partition are less than the elements to the right of the partition. • It is based on dividing the list into the sublist until the sorting is completed. Quick Sort 1. 2. 3. 4. 5. 6. Consider an array of N elements. One of the elements should be chosen as the key element. Let the first element be chosen as the key element. Two pointers up and down are used. The pointer up moves from first to last element. The pointer down moves from last to the first element. In the forward direction, 1. 2. 7. As long as the element is less than key element up pointer moves each time. If element is greater than key element, the process is stopped. In the backward direction, • As long as element is greater than key element, down pointer moves each time. • If element is less than key element, the process is stopped. 9. The elements a[up] and a[down] are exchanged. Quick Sort 11. The same procedure is continued until the two pointers cross over i.e. up>down Quick Sort Recursive algorithm: 1. If there are one or less elements in the array to be sorted, return immediately (terminating condition for recursive procedure) 2. Pick an element in the array to serve as pivot point (left most element or first element is array is used) – key element 3. Split the array into two parts: one with elements larger than the pivot and the other with elements smaller than the pivot 4. Recursively repeat the algorithm for both halves or the original array. Quick Sort Index 0 1 2 3 4 Element 49 37 15 94 32 73 lb up 5 ub down Number of elements n = 6 Lower bound lb = 0 Upper bound ub = 5 First element a[0]=49 is key element Up=lb+1 Down = ub = 5 Step 1: Index 0 1 2 3 4 5 Element 49 37 15 94 32 73 lb up Index 0 1 2 3 4 5 Element 49 37 15 94 32 73 Ub, dow n Step 2: lb up Ub, dow n Check key>a[up] 49>37 is true. Then up is incremented by 1. up=2 Check key>a[up] 49>15 is true. Then up is incremented by 1. up=3 Quick Sort Step 3: Index 0 1 2 3 4 5 Element 49 37 15 94 32 73 lb up ub, dow n Step 4: Index 0 1 2 3 4 5 Element 49 37 15 94 32 73 up down ub lb Index 0 1 2 3 4 5 Element 49 37 15 32 94 73 up down ub lb Check key>a[up] 49>94 is false. Then stop incrementing up pointer value Is key<a[down} 49<73 is true. Decrement down pointer. Check key<a[down] 49<32 is false Then stop decrementing down pointer If up<down exchange a[up] and a[down]. Since 3<4, a[3] which is 94 and a[4] which is 32 are swapped. Quick Sort Step 5: Index 0 1 2 3 4 5 Element 49 37 15 32 94 73 Up, down ub lb Step 6: Index 0 1 2 3 4 5 Element 49 37 15 32 94 73 down Up ub lb Check key>a[up] 49>32 is true. Then up is incremented by 1. up=3+1=4 Check key>a[up] 49>94 is false. Stop incrementing value of up pointer. So up=4 Check 49<94 is true. So decrement down pointer by 1. so down = 4-1=3 Quick Sort Step 7: Index 0 1 2 3 4 5 Element 32 37 15 49 94 73 down Up ub lb Step 8: Index 0 1 2 3 4 5 Element 32 37 15 49 94 73 down Up ub lb Check key <a[down] 49<32 is false So stop decrementing down pointer If up > down exchange a[down] and key Key element 49 has been placed at the correct position in the array. All the elements that are less than the element in the 3rd position are to the left. All the elements that are greater than the element in the 3rd position are to the right. So the list is now divided into two sub lists : list1 and list2 The same procedure is applied to list1 and list2 separately. At the end of each stage, one element reaches its correct position in the list. Quick Sort #include <stdio.h> quicksort(int a[],int lb,int ub); main() { int I,n,a[20],lb,ub; clrscr(); printf(“\nEnter size of array”); scanf(“%d”,&n); printf(“\nEnter elements of array\n”); for(i=1;i<=n;i++) scanf(“%d”,&a[i]); lb=1; ub=n; printf(“\nOriginal List\n”); for(i=1;i<=n;i++) printf(“%4d”,a[i]); quicksort(a,lb,ub); printf(“\nSorted list\n”); for(i=1;i<=n;i++) printf(“%4d”,a[i]); return; } while(flag) { quicksort(int a[],int lb,int ub) up++; while (a[up] < key) { up++; int up,down,temp,key; while (a[down]>key) down--; int flag=1; if(up<down) if(lb <ub) { { temp =a[up]; up=lb; a[up]=a[down]; a[down]=temp; down=ub;key=a[lb]; } else flag=0; } Quicksort temp=a[lb]; a[lb]=a[down]; a[down]=temp; quicksort(a,lb,down-1); quicksort(a,down+1,ub) ; } return; } Output: Ener the size of array: 6 Enter elements of array 46 37 15 94 32 73 Original List 46 37 15 94 32 73 Sorted List 15 32 37 46 73 94 Quicksort • Complexity of the Quick Sort Algorithm • To sort an unsorted list with 'n' number of elements, we need to make ((n-1)+(n-2)+(n-3)+......+1) = (n (n-1))/2 number of comparisions in the worst case. If the list is already sorted, then it requires 'n' number of comparisions. • Worst Case : O(n2) Best Case : O (n log n) Average Case : O (n log n) REVIEW QUESTIONS Differentiate between Data and Information. Differentiate between Group and Elementary items. Give examples. What are the factors on which the choice of representation of data for computer solution depends? 2. Define Data structure. 3. What is Abstract Data Type ? 4. Explain classification of data structures. 5. What are primitive data structures? 6. What is non-primitive data structure ? 7. Differentiate between linear and non-linear data structures. 8. Explain Linear data structure. Give an example. 9. What is Non-linear data structure ? Give an example. 10. What is Homogeneous data structure ? Give example. 1. 2. 3. REVIEW QUESTIONS 11. What is Non-Homogeneous data structure ? Give example. 12. Differentiate between static and Dynamic data structures. 13. Explain Data structure operations performed on primitive data structures. 14. Explain various data structure operations performed on non-primitive datastructures. 15. Explain the classification of data structures with examples. 16. Explain the classification of data structure operations. 17. Explain Abstract Data Type. REVIEW QUESTIONS 18. Write an algorithm to traverse an array of N elements. 19. Write an algorithm to insert an element at a specific position in an array. 20. Write an algorithm to delete an element from a given position in an array. 21. Write an algorithm to search for a given element in an array using Sequential search algorithm. 22. Write an algorithm to sort array elements in ascending order using Bubble sort technique 23. Write an algorithm to merge two sorted lists into a single list of elements. 24. Write C programs for all the above algorithms. 25. What are the factors to be considered in choosing best method for algorithms? REVIEW QUESTIONS 26. What is algorithm analysis? Explain steps involved in Algorithm analysis. 27. Explain the two major measures of efficiency of algorithms. 28. What is time space tradeoff? Give any two examples. 29. List the applications of data structures. 30. How is a multi dimensional array declared and initialized? Give examples. 31. Write a C program to read and print elements in a 3 dimensional array. 32. Define Sparse matrix. What are its advantages? 33. How is a sparse matrix represented in memory? Give one example. 34. Write a C program to represent a 2d sparse matrix of size m x n to array representation with row,column and value. REVIEW QUESTIONS 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. Explain Binary search algorithm with a suitable example. Write iterative method of Binary search algorithm. Write a C function for recursive binary search algorithm. Write a C program for Binary search algorithm implementation. Compare Linear search and Binary search. Write the time complexity of Binary search algorithm. List the advantages and disadvantages of Binary search. Explain Merge sort with an example. Write Merge sort algorithm. Write a C program for Merge sort. What is the time complexity of Merge sort. REVIEW QUESTIONS 46. Explain Quick sort algorithm with a suitable example. 47. What is the other name for Quick sort algorithm? 48. What algorithm design strategy is used for designing Binary search, merge sort and quick sort algorithms? 49. Write C program for Quick sort. 50. Discuss time complexity of Quick sort.