0% found this document useful (0 votes)
13 views54 pages

Data Structures and Algorithms

sdsdsds

Uploaded by

BV Thái Dương
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views54 pages

Data Structures and Algorithms

sdsdsds

Uploaded by

BV Thái Dương
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

FPT SOFTWARE WORKFORCE ASSURANCE

Data Structures & Algorithms


HoangND1

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Agenda
 Data Structures
 Collections
 Analyzing an Algorithm
 Non-primitive data structures
 Arrays
 Linked Lists
 Binary Tree
 General Tree
 Heaps
 Queues
 Stacks
 Sorting Algorithms
 Searching Algorithms
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0
Data Structures
 "Once you succeed in writing the programs for
complicated algorithms, they usually run
extremely fast. The computer doesn't need to
understand the algorithm, it’s task is only to run
the programs.“
 There are a number of facets to good programs,
they must
 run correctly
 run efficiently
 be easy to read and understand
 be easy to debug and
 be easy to modify.

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Data Structures (Cont.)
What is Data Structure ?
 A scheme for organizing related pieces of information

 A way in which sets of data are organized in a particular

system
 An organised aggregate of data items

 A computer interpretable format used for storing,


accessing, transferring and archiving data
 The way data is organised to ensure efficient processing:

this may be in lists, arrays, stacks, queues or trees


Data structure is a specialized format for organizing and
storing data so that it can be be accessed and worked with
in appropriate ways to make an a program efficient
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0
Data Structures (Cont.)
 Data structures can be classified in to
 Primitive data structures
 Non primitive data structure.
 Primitive data structure:
 These are data structures that can be manipulated directly by
machine instructions.
 In C language, the different primitive data structures are int,
float, char, double.
 Non primitive data structures:
 These are data structures that can not be manipulated directly
by machine instructions. Arrays, linked lists, files etc., are some
of non-primitive data structures and are classified into linear
data structures and non-linear data structures.

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Data Structures (Cont.)
Data Structure = Organised Data + Allowed Operations
There are two design aspects to every data structure:
the interface part
The publicly accessible functions of the type.
Functions like creation and destruction of the object,
inserting and removing elements (if it is a container),
assigning values etc.

the implementation part :


Internal implementation should be independent of
the interface. Therefore, the details of the
implementation aspect should be hidden out from
the users.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0
Collections

•Programs often deal with collections of items.


•These collections may be organised in many ways and use
many different program structures to represent them, yet, from
an abstract point of view, there will be a few common
operations on any collection.

create Create a new collection


add Add an item to a collection

delete Delete an item from a collection

find Find an item matching some criterion in the collection


destroy Destroy the collection

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Analyzing an Algorithm
 Simple statement sequence
s1; s2; …. ; sk
 Complexity is O(1) as long as k is constant
 Simple loops
for(i=0;i<n;i++) { s; } where s is O(1)
 Complexity is n O(1) or O(n)
 Loop index doesn’t vary linearly
h = 1;
while ( h <= n ) { s; h = 2 * h;}
 Complexity O(log n)
 Nested loops (loop index depends on outer loop index)
for(i=0;i<n;i++) f
for(j=0;j<n;j++) { s; }
 Complexity is n O(n) or O(n2)

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Arrays
An Array is the simplest form of implementing a collection
 Each object in an array is called an array element
 Each element has the same data type (although they may have different
values)
 Individual elements are accessed by index using a consecutive range of
integers
One Dimensional Array or vector
int A[10];
for ( i = 0; i < 10; i++)
A[i] = i +1;

A[0] A[1] A[2] A[n-2] A[n-1]

1 2 3 N-1 N

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Arrays (Cont.)
Multi-dimensional Array
A multi-dimensional array of dimension n (i.e., an n-dimensional array or simply n-
D array) is a collection of items which is accessed via n subscript expressions. For
example, in a language that supports it, the (i,j) th element of the two-dimensional
array x is accessed by writing x[i,j].

Column
0 1 2 3 4 5 6 7 8 9 10 j n
0
1
R
2
O
: : : : : : : : : : : : : : :
W
i x

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Arrays (Cont.)

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Array : Limitations
 Simple and Fast but must specify size during construction
 If you want to insert/ remove an element to/ from a fixed
position in the list, then you must move elements already in the
list to make room for the subsequent elements in the list.
 Thus, on an average, you probably copy half the elements.
 In the worst case, inserting into position 1 requires to move all
