8/6/2019 Introducing Data Structures and Algorithms
1/23
Ateneo de Naga University
Welcome toCOEM3111st Semester, S/Y 2011-2012
Robert John F. Buena, CpEinstructor
8/6/2019 Introducing Data Structures and Algorithms
2/23
Data
Structuresand
Algorithms
8/6/2019 Introducing Data Structures and Algorithms
3/23
Overview of DataStructures and Algorithms
8/6/2019 Introducing Data Structures and Algorithms
4/23
Obj ectivesFind out why you need to knowabout data structures and
algorithmsDiscover what data structures andalgorithms areLearn some terminology well usein the rest of the book
8/6/2019 Introducing Data Structures and Algorithms
5/23
W hat is a data structure?an arrangement of data in acomputers memory (or sometimes on a disk)include linked lists, stacks,binary trees, and hash ta bles,among others
8/6/2019 Introducing Data Structures and Algorithms
6/23
W hat is an algorithm?detailed instructions for carrying out some operationmanipulates the data in thosestructures in various ways,such as inserting a new dataitem, searching for aparticular item, or sorting theitems
8/6/2019 Introducing Data Structures and Algorithms
7/23
Some Uses for DataStructures and Algorithms
Real-world Data StorageProgrammers ToolModeling
8/6/2019 Introducing Data Structures and Algorithms
8/23
Real-world Data Storagedata that descri bes physicalentities external to the computer e.g personnel record, inventoryrecord, financial transaction
recordCard file program (simulates abox of index cards)
8/6/2019 Introducing Data Structures and Algorithms
9/23
Q uestions to answer inwriting a card file program
y How would you store the data in your computers memory?
y
W ould your method work for a hundred filecards? A thousand? A million?y W ould your method permit quick insertion of
new cards and deletion of old ones?y W
ould it allow for fast searching for a specifiedcard?y Suppose you wanted to arrange the cards in
alpha betical order. How would you sort them?
8/6/2019 Introducing Data Structures and Algorithms
10/23
Programmers Toolsome data storage structures arenot meant to be accessed by the
user, but by the program itself programmer uses suchstructures as tools to facilitatesome other operationstacks, queues, and priorityqueues are often used in thisway
8/6/2019 Introducing Data Structures and Algorithms
11/23
Real-world Modelingsome data structures directlymodel a real-world situationstacks, queues, and priorityqueues are often used for thispurpose
a queue, for example, can modelcustomers waiting in line at abank
8/6/2019 Introducing Data Structures and Algorithms
12/23
Overview of Data StructuresData Structure Advantages Disadvantages
Array Q uick insertion, veryfast access if index known. Slow search, slow deletion, fixed
size.Ordered array Q uicker search than
unsorted array. Slow insertion and deletion, fixed size.Stack Provides last-in,
first-out access. Slow access to other items.
Q ueue Provides first-in,first-out access. Slow access to other items.
Linked list Q uick insertion, quickdeletion. Slow search.
Binary tree Q uick search, insertion,deletion (if treeremains balanced). Deletion algorithm is
complex.Red- black tree Q uick search, insertion,
deletion. Tree alwaysbalanced. Complex.
8/6/2019 Introducing Data Structures and Algorithms
13/23
Overview of Data StructuresData Structure Advantages Disadvantages2-3-4 tree Q uick search, insertion,
deletion. Tree alwaysbalanced. Similar treesgood for disk storage. Complex.
Hash ta ble Very fast access if keyknown. Fast insertion. Slow deletion, accessslow if key not known,inefficient memory usage.
Heap Fast insertion,deletion,access to largest item. Slow access to other items
8/6/2019 Introducing Data Structures and Algorithms
14/23
Overview of AlgorithmsInsert a new data itemSearch for a specified itemDelete a specified itemtraverse - visiting each item in turn
sorting - ordered arrangement of items based on a keyrecursion - function calling itself
8/6/2019 Introducing Data Structures and Algorithms
15/23
Initial Definitionsdata filerecordsfieldskeysearch key
8/6/2019 Introducing Data Structures and Algorithms
16/23
Data filea collection of similar data itemse.g. if you create an address book using
the Card file program, the collection of cards youve created constitutes a datafile.the word f ile should not be confused with
the files stored on a computers hard disk.a data file refers to data in the real world,which might or might not be associatedwith a computer.
8/6/2019 Introducing Data Structures and Algorithms
17/23
RecordR ecords are the units into which a data file isdivided.They provide a format for storing information.
In the Card file program, each card representsa record.A record includes all the information a boutsome entity, in a situation in which there are
many such entities.A record might correspond to a person in apersonnel file, a car part in an auto supplyinventory, or a recipe in a cook book file.
8/6/2019 Introducing Data Structures and Algorithms
18/23
FieldsA record is usually dividedinto several f ieldsA field holds a particular kindof dataMore sophisticated data baseprograms use records withmore fields than Card file has
8/6/2019 Introducing Data Structures and Algorithms
19/23
Key
To search for a record withina data file you must designateone of the records fields as ak ey key unloc k s the entire record
8/6/2019 Introducing Data Structures and Algorithms
20/23
8/6/2019 Introducing Data Structures and Algorithms
21/23
Summaryy A data structure is the organization of data in acomputers memory (or in a disk file).
y The correct choice of data structure allows ma jor
improvements in program efficiency.y Examples of data structures are arrays, stacks,
and linked lists.y An algorithm is a procedure for carrying out a
particular task.y Data structures can be used to build data files.y A data file is a collection of many similar records.y Examples of data files are address books, recipe
books, and inventory records.
8/6/2019 Introducing Data Structures and Algorithms
22/23
Summaryy Data structures can also be used as
programmers tools: they help execute analgorithm.
y A record often represents a real-world o bject, likean employee or a car part.
y A record is divided into fields. Each field storesone characteristic of the o bject descri bed by therecord.
y A key is a field thats used to carry out some
operation on the data. For example, personnelrecords might be sorted by a LastName field.y A datafile can be searched for all records whose
key field has a certain value. This value is calleda search key.
8/6/2019 Introducing Data Structures and Algorithms
23/23
Workshop
1. W hat is a data structure?2. W hat is an algorithm?
3. Name two things you can use datastructures for.
4. Name an algorithm commonly applied tostored data.
5. True or false: There is only one record in adata file.