IT 4043
Data Structures and Algorithms
Budditha Hettige
Department of Computer Science
1
IT4043: Data Structures and Algorithms
• Lectures
– 30 Hours
• Practical /Tutorials
– 30 hours
• Assignment (30%)
– Practical Test (10 marks)
– Quizzes 4 (Get maximum 2 ) (10 x2 = 20 marks )
– Total 30%
• Final Examination (70 %)
– 4 Questions (3 hours) (70%)
• Reference:
– https://budditha.wordpress.com/
7/11/2015 Budditha Hettige (budditha@yahoo.com) 2
Syllabus
• Introduction to DSA
• Abstract Data Types
• List Operation Using Arrays
• Stacks
• Queues
• Recursion
• Link List
• Sorting
• Searching
• Algorithms Analysis
3
Introduction
Data Structure
• Data + Structure
• A data structure is an arrangement of data in a
computer’s memory (or sometimes on a disk).
• Example:
– arrays
– linked lists
– Stacks
– Queues
– binary trees
5
• Algorithms
• Algorithms manipulate the data in these
structures in various ways, such as searching for
a particular data item and sorting the data.
6
Example:
Algorithms for searching/Sorting
7
Example
• Write a Java program enter 10 integer number in to an
array
• Write 2 method findMin() and findMax() that returns
smallest and largest value of the above data set.
• Write a method name findItem(int index) that return a
value of the given array index
8
7 3 9 2 4 7 5 3 6 8
Data Structures and Algorithms for..
• Real-world data storage
– A personnel record describes an actual human being
– An inventory record describes an existing car part or
grocery item
– A financial transaction record describes, an actual
check written to pay the electric bill.
• Programmer’s tools
• Modeling
Data Structures and Algorithms 9
Advantages & Disadvantages
10
Abstract Data Types (ADT)
12
Abstract Data Type (ADT)
• ADT is a collection of data and a set of
operations that can be performed on the data.
• It enables us to think abstractly about the data …
• We can separate concepts from
implementation.
• Typically, we choose a data structure and
algorithms that provide an implementation of an
ADT.
ADT
13
Data Methods
• Simple Example
– Points
– Line
– Shape
– Length
– Matrix
– Vector
• Advanced Example
– List
– Stack
– Queue
– Link List
– Trees
Point Example
• Methods
– Point()
– Point(int x, int y)
– Point(Point p)
– getX()
– getY()
– setX()
– setY()
– getLength()
– addX(int x)
– addY(int y)
14
Line Example
• Attribute
– 2 Line
• Methods
– Line()
– Line(P1, P2)
– getLength()
– getArea()
– getX()
– getY()
15
Shape Example
16
Example –Length (feet-inch)
• Length()
• Length(Feet, Inch)
• Add(L1, L2)
• Subtract(L1, L2)
• Print ()
• getFromMeter(float Len )
• getMeter()
17
List
19
Linear List
• Definitions
– Linear list is a data object whose instances are of the form
(e1,e2,…,en)
– ei is an element of the list.
– e1 is the first element, and en is the last element.
– n is the length of the list.
– When n = 0, it is called an empty list.
– e1 comes before e2, e2 comes before e3, and so on.
• Examples
– student names order by their alphabets
– a list of exam scores sorted by descending order
20
Implementations of Linear List
• Array-based (Formula-based)
– Uses a mathematical formula to determine where (i.e., the memory address)
to store each element of a list
• Linked list (Pointer-based)
– The elements of a list may be stored in any arbitrary set of locations
– Each element has an explicit pointer (or link) to the next element
• Indirect addressing
– The elements of a list may be stored in any arbitrary set of locations
– Maintain a table such that the ith table entry tells us where the ith element
is stored
• Simulated pointer
– Similar to linked representation but integers replace the C++ pointers
21
ADT for LinearArrayList
class LinearArrayList {
Methods
Create(): create an empty linear list
Create(s): create a list with size s
IsEmpty(): return true if empty, false otherwise
ISFull(): return true if List is full
Length(): return the list size
Find(k): return the kth element of the list
Search(x): return the position of x in the list
Delete(k): delete the kth element and return it
Insert(k,x): insert x just after the kth element
Print(out): put the list into the output stream out
Add(x): Add new item to the list
Clear(): clear all items
Remove(): Remove top item of the list
}
Array List
• createList()
• insertFirst()
• insertLast()
• insertMiddle()
• isFull()
• findItems()
• print()
• clear()
22
Array List
• Remove Item from list
23
Example (Java Code)
24

