You are on page 1of 20

# DATA STRUCTURE

The combination which describes the set of elements together with the description of all legal operation is termed as data structure. In other words data structure will tell us which are the elements required as well as the legal operations on those set of elements. For example: Consider the set of elements as integer & the operations on integer are addition, subtraction, multiplication, division etc. Therefore the data objects integer along with description of these will be a data structure. The standard way of defining data structure is as follows: Definition: A data structure is a set of domains D, a set of function F & set of axioms A. The triple (D, F, A) denotes the data structure d. Where D is the domain.

DATA STRUCTURE

## Non-Linear Data Type Trees Graphs Tables Sets

Primitive Data Type Primitive Data Types are the basic building blocks used for holding and managing data in any commonly used programming language. They are the predefine data types. From language to language the collection may vary but when we code we have a collection of different ways to hold data. Some are: integers (signed or unsigned meaning positive only or negative and positive) strings (text) chars (characters) booleans (true or false) floats/reals (numbers with decimal values) doubles (floats on steroids) enumerations (think small lists like the days of the week, suits in a deck of cards or state)

Composite Data Type These are more complex data types. These data types are derived from the primitive data types. They stress on formation of sets of homogeneous (In this type of data structures, values of the same types of data are stored, as in an array) and heterogeneous (In this type of data structures, data values of different types are grouped, as in structures and classes) data elements. They are of 2 types: 1. Linear Data Type In linear data structures, values are arranged in linear fashion. Arrays, linked lists, stacks and queues are examples of linear data structures in which values are stored in a sequence. 2. Non- Linear Data type This type is opposite to linear. The data values in this structure are not arranged in order. Tree, graph, table and sets are examples of non-linear data structures.

ARRAYS
Array means collection. An array is used to store elements of the same type. It is a very popular and useful data structure and stores data elements in contiguous locations. More than one element is arranged in sequence so it is also called a composite data structure. Array is a linear and homogenous data structure. Homogenous means that the same types of elements are stored in it. It can be combined with a non-homogenous structure and a complex data structure can be created.

When we declare a variable for example, int x; The variable x is declared & a memory location of two bytes is allocated to it. Later, a single value can be stored in it as shown below: x = 4;

The VITA concept is associated with all variables. V stands for value of a variable I stands for identification of variable T stands for type of variable A stands for address of variable. Every variable has a name, value & memory location. Hence, from above we can say that only value can be stored in a variable.

Characteristics of Array
1. Array elements are stored in successive memory locations. 2. Once the array is declared, its lowest bound cannot be changed, but the upper bound can be expanded with the C++ compiler. The array name itself is a constant pointer and we cannot modify it. Therefore, the lowest bound of an array cannot be expanded. 3. We know that the array name itself is a pointer. Though it is a pointer, it does not need the '*' operator. The brackets [] automatically denote that the variable is a pointer. 4. All the elements of an array share the same name, and they are distinguished from one another with the help of the element number. 5. The amount of memory required for an array depends upon the data type and the number of elements. Total bytes=sizeof (data type) * size of array 6. Operations such as insertion and deletion of element done with a list cannot be done with an array. Once an array is created, we cannot remove or insert memory locations. An element can be deleted or replaced but the memory location remains as it is. 7. When an array is declared and not initialized, it contains garbage values. If we declare an array as static, all elements are initialized to zero. However, the values of static type data persist and remain in the memory as long as the program executes. To overcome this problem, we initialize the first element of an array with zero or any other number. All the remaining elements are automatically initialized to zero, provided that the initialization is done in the declaration statement of array.

Types of Array
There are 3 types of array i.e.

ONE DIMENSIONAL ARRAY A one-dimensional array does not have any corresponding elements; it has only one row of elements. So far, the example we have discussed is of a one-dimensional array. For example: int Num[5] ; /* one dimensional array */

TWO DIMENSIONAL ARRAY Two-dimensional arrays can be thought of as rectangular display of elements with rows and columns. for example: int x[3][3];

THREE DIMENSIONAL ARRAY A three-dimensional array can be thought of as array of arrays. Consider the following program:

SPARSE MATRICES
A sparse matrix is a two-dimensional array in which most of the elements are null. The sparse matrices are very useful in various applications. In sparse matrices, memory is wasted because the NULL values are stored and hence to overcome wastage of memory a mechanism must be followed.

LINEAR SEARCH
The linear search is a conventional method of searching data. The linear search is a method of searching a target element in a list sequence. The expected element is to be searched in the entire data structure in a sequential method from starting to last element. Though, it is simple and straightforward, it has serious limitations. It consumes more time and reduces the retrieval rate of the system. The linear or sequential name implies that the items are stored in systematic manner. The linear search can be applied on sorted or unsorted linear data structure. Following fig. shows the searching by using the linear search method.

For example: WAP to search an element from the entered numbers. Also, indicate its position in case the element is found or unavailability. #include<stdio.h> #include<conio.h> void main() { int sn, arr[10], a; clrscr(); printf(Enter ten elements:-); for(a=0; a<10;a++) scanf(%d, &arr[a]); printf(Enter the element to be searched :-); scanf(%d, &sn); a = 0; while(a<10) { if(arr[a] == sn) { printf( The number %d is found at %d location, sn, a+1); break; } a++; } if(arr[a] !=sn) printf( The element %d is not found, sn); } OUTPUT: Enter ten elements: -12 36 64 75 89 65 159 190 91 87 Enter the element to be searched:- 64 The number 64 is found at 3 locations.

BINARY SEARCH
The binary search approach is different from the linear search. Here, search of an element is not passed in sequence as in the case of linear search. Instead, two partitions of lists are made and then the given element is searched. Hence, it creates two parts of the lists known as binary search. Binary search is quicker than the linear search. It is an efficient searching technique and works with sorted lists. However, it cannot be applied on unsorted data structure. Before applying binary search, the linear data structure must be sorted in either ascending or descending order. The binary search is unsuccessful if the elements are unordered. The binary search is based on the approach divide-and-conquer. In binary search, the element is compared with the middle element. If the expected element falls before the middle element, the left portion is searched otherwise right portion is searched.

## The binary search method is illustrated in below fig.

The above fig. represents the operation of binary search. The total elements in the list are 8, & they are shown in ascending order. In case the key element is less than 5 then searching is done in lower half otherwise in the upper half. For example: WAP to sort an array elements using binary search.

INSERTION SORT
In insertion sort the element is inserted at appropriate place. For example, consider an array of n elements. In this type, swapping of elements is done without taking any temporary variable. The greater numbers are shifted towards the end of the array and smaller are shifted to beginning. Here, a real life example of playing cards can be cited. We keep the cards in increasing order. The card having least value is placed at the extreme left and the largest one at the other side. In between them the other cards are managed in ascending order. Consider an example of playing cards. The playing cards having values 4, 6, 2, 1, 8 can be sorted according to the method described above, as below fig. illustrates the Insertion sort.

## For example: WAP to sort array elements using Insertion sort.

SELECTION SORT
The selection sort is nearly the same as exchange sort. Assume, we have a list containing n elements. By applying selection sort, the first element is compared with all remaining (n-1) elements. The smallest element is placed at the first location. Again, the second element is compared with remaining (n-2) elements. If the item found is lesser than the compared elements in the remaining (n-2) list then the swap operation is done. In this type, entire array is checked for the smallest element and then swapped. Below fig. illustrates the process of sorting elements by selection sort. In each pass, one element is sorted and kept at the left. Initially, the elements are temporarily sorted and after next pass, they are permanently sorted and kept at the left. Permanently sorted elements are covered with squares and temporarily with circles. Element inside the circle "O" is chosen for comparing with the other elements marked in a circle and sorted temporarily. Sorted elements inside the square "" are shown.

## For example: WAP to sort elements of array using Selection sort