the elements.
 Copying elements can result in longer running times for a
program if insert/ remove operations are frequent, especially
when you consider the cost of copying is huge (like when we
copy strings)
 An array cannot be extended dynamically, one have to allocate a
new array of the appropriate size and copy the old array to the
new array
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0
Linked Lists
 The linked list is a very flexible dynamic data structure:
items may be added to it or deleted from it at will
 Dynamically allocate space for each element as needed
 Include a pointer to the next item
 the number of items that may be added to a list is limited only by
the amount of memory available
Linked list can be perceived as connected (linked) nodes
Each node of the list contains
 the data item Data Next
 a pointer to the next node
 The last node in the list contains a NULL pointer to
indicate that it is the end or tail of the list.
object

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Linked Lists (Cont.)
 Collection structure has a pointer to the list head
 Initially NULL
 Add first item The variable (or handle)
which represents the list
 Allocate space for node is simply a pointer to the
 Set its data pointer to object node at the head of the
list.
 Set Next to NULL
 Set Head to point to new node
Collection
node
Head
Data Next Tail

object

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Linked Lists (Cont.)
 Add a node
 Allocate space for node
 Set its data pointer to object
 Set Next to current Head
 Set Head to point to new node

Collection

Head

node node
Data Next Data Next

object2 object
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0
Linked Lists - Add implementation

 Implementation
struct t_node {
void *item;
Recursive type definition -
struct t_node *next;
C allows it!
} node;
typedef struct t_node *Node;
struct collection {
Node head;
……
};
int AddToCollection( Collection c, void *item ) {
Node new = malloc( sizeof( struct t_node ) );
new->item = item;
new->next = c->head;
c->head = new;
Error checking, asserts
return TRUE;
omitted for clarity!
}
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0
Linked Lists - Find implementation
 Implementation