Data Structures 01

  • 1.
    IT 4043 Data Structuresand Algorithms Budditha Hettige Department of Computer Science 1
  • 2.
    IT4043: Data Structuresand Algorithms • Lectures – 30 Hours • Practical /Tutorials – 30 hours • Assignment (30%) – Practical Test (10 marks) – Quizzes 4 (Get maximum 2 ) (10 x2 = 20 marks ) – Total 30% • Final Examination (70 %) – 4 Questions (3 hours) (70%) • Reference: – https://budditha.wordpress.com/ 7/11/2015 Budditha Hettige (budditha@yahoo.com) 2
  • 3.
    Syllabus • Introduction toDSA • Abstract Data Types • List Operation Using Arrays • Stacks • Queues • Recursion • Link List • Sorting • Searching • Algorithms Analysis 3
  • 4.
  • 5.
    Data Structure • Data+ Structure • A data structure is an arrangement of data in a computer’s memory (or sometimes on a disk). • Example: – arrays – linked lists – Stacks – Queues – binary trees 5
  • 6.
    • Algorithms • Algorithmsmanipulate the data in these structures in various ways, such as searching for a particular data item and sorting the data. 6
  • 7.
  • 8.
    Example • Write aJava program enter 10 integer number in to an array • Write 2 method findMin() and findMax() that returns smallest and largest value of the above data set. • Write a method name findItem(int index) that return a value of the given array index 8 7 3 9 2 4 7 5 3 6 8
  • 9.
    Data Structures andAlgorithms for.. • Real-world data storage – A personnel record describes an actual human being – An inventory record describes an existing car part or grocery item – A financial transaction record describes, an actual check written to pay the electric bill. • Programmer’s tools • Modeling Data Structures and Algorithms 9
  • 10.
  • 11.
  • 12.
    12 Abstract Data Type(ADT) • ADT is a collection of data and a set of operations that can be performed on the data. • It enables us to think abstractly about the data … • We can separate concepts from implementation. • Typically, we choose a data structure and algorithms that provide an implementation of an ADT.
  • 13.
    ADT 13 Data Methods • SimpleExample – Points – Line – Shape – Length – Matrix – Vector • Advanced Example – List – Stack – Queue – Link List – Trees
  • 14.
    Point Example • Methods –Point() – Point(int x, int y) – Point(Point p) – getX() – getY() – setX() – setY() – getLength() – addX(int x) – addY(int y) 14
  • 15.
    Line Example • Attribute –2 Line • Methods – Line() – Line(P1, P2) – getLength() – getArea() – getX() – getY() 15
  • 16.
  • 17.
    Example –Length (feet-inch) •Length() • Length(Feet, Inch) • Add(L1, L2) • Subtract(L1, L2) • Print () • getFromMeter(float Len ) • getMeter() 17
  • 18.
  • 19.
    19 Linear List • Definitions –Linear list is a data object whose instances are of the form (e1,e2,…,en) – ei is an element of the list. – e1 is the first element, and en is the last element. – n is the length of the list. – When n = 0, it is called an empty list. – e1 comes before e2, e2 comes before e3, and so on. • Examples – student names order by their alphabets – a list of exam scores sorted by descending order
  • 20.
    20 Implementations of LinearList • Array-based (Formula-based) – Uses a mathematical formula to determine where (i.e., the memory address) to store each element of a list • Linked list (Pointer-based) – The elements of a list may be stored in any arbitrary set of locations – Each element has an explicit pointer (or link) to the next element • Indirect addressing – The elements of a list may be stored in any arbitrary set of locations – Maintain a table such that the ith table entry tells us where the ith element is stored • Simulated pointer – Similar to linked representation but integers replace the C++ pointers
  • 21.
    21 ADT for LinearArrayList classLinearArrayList { Methods Create(): create an empty linear list Create(s): create a list with size s IsEmpty(): return true if empty, false otherwise ISFull(): return true if List is full Length(): return the list size Find(k): return the kth element of the list Search(x): return the position of x in the list Delete(k): delete the kth element and return it Insert(k,x): insert x just after the kth element Print(out): put the list into the output stream out Add(x): Add new item to the list Clear(): clear all items Remove(): Remove top item of the list }
  • 22.
    Array List • createList() •insertFirst() • insertLast() • insertMiddle() • isFull() • findItems() • print() • clear() 22
  • 23.
    Array List • RemoveItem from list 23
  • 24.