BUBBLE SORT
Bubble sort is a commonly used sorting algorithm. In this type, two successive elements are compared and swapping is done if the first element is greater than the second one. The elements are sorted in ascending order. It is easy to understand but it is time consuming. Bubble sort is an example of exchange sort. In this method repetitively comparison is performed between the two successive elements and if essential swapping of elements is done. Thus, step-by-step entire array elements are checked. It is different from the selection sort. Instead of searching the minimum element and then applying swapping, two records are swapped instantly upon noticing that they are not in order. The below fig. illustrates bubble sort.

For example: WAP to sort a given array by using the Bubble sort

STRING
A string is a special type of linear one-dimensional array. A book can be thought of as a string of characters. A program can also be looked as a string of characters. String is an important data structure widely used in computer science for storing and manipulating textual data. Thus it is important to study techniques for representing and manipulating strings because of their widespread use. A number of programming languages provide strings as a virtual data type of the language. Some languages have a special library for handling string operations. String manipulations can also be performed using arrays and lists. A string differs from the one-dimensional array in three basic ways: (i) A string is a one-dimensional array of characters that are usually restricted to the set of printable characters and a few control characters that are useful in text based applications (ii) A string is a dynamic data structure which grows in size as new characters are added and contracts as characters are removed, in contrast to the static fixed-length of the array structure (iii) The length of the string can be obtained through a function.

STRING REPRESENATATION
Strings are an important part of any program. We can use them to write messages to the users, to read text files, and generally to make interaction with users smoothly. Computers are frequently used for processing or manipulating non-numeric data, called character data. String manipulating is often a large part of any programming task. Such processing usually involves some type of pattern matching, that is, to see if a particular word S appears in a given text T. So it requires to study string-manipulation routines. In this subsection we will look at how strings are represented and manipulated in C language. A finite sequence S of zero or more characters is called a string. The number of characters in a string is called its length. The string with zero characters is called the empty string or the null string. Normally specific strings will be denoted by enclosing their characters in double quotation marks. This mark will also act as string delimenter. For example, COMPUTER IT IS A PROGRAMME are strings with length 8 & 15 (including blank spaces, if any, since the blank space is also a character). The Internal representation of character strings in C.

STRING MANUPULATION
A string is viewed simply as a sequence or linear array of characters. Various string operations have been developed which are not normally used with other kinds of array. The importance of these string manipulation routines will become readily apparent when these routines are used to implement the complex programs. The development of these programs was greatly simplified by using the string functions. The number of characters in a string is called its length. We write a function len(String) for the length of a given string. Thus len(Program) = 7, len(This is ) = 7 len() = 0 The function len (String) is given below: Example: /* Return a count of the number of characters in the variable of string*/ len(String) char string[ ]; { int i; for(i=0; string *i+!=\0; i++) ; return(i); /*Number of characters in the string*/ } The substring of a string returns any length of consecutive characters. A substring function requires three parameters. (i) The name of the string or the string itself. (ii) The position of the first character of the substring in the given string. (iii) The length of the substring or the position of the last character in te substring. (iv) Copy of the substring into another string. We now write the substr(S1, K, L, S2) function below: Example:

There are alot useful functions such as Insert(), Index() and remove()

COMPLEXITY OF ALGORITHMS
ALGORITHM DEFINATION: After selecting the data structures, an algorithm, based on the data structure, needs to be designed. The set of rules that define how a particular problem can be solved in a finite sequence of steps is called an algorithm. An algorithm written in a computer language is called a program. An algorithm can be defined as a finite sequence of instructions, each of which has a clear meaning and can be executed with a finite amount of effort in finite time. The desirable features of an algorith are: Each step of the algorithm should be simple. It should be unambigous in the sense that the logic should be crisp & clear. It should be effective, i.e. it must be lead to unique solution of a problem. It must be end in a finite number of steps. It should be as effiecient as possible. COMPLEXITY OF ALGORITHM The complexity of an algorithm is a function describing the efficiency of the algorithm in terms of the amount of data the algorithm must process. Usually there are natural units for the domain and range of this function. There are two main complexity measures of the efficiency of an algorithm: Time complexity is a function describing the amount of time an algorithm takes in terms of the amount of input to the algorithm. "Time" can mean the number of memory accesses performed, the number of comparisons between integers, the number of times some inner loop is executed, or some other natural unit related to the amount of real time the algorithm will take. We try to keep this idea of time separate from "wall clock" time, since many factors unrelated to the algorithm itself can affect the real time (like the language used, type of computing hardware, proficiency of the programmer, optimization in the compiler, etc.). It turns out that, if we chose the units wisely, all of the other stuff doesn't matter and we can get an independent measure of the efficiency of the algorithm. Space complexity is a function describing the amount of memory (space) an algorithm takes in terms of the amount of input to the algorithm. We often speak of "extra" memory needed, not counting the memory needed to store the input itself. Again, we use natural (but fixed-length) units to measure this. We can use bytes, but it's easier to use, say, number of integers used, number of fixed-sized structures, etc. In the end, the function we come up with will be independent of the actual number of bytes needed to represent the unit. Space complexity is sometimes ignored because the space used is minimal and/or obvious, but sometimes it becomes as important an issue as time.

