Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
JTO LICE Data Structure and Algorithms LICE - JTO Limited Internal Competitive Examination STUDY MATERIAL DATA STRUCTURE AND ALGORITHMS DATA STRUCTURE AND ALGORITHMS DATA STRUCTURE AND ALGORITHMS 28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved 1 JTO LICE Data Structure and Algorithms CONTENTS 1. BASIC CONCEPT OF DATA REPRESENTATION 2. INTRODUCTION OF BASIC DATA STRUCTURES AND ALGORITHMS 3. GARBAGE COLLECTION 4. STORAGE ALLOCATION 5. C POINTERS 6. ARRAY 7. STACK 8. QUEUE 9. LINKED LIST 10. TREES AND GRAPHS 11. INTRODUCTION TO ALGORITHM DESIGN 12. RECURRENCE RELATION 13. DIVIDE AND CONQUER 14. OTHER SORTING ALGORITHMS LICE Practice test at the End of chapter SYLLABUS: Basis concepts of data representation, introduction to algorithms design and data structure, Arrays stacks and queues, linked lists, storage allocation and garbage collection, symbol tables, Searching, Sorting and Merging Techniques. 28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved 2 Data Structure and Algorithms JTO LICE 3 1. Basic concept of Data Representation Data Representation refers to the methods used internally to represent information stored in a computer. Computers store lots of different types of information: numbers text graphics of many varieties (stills, video, animation) sound At least, these all seem different to us. However, ALL types of information stored in a computer are stored internally in the same simple format: a sequence of 0's and 1's. How can a sequence of 0's and 1's represent things as diverse as your photograph, your favorite song, a recent movie, and your term paper? It all depends on how we interpret the information. Computers use numeric codes to represent all the information they store. These codes are similar to those you may have used as a child to encrypt secret notes: let 1 stand for A, 2 stand for B, etc. With this code, any written message can be represented numerically. The codes used by computers are a bit more sophisticated, and they are based on the binary number system (base two) instead of the more familiar (for the moment, at least!) decimal system. Computers use a variety of different codes. Some are used for numbers, others for text, and still others for sound and graphics. Memory Structure in Computer Memory consists of bits (0 or 1) o bytes (=8 bits) o a single bit can represent two pieces of information a single byte can represent 256 = 2x2x2x2x2x2x2x2 = 28 pieces of information words (=2,4, or 8 bytes) o a 2 byte word can represent 2562 pieces of information (approximately 65 thousand). Byte addressable - each byte has its own address. Binary Numbers Normally we write numbers using digits 0 to 9. This is called base 10. However, any positive integer (whole number) can be easily represented by a sequence of 0's and 1's. Numbers in this form are said to be in base 2 and they are called binary numbers. Base 10 numbers use a positional system based on powers of 10 to indicate their value. The number 123 is really 1 28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved JTO LICE Data Structure and Algorithms hundred + 2 tens + 3 ones. The value of each position is determined by ever-higher powers 4 of 10, read from left to right. Base 2 works the same way, just with different powers. The number 101 in base 2 is really 1 four + 0 twos + 1 one (which equals 5 in base 10). Text Text can be represented easily by assigning a unique numeric value for each symbol used in the text. For example, the widely used ASCII code (American Standard Code for Information Interchange) defines 128 different symbols (all the characters found on a standard keyboard, plus a few extra), and assigns to each a unique numeric code between 0 and 127. In ASCII, an "A" is 65," B" is 66, "a" is 97, "b" is 98, and so forth. When you save a file as "plain text", it is stored using ASCII. ASCII format uses 1 byte per character 1 byte gives only 256 (128 standard and 128 non-standard) possible characters The code value for any character can be converted to base 2, so any written message made up of ASCII characters can be converted to a string of 0's and 1's. Graphics Graphics that are displayed on a computer screen consist of pixels: the tiny "dots" of color that collectively "paint" a graphic image on a computer screen. The pixels are organized into many rows on the screen. In one common configuration, each row is 640 pixels long, and there are 480 such rows. Another configuration (and the one used on the screens in the lab) is 800 pixels per row with 600 rows, which is referred to as a "resolution of 800x600." Each pixel has two properties: its location on the screen and its color. A graphic image can be represented by a list of pixels. Imagine all the rows of pixels on the screen laid out end to end in one long row. This gives the pixel list, and a pixel's location in the list corresponds to its position on the screen. A pixel's color is represented by a binary code, and consists of a certain number of bits. In a monochrome (black and white) image, only 1 bit is needed per pixel: 0 for black, 1 for white, for example. A 16 color image requires 4 bits per pixel. Modern display hardware allows for 24 bits per pixel, which provides an astounding array of 16.7 million possible colors for each pixel! Compression Files today are so information-rich that they have become very large. This is particularly true of graphics files. With so many pixels in the list, and so many bits per pixel, a graphic file can easily take up over a megabyte of storage. Files containing large software applications can require 50 megabytes or more! This causes two problems: it becomes costly to store the files (requires many floppy disks or excessive room on a hard drive), and it becomes costly to transmit these files over networks and phone lines because the transmission takes a long time. In addition to studying how various types of data are represented, you will have the opportunity today to look at a technique known as data compression. The basic idea of 28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved JTO LICE Data Structure and Algorithms compression is to make a file shorter by removing redundancies (repeated patterns of bits) 5 from it. This shortened file must of course be de-compressed - have its redundancies put back in - in order to be used. However, it can be stored or transmitted in its shorter compressed form, saving both time and money. Practice TEST -LICE 1. In BCD code, the maximum possible characters set size is A. character set of 64 B. character set of 84 C. character set of 94 D. character set of 104 Ans-A 2. A small group of bits treated as a single unit is called as A. bits B. binary characters C. input characters D. bytes Ans-D 3. The most commonly used codes for representing the bits are A. ASCII B. BCD C. EBCDIC D. all of above Ans-D 4. The different characters that can be encoded is 2n where n is A. number of bits for each character B. number of bytes for each character C. number of mega bytes D. number of gigabytes Ans-A 5. The numbers that are written with base 16 are classified as A. whole numbers 28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved Data Structure and Algorithms JTO LICE 6 B. hexadecimal C. exponential integers D. mantissa Ans-B 6. The pictures and graphs that are to be printed with the help of chosen characters are called A. graphics characters B. alphanumeric characters C. character sets D. control characters Ans-A 7. The complete set of special characters used by any system is classified as A. control characters B. graphics characters C. character set D. alphanumeric characters Ans-C 8. The one value which is written with the mantissa and the exponent is called A. exponent B. mantissa C. base power D. floating point number Ans-D 9. In ASCII code, the maximum possible characters set size is A. character set of 128 B. character set of 138 C. character set of 148 D. character set of 158 Ans- A 10. The code 'EBCDIC' that is used in computing stands for 28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved Data Structure and Algorithms A. extended BCD interchange code JTO LICE B. extension of BCD information code C. extension of BCD interchange conduct D. extended BCD information conduct Ans- A 11. All the decimal values and all the integers are included in set of A. whole numbers B. natural numbers C. real numbers D. integers Ans- C 12. The number of bits in 'ASCII' code used in computing are A. five bits B. six bits C. twenty bits D. ten bits Ans- C 13. The symbols such as letters or any digit are called A. characters B. small bits C. small bytes D. output characters Ans- A 28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved 7 Data Structure and Algorithms JTO LICE 2. 8 Introduction of Basic Data Structures and Algorithms A data structure is an arrangement of data in a computer's memory or even disk storage. An example of several common data structures are arrays, linked lists, queues, stacks, binary trees, and hash tables. Algorithms, on the other hand, are used to manipulate the data contained in these data structures as in searching and sorting. Many algorithms apply directly to a specific data structures. When working with certain data structures you need to know how to insert new data, search for a specified item, and deleting a specific item. Commonly used algorithms include are useful for: Searching for a particular data item (or record). Sorting the data. There are many ways to sort data. Simple sorting, Advanced sorting Iterating through all the items in a data structure. (Visiting each item in turn so as to display it or perform some other action on these items) Characteristics of Data Structures Data Structure Array Ordered Array Advantages Quick inserts Fast access if index known< Faster search than unsorted array Last-in, first-out access First-in, first-out access Quick inserts Quick deletes Binary Tree Quick search Quick inserts Quick deletes (If the tree remains balanced) Red-Black Tree Quick search Quick inserts Quick deletes (Tree always remains balanced) 2-3-4 Tree Quick search Quick inserts Quick deletes (Tree always remains balanced) (Similar trees good for disk storage) Hash Table Very fast access if key is known Stack Queue Linked List Disadvantages Slow search Slow deletes Fixed size Slow inserts Slow deletes Fixed size Slow access to other items Slow access to other items Slow search Deletion algorithm is complex Complex to implement Complex to implement Slow deletes Access slow if key is not known 28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved JTO LICE Heap Graph Data Structure and Algorithms Quick inserts Quick inserts Quick deletes Access to largest item Best models real-world situations Inefficient memory usage Slow access to other items Some algorithms are slow and very complex Practice Question-MCQ LICE 1. Which of the following data structure is non-linear type? A) Strings B) Lists C) Stacks D) Tree Ans-D 2. Which of the following data structure is linear type? A) Array B) Tree C) Graphs D) Hierarchy Ans-A 3. The logical or mathematical model of a particular organization of data is called a ......... A) Data structure B) Data arrangement C) Data configuration D) Data formation Ans-A 4. The simplest type of data structure is .................. A) Multidimensional array B) Linear array C) Two dimensional array D) Three dimensional array AnsB 5. Linear arrays are also called ................... A) Straight line array B) One-dimensional array 28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved 9 JTO LICE C) Vertical array D) Horizontal array Ans-B Data Structure and Algorithms 10 6. Arrays are best data structures ............ A) For relatively permanent collections of data. B) For the size of the structure and the data in the structure are constantly changing C) For both of above situation D) For none of the above Ans-A 7. Which of the following data structures are indexed structures? A) Linear arrays B) Linked lists C) Graphs D) Trees Ans-A 8. Each node in a linked list has two pairs of .............. and ................... A) Link field and information field B) Link field and avail field C) Avail field and information field D) Address field and link field Ans-A 9. A ........................ does not keep track of address of every element in the list. A) Stack B) String C) Linear array D) Queue Ans-C 10. When does top value of the stack changes? A) Before deletion B) While checking underflow C) At the time of deletion D) After deletion Ans-D 28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved JTO LICE Data Structure and Algorithms 3. Garbage collection 11 Garbage collection (GC) is a dynamic approach to automatic memory management and heap allocation that processes and identifies dead memory blocks and reallocates storage for reuse. The primary purpose of garbage collection is to reduce memory leaks. Garbage collection implementation requires three primary approaches: Mark-and-sweep - In process when memory runs out, the GC locates all accessible memory and then reclaims available memory. Reference counting - Allocated objects contain a reference count of the referencing number. When the memory count is zero, the object is garbage and is then destroyed. The freed memory returns to the memory heap. Copy collection - There are two memory partitions. If the first partition is full, the GC locates all accessible data structures and copies them to the second partition, compacting memory after GC process and allowing continuous free memory. Storage allocation There are two type of storage allocation: 1. Static memory allocation 2. Dynamic memory allocation STATIC MEMORY ALLOCATION Static memory allocation is the allocation of memory at compile-time before the associated program is executed, unlike dynamic memory allocation or automatic memory allocation where memory is allocated as required at run-time. An application of this technique involves a program module (e.g. function or subroutine) declaring static data locally, such that these data are inaccessible in other modules unless references to it are passed as parameters or returned. A single copy of static data is retained and accessible through many calls to the function in which it is declared. Static memory allocation therefore has the advantage of modularizing data within a program design in the situation where these data must be retained through the runtime of the program DYANAMIC MEMORY ALLOCATION A computer system in which memory capacity is made available to a program on the basis of actual, momentary needduring program execution, and areas of storage may be reassigned at any time. Also known as dynamic allocation; dynamic memory allocation. Two basic operations in dynamic storage management: 28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved JTO LICE Data Structure and Algorithms o Allocate a given number of bytes o Free a previously allocated block 12 Two general approaches to dynamic storage allocation: o Stack allocation (hierarchical): restricted, but simple and efficient. o Heap allocation: more general, but less efficient, more difficult to implement. Stack Allocation A stack can be used when memory allocation and freeing are partially predictable: memory is freed in opposite order from allocation. If alloc(A) then alloc(B) then alloc(C), then it will be free(C) then free(B) then free(A). Example: procedure call. X calls Y calls Y again. Stacks are also useful for lots of other things: tree traversal, expression evaluation, top- down recursive descent parsers, etc. A stack-based organization keeps all the free space together in one place. Heap Allocation Heap allocation must be used when allocation and release are unpredictable Memory consists of allocated areas and free areas (or holes). Inevitably end up with lots of holes. Goal: reuse the space in holes to keep the number of holes small, keep their size large. Fragmentation: inefficient use of memory due to holes that are too small to be useful. Stack allocation is perfect: only one hole. Typically, heap allocation schemes use a free list to keep track of the storage that is not in use. Best fit: keep linked list of free blocks, search the whole list on each allocation, choose block that comes closest to matching the needs of the allocation, save the excess for later. During release operations, merge adjacent free blocks. First fit: just scan list for the first hole that is large enough. Free excess. Also merge on releases. Most first fit implementations are rotating first fit. Best fit is not necessarily better than first fit! First fit tends to leave "average" size holes, while best fit tends to leave some very large ones, some very small ones. The very small ones can't be used very easily. Symbol table 28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved Data Structure and Algorithms JTO LICE a symbol table is a data structure used by a language translator such as 13 a compiler or interpreter, where each identifier in a program's source code is associated with information relating to its declaration or appearance in the source. A C compiler that parses this code will contain at least the following symbol table entries: Symbol name bar Type Scope function, double Extern double function parameter function, double Global count int function parameter sum double block local int for-loop statement x foo i SAMPLE FILE Buy complete package Call +91-9990657855, 011-26514888 28-B/7, Jia Sarai, Near IIT, Hauz Khas, New Delhi-110016. Ph. 011-26514888. www.engineersinstitute.com © 2015 ENGINEERS INSTITUTE OF INDIA® . All Rights Reserved