void *FindinCollection( Collection c, void *key ) {
Node n = c->head;
while ( n != NULL ) {
if ( KeyCmp( ItemKey( n->item ), key ) == 0 )
{
return n->item;
n = n->next;
}
return NULL;
}

Add time Constant - independent of n


Search time Worst case - n

• A recursive implementation is also possible!

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Linked Lists - Delete implementation
 Implementation
void *DeleteFromCollection( Collection c, void *key ) {
Node n, prev;
n = prev = c->head;
while ( n != NULL ) {
if ( KeyCmp( ItemKey( n->item ), key ) == 0 ) {
prev->next = n->next;
return n;
}
prev = n;
n = n->next;
}
return NULL; head
}

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Linked Lists - Variations
 Simplest implementation By ensuring that the tail of the list is
 Add to head always pointing to the head, we can
build a circularly linked list
 Last-In-First-Out (LIFO)
semantics head is tail->next
LIFO or FIFO using ONE pointer
 Modifications
 First-In-First-Out (FIFO)
 Keep a tail pointer head

struct t_node { tail


void *item;
struct t_node *next;
} node;
typedef struct t_node *Node;
struct collection {
Node head, tail;
};
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0
Linked Lists - Doubly linked
 Doubly linked lists
Can be scanned in both Applications requiring
directions both way search
struct t_node { Eg. Name search in
void *item; telephone directory
struct t_node *prev,
*next;
} node;

typedef struct t_node *Node;


struct collection {
Node head, tail;
};
head prev prev prev

tail

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Binary Tree
 The simplest form of Tree is a Binary Tree
Note the
 Binary Tree Consists of recursive
 Node (called the ROOT node) definition!
 Left and Right sub-trees
 Both sub-trees are binary trees
 The nodes at the lowest levels of the tree (the ones with no sub-trees) are called leaves

In an ordered binary tree


the keys of all the nodes in
• the left sub-tree are less
than that of the root
• the keys of all the nodes
in the right sub-tree are
greater than that of the
root,
• the left and right sub-
trees are themselves
Each sub-tree ordered binary trees.
is itself
a binary tree
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0
Binary Tree (Cont.)
• If A is the root of a binary tree and B is the root
of its left/right subtree then A
o A is the father of B
o B is the left/right son of A B C
• Two nodes are brothers if they are left and right
sons of the same father D
E
• Node n1 is an ancestor of n2 (and n2 is descendant F G
of n1) if n1 is either the father of n2 or the father of
some ancestor of n2
• Strictly Binary Tree: If every nonleaf node in a binary tree has non empty left
and right subtrees
• Level of a node: Root has level 0. Level of any node is one more than the level
of its father
• Depth: Maximum level of any leaf in the tree
l
A binary tree can contain at most 2 nodes at level l
d+1
Total nodes for a binary tree with depth d = 2 -1

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Binary Tree - Implementation

struct t_node {
void *item;
struct t_node *left;
struct t_node *right;
};

typedef struct t_node *Node;

struct t_collection {
Node root;
……
};

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Binary Tree - Implementation
 Find

extern int KeyCmp( void *a, void *b );


/* Returns -1, 0, 1 for a < b, a == b, a > b */

void *FindInTree( Node t, void *key ) { Less,


if ( t == (Node)0 ) return NULL; search
switch( KeyCmp( key, ItemKey(t->item) ) ) { left
case -1 : return FindInTree( t->left, key );
case 0: return t->item;
case +1 : return FindInTree( t->right, key );
}
} Greater,
search right
void *FindInCollection( collection c, void *key ) {
return FindInTree( c->root, key );
}
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0
Binary Tree - Performance
 Find
Complete Tree

Height, h
 Nodes traversed in a path from the root to a leaf
Number of nodes, h
 n = 1 + 21 + 22 + … + 2h = 2h+1 - 1
 h = floor( log2 n )
Since we need at most h+1 comparisons,
find in O(h+1) or O(log n)
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0
Binary Tree - Traversing
Traverse: Pass through the tree, enumerating each
node once
 PreOrder (also known as depth-first order)
1. Visit the root
2. Traverse the left subtree in preorder
3.Traverse the right subtree in preorder
 InOrder (also known as symmetric order)
1. Traverse the left subtree in inorder
2. Visit the root
3. Traverse te right subtree in inorder
 PostOrder (also known as symmetric order)
1. Traverse the left subtree in postorder
2. Traverse the right subtree in postorder
3. Visit the root

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Binary Tree - Applications
 A binary tree is a useful data structure when two-way
decisions must be made at each point in a process
 Example: Finding duplicates in a list of numbers
 A binary tree can be used for representing an expression

containing operands (leaf) and operators (nonleaf node).


Traversal of the tree will result in infix, prefix or postfix forms
of expression

Two binary trees are MIRROR SIMILAR if they are both empty
or if they are nonempty, the left subtree of each is mirror
similar to the right subtree

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


General Tree
 A tree is a finite nonempty set of elements in
which one element is called the ROOT and
remaining element partitioned into m >=0 disjoint
subsets, each of which is itself a tree
 Different types of trees – binary tree, n-ary tree,
red-black tree, AVL tree

A Hierarchical Tree

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Heaps
Heaps are based on the notion of a complete tree
A binary tree is completely full if it is of height, h, and has 2h+1-1 nodes.
 A binary tree of height, h, is complete iff
 it is empty or
 its left subtree is complete of height h-1 and its right subtree is
completely full of height h-2 or
 its left subtree is completely full of height h-1 and its right subtree is
complete of height h-1.
 A complete tree is filled from the left:
 all the leaves are on
 the same level or two adjacent ones and
 all nodes at the lowest level are as far to the left as possible.
 A binary tree has the heap property iff
 it is empty or
 the key in the root is larger than that in either child and both subtrees
have the heap property.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0
Heaps (Cont.)
 A heap can be used as a priority queue:
 the highest priority item is at the root and is trivially
extracted. But if the root is deleted, we are left with
two sub-trees and we must efficiently re-create a
single tree with the heap property.
 The value of the heap structure is that we can both
extract the highest priority item and insert a new
one in O(logn) time.
Example:
A deletion will remove the
T at the root

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Heaps (Cont.)
To work out how we're going to maintain
the heap property, use the fact that a
complete tree is filled from the left. So
that the position which must become
empty is the one occupied by the M. Put
it in the vacant root position.
This has violated the condition that the
root must be greater than each of its
children. So interchange the M with the
larger of its children.
The left subtree has now lost the heap
property. So again interchange the M
with the larger of its children.
We need to make at most h interchanges
of a root of a subtree with one of its
children to fully restore the heap
property.
O(h) or O(log n)

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Heaps (Cont.)

Addition to a Heap
To add an item to a heap, we follow the reverse procedure.
Place it in the next leaf position and move it up.
Again, we require O(h) or O(logn) exchanges.

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Data Structure Comparisons
Arrays Linked List Trees
Simple, fast Simple Still Simple
Inflexible Flexible Flexible
Add O(1) O(1) O(log n)
O(n) inc sort sort -> no adv
Delete O(n) O(1) - any O(log n)
O(n) - specific
Find O(n) O(n) O(log n)
O(logn) (no bin search)
binary search

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Queues
Queues are dynamic collections which have some concept of
order
 FIFO queue
 A queue in which the first item added is always the first one
out.
 LIFO queue
 A queue in which the item most recently added is always the
first one out.
 Priority queue
 A queue in which the items are sorted so that the highest
priority item is always the next one to be extracted.

Queues can be implemented by Linked Lists

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Stacks
 Stacks are a special form of collection with LIFO semantics
 Two methods
 int push( Stack s, void *item );

- add item to the top of the stack


 void *pop( Stack s );

- remove most recently pushed item from the top of the stack
 Like a plate stacker
 Other methods
int IsEmpty( Stack s );
Determines whether the stack has anything in it

void *Top( Stack s );


Return the item at the top without deleting it

* Stacks are implemented by Arrays or Linked List


© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0
Stacks (Cont.)
 Stack very useful for Recursions
 Key to call / return in functions
& procedures
function f( int x, int y) {
int a;
if ( term_cond ) return …;
a = ….;
return g( a );
}

function g( int z ) {
int p, q;
p = …. ; q = …. ;
return f(p,q);
} Context
for execution of f

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Sorting Algorithms
A file is said to be SORTED on the key if i < j implies that k[i]
preceeds k[j] in some ordering of the keys
Different types of Sorting
• Exchange Sorts
 Bubble Sort
 Quick Sort
• Insertion Sorts
• Selection Sorts
 Heap Sort
 Binary Tree Sort
• Merge and Radix Sorts

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Insertion Sort
First card is already sorted
With all the rest,
Scan back from the end until you find the first card
larger than the new one O(n)
Move all the lower ones up one slot O(n)
insert it O(1)
For n cards Complexity O(n2)

A K 10 5 J 4 2 Q 9

9

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Bubble Sort
Bubble Sort
 From the first element
Exchange pairs if they’re out of order
Repeat from the first to n-1
Stop when you have only one element to check
/* Bubble sort for integers */
#define SWAP(a,b) { int t; t=a; a=b; b=t; }

void bubble( int a[], int n ) { Outer loop n iterations


int i, j;
for(i=0;i<n;i++) { /* n passes thru the array */
Inner loop
/* From start to the end of unsorted part */
n-1, n-2, n-3, … , 1 iterations
for(j=1;j<(n-i);j++) {
/* If adjacent items out of order, swap */
if( a[j-1]>a[j] ) SWAP(a[j-1],a[j]); O(1) statement
}
}
}
Overall O(n2)
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0
Selection Sort
 Algorithm:
 Pass through elements sequentially;
 In the ith pass, we select the element with the lowest value
in A[i] through A[n], then swap the lowest value with A[i].
 Time complexity: O(n2)
 Example: Sort the list {25, 57, 48, 37, 12}

40
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0
Quick Sort 1/2
 Quick sort, also known as partition sort, sorts by employing a
divide-and-conquer strategy.
 Algorithm:
 Pick an pivot element from the input;
 Partition all other input elements such that elements less than the
pivot come before the pivot and those greater than the pivot come
after it (equal values can go either way);
 Recursively sort the list of elements before the pivot and the list of
elements after the pivot.
 The recursion terminates when a list contains zero or one element.
 Time complexity: O(nlogn) or O(n2)
 Demo: http://pages.stern.nyu.edu/~panos/java/Quicksort/
 Example: Sort the list {25, 57, 48, 37, 12}

41
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0
Quick Sort 2/2
 Example of Divide and Conquer algorithm
 Two phases
 Partition phase
 Divides the work into half

< pivot pivot > pivot

 Sort phase
 Conquers the halves!
< pivot > pivot

< p’ p’ > p’ pivot < p” p” > p”

quicksort( void *a, int low, int high ) {


int pivot;
if ( high > low ) /* Termination condition! */ {
pivot = partition( a, low, high );
quicksort( a, low, pivot-1 );
quicksort( a, pivot+1, high );
}
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0
Heap Sort
Heaps also provide a means of sorting:
 construct a heap,

 add each item to it (maintaining the heap


property!),
 when all items have been added, remove them
one by one (restoring the heap property as
each one is removed).
 Addition and deletion are both O(logn)
operations. We need to perform n additions
and deletions, leading to an O(nlogn) algorithm
 Generally slower

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Comparisons of Sorting
Insertion O(n2) Guaranteed
Bubble O(n2) Guaranteed
Heap O(n log n) Guaranteed
Quick O(n log n) Most of the time!
O(n2)
Bin O(n) Keys in small range
O(n+m)
Radix O(n) Bounded
keys/duplicates O(nlog n)

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Searching Algorithms
 Fundamental operation
 Finding an element in a (huge) set of other elements
 Each element in the set has a key
 Searching is the looking for an element with a given key
 distinct elements may have (share) the same key
 how to handle this situation?
 first, last, any, listed, ...
 May use a specialized data structure
 Things to consider
 the average time
 the worst-case time and
 the best possible time.

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Sequential Search

 Store elements in an array


 Unordered

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Sequential Search Analysis

 Generic simple algorithm


 Space complexity: O(1)

 Time complexity

 Time is proportional to n
 We call this time complexity O(n)
 Worst case: N + 1 comparisons
 Best case: 1 comparison
 Average case (successfull): (1+2+...+N)/N = (N+1)/2

 Both arrays (unsorted) and linked lists

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Sequential Search in a
(sorted) Linked List 1/2

 Keep the list sorted


 Easy to implement with linked list (exercice: do it)!

// return first node with key 'k' in 'l';


// return 'NULL' if not found
// 'l' is sorted
node find(list l, int k) {
node z = list_end(l);
node_setKey(z, k); // sentinel
for (node n = list_start(l);
node_getKey(n) > k;
n = node_next(n));
if (node_getKey(n) != k) return NULL;
return n;
}
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0
Sequential Search in a
(sorted) Linked List 2/2

 Space complexity: O(1)


 Time complexity
Best case: 1 comparison
Average case (successfull): same as the sequential
search in unordered list (array): (N+1)/2
Worst case (unsuccessfull):
 consider the sentinel as part of the list
 then a search is always “successfull” (finding the
sentinel at least)
 Hence: (N+2)/2

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Sequential Search Improvements

 Static caching
Use the relative access frequency of elements
 store the most often accessed elements at the first
places
 Dynamic caching
For each access, move the element to the first
position
 Needs a linked list data structure to be efficient
 Very difficult to analyze the complexity in
theory: very efficient in practice
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0
Binary Search
 Sorted array on a key
 first compare the key with the item
in the middle position of the array
 If there's a match, we can return
immediately.
 If the key is less than the middle
key, then the item sought must lie
in the lower half of the array
 if it's greater then the item sought
must lie in the upper half of the
array
 Repeat the procedure on the lower
(or upper) half of the array -
RECURSIVE

Time complexity O(log n)

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Binary Search Implementation
static void *bin_search( collection c, int low, int high, void *key ) {
int mid;
if (low > high) return NULL; /* Termination check */
mid = (high+low)/2;
switch (memcmp(ItemKey(c->items[mid]),key,c->size)) {
case 0: return c->items[mid]; /* Match, return item found */
case -1: return bin_search( c, low, mid-1, key); /* search lower half */
case 1: return bin_search( c, mid+1, high, key ); /* search upper half */
default : return NULL;
}
}

void *FindInCollection( collection c, void *key ) {


/* Find an item in a collection
Pre-condition:
c is a collection created by ConsCollection
c is sorted in ascending order of the key
key != NULL
Post-condition: returns an item identified by key if one exists, otherwise returns NULL */

int low, high;


low = 0; high = c->item_cnt-1;
return bin_search( c, low, high, key );
}
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0
Binary Search vs Sequential Search
 Find method Logs
 Sequential search Base 2 is by far
 Worst case time: c1 n the most common
 Binary search in this course.
Assume base 2
 Worst case time: c2 log2n
unless otherwise
Compare n with logn noted!
60

50
Small
problems - 40
n
we’re not Large 4 log n
Time

30
interested! problemsn -
20 we’re
log2n interested
10
Binary search in this gap!
More complex 0
Higher constant factor 0 10 20 30 40 50 60
n

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0


Q&A

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 09e-BM/DT/FSOFT v1/0

You might also like