For example, we might say "this algorithm takes n2 time," where n is the number of items in the input. Or we might say "this algorithm takes constant extra space," because the amount of extra memory needed doesn't vary with the number of items processed. /////////////////ASYMPTOTIC NOTATION BIG-O ALGORITHM\\\\\\\\\\\\\\\\ http://www.cs.utexas.edu/users/djimenez/utsa/cs1723/lecture2.html http://www.introprogramming.info/english-intro-csharp-book/read-online/chapter-19-data-structures-andalgorithm-complexity/

RECORDS
A table or a file is a collection of elements, each of which is called a record. Each record consists of a set of fields. One of the field has a special meaning called key field. Such key is also called an internal key. Sometimes there is a separate table of keys that include pointers to the records. Such keys are called external keys. For each file, there is at least one unique key, that is, no two records have the same key value and it is called a primary key. However, keys need not always be unique. Such a key is called a secondary key. A searching method is a process that accepts an argument x & tries to locate a record whose key value is x. The process may return the entire record or it may return the address of the record. If seraching for a record is unsuccessful, then a null value will return.

POINTERS
A pointer is a link or reference to data structures. The most important characteristic of pointer is that it allows identical method of referencing any data structures, no matter of what complexity and type. The pointer can point to any data type character, float, int, etc., and the method of accessing elements are the same. A pointer also allows quick insertion and deletion of an item in a list. There are two techniques of accessing data structures. 1. Computed Address In this technique the data structures can directly be accessed using an address. The address is calculated by the compiler or interpreter, which translates the source program to an object code. 2. Pointer Addressing The C/C++ pointers are of this type, in which a memory address is assigned to a pointer variable. Depending upon the complexicity of data structures some operations are also required in this technique. However, this technique is time consuming. A pointer is a memory variable that stores a memory address of another variable. It can have any name that is acceptable for other variables and it is declared in the same way as other variables. It is always denoted by '*'. a. Pointers save memory space. b. Execution time of a program is faster because address of variable can be accessed and read / write operation with addresses directly done. c. Memory is used efficiently with pointers. d. Pointers are used with data structures. They are very useful for representing arrays. int *x; float *y; char *y;

1. In the first statement 'x' is an integer pointer and it tells the compiler that it holds the address of an integer variable. The variable f is a float pointer and it can hold the address of only the float variable. The *y is a character variable and holds the address of any character variable. 2. The indirection operator (*) is also called the deference operator. When a pointer is dereference, the pointer retrieves the value stored at that address. 3. The indirection operator (*) is used in two distinct ways with pointers, declaration and deference. 4. When the pointer is dereference, the indirection operator indicates that the value at that location stored in the pointer is to be accessed rather than address. 5. The '&' operator is an address operator and it gives the address of the variable. The '&' immediately preceding the variable returns the address of the variable.

Arithmetic operations on pointer variables are also possible. Increase, decrease, prefix and postfix operations can be performed with the help of pointers. The following operations are not possible with address: 1. Addition of two addresses (pointers). 2. Multiplication of two addresses or multiplication with a constant. 3. Division of address with a constant.