Chapter4_Data_Structures_and_Algorithms
Chapter4_Data_Structures_and_Algorithms
© HĐC 2024.2
Content
Queue: a data set with elements sorted in sequence, elements can be pushed
into one end of the queue and extracted from the other end of the queue, i.e.
first-in first-out list (FIFO)
front rear
dequeue enqueue
Ring buffer (circular buffer): similar to queue that uses a single, fixed-
size buffer as if it were connected end-to-end. If it is full, it will replace the first
element with the new one. R F
6 7 8 9 A B 5
top top
Tree: a data set contains elements linked to each other and can be
accessed in sequence from the root
A tree of which each node has at most two children (two branches) is called
binary tree
Root A
B C
D E F G H
I J K
© HDC 2024.2 Chapter 4: Data Structures & Algorithms 8
Common data structures (cont.)
Set: data structure which stores a collection of distinct elements
Carolyne
Alice
Dave Farah
Bob
Emily
Map: a data set with sorted elements which can be accessed fast
by using “key”
Hash table: data structure with elements sorted with integer codes
generated by a special function
key_1 Index Value
0 value_1
Hash
key_2 1 value_2
Function
2 value_3
key_3 3 value_4
Stack
Temporary memory for local variables Operating system
(automatic extent) Other programs
Data are in sequence based on the rule FILO
Data is pushed in when a function is called Code (Text)
and it is freed (pop out) when exiting the
function Global variables
C:
malloc(): input argument is the number of byte, return a
non-type pointer (void*) which contain the address of
memory block allocated (in heap), return 0 if failed
free(): input argument is a non-type pointer (void*), free
memory which has the address as provided
C++:
Operator new[] allocates space for array with data type and
number of elements as defined in heap, return a pointer with
data type, or return 0 if failed
Operator delete[] deallocate the space and its input
argument is a pointer
Operator new[] and delete[] are applicable to allocate and
deallocate space of variables, objects besides arrays
char *s1;
s1 = string_duplicate("this is a string");
...
free(s1);
Method
Each memory block:
newsize = K * oldsize;
Usually, we choose K = 2
Demonstration of realloc() usage
int main () {
std::vector<int> myvector;
for (int i=1; i<=5; i++) myvector.push_back(i);
void main() {
int n;
cout << "Enter the number of your national holidays:";
cin >> n;
Date* date_list;
createDateList(n, date_list);
for (int i=0; i < n; ++i) {
...
}
for (....) { cout<< ....}
delete [] date_list;
}
Efficiency
Memory is allocated as much as it is required and when it is
required while the program is running
Memory is allocated within the free space of the computer
(heap), it depends on only computer memory
Allocated memory can be freed once it is not used anymore
Flexibility
Lifetime of the dynamically allocated memory may be longer
than the lifetime of the object allocated it
It is possible to call a function to allocated memory and another
function to deallocate it
The flexibility may cause memory leak
pHead
Item A Data A
Item B Data B
Item C Data C
Item X Data X
pHead pHead
Data A Data B
Data B Data T
Data C Data C
Data X Data X
pHead pHead
Data A Data A
Data B Data B
Data C Data C
Data X Data X
Advantages:
Flexible usage, allocating memory when needed and
deallocating after using
Add/delete element via pointer; time taken to perform these
task is constant, doesn’t depend on data length or position
Access data in sequence
Disadvantages:
Added element must be allocated dynamic memory
Deleting element requires respected memory space to be freed
If data type is not large, the overhead may be dominant
Searching data is based on linear methods which consume more
time
#include <string>
using namespace std;
struct MessageItem {
string subject;
string content;
MessageItem* pNext;
};
struct MessageList {
MessageItem* pHead;
};
void initMessageList(MessageList& l);
void addMessage(MessageList&, const string& sj,
const string& ct);
bool removeMessageBySubject(MessageList&l,
const string& sj);
void removeAllMessages(MessageList&);
Advantages:
A DLL can be traversed in both forward and backward direction
The delete operation in DLL is more efficient
We can quickly insert a new node before a given node
Example:
typedef struct list_t List;
struct list_t {
int item;
List *next;
};
Main reasons to use typedef
Simplify the complex name
Example of function pointer
typedef int (*PFI)(char *, char *);
PFI pfarray[10];
Self-defined data type make the program easier to read, e.g.:
Length is more intelligible than int).
Basic Operations
Initializing, using it and then de-initializing the stack
Two primary operations:
push() − Pushing (storing) an element on the stack
pop() − Removing (accessing) an element from the stack
push pop
top top
F R
1 2
F R
1 2 3
F R
2 3
F R
3
F R
3 4 5 …
R F
6 7 8 9 3 4 5
R F
6 7 8 9 A 4 5
R F
6 7 8 9 A B 5
struct Dictionary_t {
/* table is an array of pointers to entries */
struct Nlist *table[HASHSIZE];
};
return NULL;
}
Why algorithm?
Efficiency: Algorithms can perform tasks quickly and
accurately.
Consistency: Algorithms are repeatable and produce consistent
results every time they are executed.
Scalability: Algorithms can be scaled up to handle large
datasets or complex problems.
Automation: Algorithms can automate repetitive tasks, reduce
the need for human intervention and freeing up time for other
tasks.
Standardization: Algorithms can be standardized and shared
among different teams or organizations.
Input data
0 n-1
a Unsorted
Desired output:
Data has been sorted in certain order
0 n-1
a Sorted array: a[0]<=a[1]<=…<=a[n-1]
Initial state
0 n-1
The smallest numbers The remaining data,
a which have been sorted unsorted
Steps:
Find the smallest number in a[k..n-1]
Swap a[k] and the smallest number we found above
0 n-1
The smallest numbers
a which have been sorted
a[k] x
Basic idea:
Starting with sorted arrays: pointers are at the beginning of the
arrays
Use the pointer to mix the position of each pair of elements
within sorted arrays to make a bigger array with sorted
elements
After merging 2 arrays, we get the desired array eventually
The basic operator is merging
a 3 12 -5 6 142 21 -17 45
typedef struct {
char name[MAX_NAME + 1];
int id;
double score;
} StudentRecord;
typedef struct {
char name[MAX_NAME + 1];
int id;
double score;
} StudentRecord;
a 3 12 -5 6 142 21 -17 45
a 3 12 -5 6 142 21 -17 45
a -5 3 12 6 142 21 -17 45
a -5 3 6 12 142 21 -17 45
a -5 3 6 12 142 21 -17 45
a -5 3 6 12 21 142 -17 45
a -17 -5 3 6 12 21 142 45
a -17 -5 3 6 12 21 45 142
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
7 20 10 30 7 20 10 7 10 20
pivot_index = 0 2 4 10 12 13 50 57 63 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
k 2
i=1 2 -3 -8 5 11
k -3
i=2 2 -3 -8 5 11
k -8
return i i=3 2 -3 -8 5 11
k 5
// Function call
int result = search(arr, N, x);
if (result == -1) {
printf("Element is not present in array");
}
else {
printf("Element is present at index %d", result);
}
return 0;
}
Steps:
Divide the search space into two halves by finding the middle
index “mid”
Compare the middle element of the search space with the key
If the key is found at middle element, the process is terminated
If the key is not found at middle element, choose which half
will be used as the next search space
o If the key is smaller than the middle element, then the left side is used
for next search
o If the key is larger than the middle element, then the right side is used
for next search
This process is continued until the key is found or the total
search space is exhausted
low high
mid
© HDC 2024.2 Chapter 4: Data Structures & Algorithms 137
4.6.2. Binary Search
Implementation:
Iterative Binary Search Algorithm
Recursive Binary Search Algorithm
Complexity
Time complexity:
o Worst case complexity: log
o Best case complexity: Ω1
o Average case complexity: Θ(log )
Space complexity: 3, i.e., three extra variables for indices
Applications:
Be a part of complex algorithms used in machine learning
Searching in computer graphics
Searching in database
return 0;
}
Homework:
Implementation of Binary search algorithm with Recursive
approach