0% found this document useful (0 votes)
31 views

Chapter4 Data Structures and Algorithms

Uploaded by

Đức Trần
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Chapter4 Data Structures and Algorithms

Uploaded by

Đức Trần
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 134

EE3490E - Fundamentals of

Embedded Programmings
Chapter 4: Data
Structures & Algorithms
Lecturer: Dr. Hoang Duc Chinh (Hoàng Đức Chính)
Department of Industrial Automation
School of Electrical Engineering
Email: [email protected] ©HDC 2022.1
Content
4.1. Introduction of data structures
4.2. Arrays and dynamic memory management
4.3. Vector structure
4.4. List structure
4.5. Sorting algorithms

Chapter 4: Data Structures & Algorithms © HDC 2022.1 2


4.1 Introduction
 Many real-life applications require complex data structures,
basic data type may not be sufficient to represent
 Examples:
 Student data: name, birthday, hometown, student identification (ID),
etc.
 Transfer function: numerator, denominator polynomial
 State space model: A, B, C, C matrices
 Process (sensing) data: parameter name, measurement range, value,
unit, timestamp, accuracy, threshold, etc.
 Graphic object: size, color, line weight, font, etc.
 Data structure representation method: define a new data
type using structure (struct, class, union, etc.)
Chapter 4: Data Structures & Algorithms © HDC 2022.1 3
Problem: Represent a set of data
 Most of the data belonging to an application are related to
each other it needs to represent a set with structure, e.g.:
 Student list: data are in alphabet order
 Generic model for a control system: include multiple components
interacting with each other
 Process (sensing) data: a data set which carries value of an input at
discrete time, input values are related to output values
 Graphic object: a window includes some graphic objects, a schematic
also include a number of graphic objects
 In general, data in the same set has the same type, or at least
compatible types
 Array is not always suitable
Chapter 4: Data Structures & Algorithms © HDC 2022.1 4
Problem: Manage (a set of) data
 Any data set can be represented by properly utilizing
data structure and array
 Algorithms (functions) process the data, in order to
manage it efficiently:
 Add a new data record into a list, a table, a set, etc.
 Delete a record from a list, a table, a set, etc.
 Search a record in a list, a table, a set, etc. based on a criteria
 Sort a list to meet a criteria
…

Chapter 4: Data Structures & Algorithms © HDC 2022.1 5


How to manage data efficiently
 Minimize memory usage: “overhead” information is
insignificant when being compared with main data
 Fast and convenient access: time taken to add, search or
delete data record should be short
 Flexibility: number of data records should not be
limited, does not need to be known in advance, suitable
for small and large scale problem
 Efficiency of data management depends on
 Data structure
 Algorithms used to add, search, sort, delete data

Chapter 4: Data Structures & Algorithms © HDC 2022.1 6


Common data structures
 Array (extended meaning): set of data which can be
accessed by index
 List: a data set contains elements each one links to another
and the set can be accessed in sequence
 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
 Map: a data set with sorted elements which can be accessed
fast by using “key”
 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)
Chapter 4: Data Structures & Algorithms © HDC 2022.1 7
Common data structures (cont.)
 Set: data structure with elements sorted in a certain manner
but can be accessed effectively
 Stack: data structure with elements sorted in sequence but
can be accessed from one end, i.e. last-in first-out (LIFO) or
first-in last-out (FILO)
 Hash table: data structure with elements sorted with integer
codes generated by a special function
 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.
 In mathematical calculation and control system: vector,
matrix, polynomial, rational fraction, transfer function, etc.
Chapter 4: Data Structures & Algorithms © HDC 2022.1 8
4.2 Array and dynamic memory
allocation
 Data can be represented by using array efficiently:
 Read and write data quickly via index or address
 Saving memory
 Fixed size array:
char buffer[SIZE];
Student student_list[100];
 Size of array must be known before compiling, users cannot
enter the number of elements, in another way, this number
cannot be a variable less flexible
 Occupy a fixed slot in stack (if it is a local variable) or data
segment (if it is a global variable) inefficient and inflexible
use of memory

Chapter 4: Data Structures & Algorithms © HDC 2022.1 9


Dynamic array
 Dynamic array is allocated in the memory as required, during the
run-time
#include <stdlib.h>/* C */
int n = 50;
...
float* p1= (float*) malloc(n*sizeof(float)); /* C */
double* p2= new double[n];// C++
 A pointer is used to handle a dynamic array, the usage is similar
to a fixed array
p1[0] = 1.0f;
p2[0] = 2.0;
 Once it is no longer in use free the allocated memory
free(p1); /* C */
delete [] p2;// C++
Chapter 4: Data Structures & Algorithms © HDC 2022.1 10
Memory allocation revision
 4 memory segments: code segment, data segment,
stack, heap
 Code segment (sometime called text segment)
 Stores constants and constant types which have been defined
 Read-only
 Handled by the compiler
 Data segment
 Used when the program is running
 Contains global and static variables (initialized or uninitialized)
 Readable and writable
 Handled by the compiler

Chapter 4: Data Structures & Algorithms © HDC 2022.1 11


Memory allocation revision (cont.)
 Stack
 Temporary memory for local variables (automatic extent)
 Data are in sequence based on the rule FILO
 Data is pushed in when a function is called and it is freed (pop
out) when exiting the function
 Handled by the compiler
 Heap (Free-store)
 Dynamic memory allocation
 Handled by developers (not the compiler)
 Managed by standard functions (malloc(), calloc() or
realloc(), free()) in C or operators (new, delete) in
C++

Chapter 4: Data Structures & Algorithms © HDC 2022.1 12


Risks
 Developer takes all the responsibility in managing the
dynamic memory
 No support from the compiler in handling memory
 May cause real-time issues
 Storing data in stack is much faster than that in heap
 Stack is managed by handling pointer, while using heap
requires internal memory management
 It is better to use stack if possible

Chapter 4: Data Structures & Algorithms © HDC 2022.1 13


Allocating and free dynamic memory
 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
Chapter 4: Data Structures & Algorithms © HDC 2022.1 14
Dynamic memory handling
 Functions which handle dynamic memory are provided
by standard library, these are the only method to access
the heap
 Allocating memory
void *malloc(size_t size)
int *p = malloc(10 * sizeof(int));
int *p = (int *) malloc(10 * sizeof(int));

Chapter 4: Data Structures & Algorithms © HDC 2022.1 15


malloc()
 If malloc() is called successfully, it returns a
pointer which points to the address of the allocated
memory
 Other wise it returns NULL
 In fact, computers have virtual memory which can be
considered unlimited
 It is a good habit to verify the returning value of this
function

Chapter 4: Data Structures & Algorithms © HDC 2022.1 16


free()
 free() release the memory segment allocated by
malloc()
 The memory is freed and ready for subsequent usage
void free(void *p)
 Don’t need type casting (cast) xxx * to void *
int *p = (int *) malloc(10 * sizeof(int));
...
free(p);

Chapter 4: Data Structures & Algorithms © HDC 2022.1 17


calloc()
 calloc() is similar to malloc() but the memory is
initialized with zero value (clear allocate)
 Its interface is a bit different from malloc()
void *calloc(size_t n, size_t size)
int *p = calloc(10, sizeof(int));

Chapter 4: Data Structures & Algorithms © HDC 2022.1 18


realloc()
 realloc() is used to change the memory allocated
by malloc(), calloc(), or realloc()
void *realloc(void *p, size_t size)
 Multiple features
 If p is NULL, it is the same as malloc()
 If it is not done successfully, it returns NULL, the memory is
reserved
 If size = 0, it is the same as free(), and returns NULL

Chapter 4: Data Structures & Algorithms © HDC 2022.1 19


More on realloc()
 realloc() is used to increase or decrease dynamic
memory
 If it increases, the existing elements are unchanged and
the newly added elements have no initial values
 If it decreases, the existing elements are the unchanged
 However, if the memory space is not sufficient,
realloc() will allocate new memory block and copy the
whole old memory block to the new one, then delete the
old one
 This action disables the pointer which point to the old memory
block

Chapter 4: Data Structures & Algorithms © HDC 2022.1 20


Dynamic memory management
 Handled by the developer
 It includes
 Pointer which points to the memory block
 Allocating and free memory block
 Size of the memory block
 Errors may occur in most of the C applications.

Chapter 4: Data Structures & Algorithms © HDC 2022.1 21


Example of wrong usage
char *string_duplicate(char *s)
/* Dynamically allocate a copy of a
string. User must remember to free this
memory. */
{
/* +1 for ’\0’ */
char *p = malloc(strlen(s)+1);
return strcpy(p, s);
}

char *s1;
s1 = string_duplicate("this is a string");
...
free(s1);
Chapter 4: Data Structures & Algorithms © HDC 2022.1 22
Common errors
 The pointer points to an undefined value
 “memory corruption”
 The pointer points to NULL
 Program halts
 Free a pointer pointing to a memory block which is not
dynamic memory like stack, constant data
 Not free memory after using (memory leak).
 Access elements which are not in the range of the
allocated array

Chapter 4: Data Structures & Algorithms © HDC 2022.1 23


Good habbits
 Each malloc() should be followed by free()
 Avoid memory corruption memory leak
 Always use malloc() and free() within a function
 Always build create() function and then destroy() with
complex objects
 Pointer must be declared when starting
 Initialized as NULL or an existing value
 NULL means “point to nowhere”
 Pointer should be set as NULL after being freed
 free(NULL) has no meaning (no effect)

Chapter 4: Data Structures & Algorithms © HDC 2022.1 24


Example: Matrix
 A matrix with fixed size (3x3), size of matrix can be
defined at compiler time
 The following example shows a matrix created as
required by user
double **matrix = create_matrix(2,3);
matrix[0][2] = 5.4;
...
destroy_matrix(matrix);

Chapter 4: Data Structures & Algorithms © HDC 2022.1 25


Example: Matrix (cont.)
double **create_matrix1(int m, int n) {
double **p;
int i;
/* Allocate pointer array. */
p = (double **) malloc(m * sizeof(double*));
/* Allocate rows. */
for (i = 0; i < m; ++i)
p[i] = (double *) malloc(n * sizeof(double));
return p;
}

Chapter 4: Data Structures & Algorithms © HDC 2022.1 26


Example: Matrix (cont.)
double **create_matrix2(int m, int n) {
double **p; int i;
assert(m>0 && n>0);
p = (double **) malloc(m * sizeof(double*));
if (p == NULL)
return p;
for (i = 0; i < m; ++i) { /* Allocate rows. */
p[i] = (double *) malloc(n * sizeof(double));
if (p[i] == NULL)
goto failed; /* Allocation failed */
}
return p;
failed:
for (−−i; i >= 0; −−i)
free(p[i]); /* delete allocated memory.*/
free(p);
return NULL;
}
Chapter 4: Data Structures & Algorithms © HDC 2022.1 27
Example: Matrix (cont.)
/* Destroy an (m x n) matrix. Notice, the n
variable is not used, it is just there to
assist using the function. */
void destroy_matrix1(double **p, int m, int n) {
int i;
assert(m>0 && n>0);
for (i = 0; i < m; ++i)
free(p[i]);
free(p);
}

Chapter 4: Data Structures & Algorithms © HDC 2022.1 28


Example: Matrix (cont.)
double **create_matrix(int m, int n) {
double **p, *q;
int i;
assert(m>0 && n>0);
p = (double **) malloc(m * sizeof(double*));
if (p == NULL)
return p;
q = (double *) malloc(m * n * sizeof(double));
if (q == NULL) {
free(p);
return NULL;
}
for (i = 0; i < m; ++i, q += n)
p[i] = q;
return p;
}
Chapter 4: Data Structures & Algorithms © HDC 2022.1 29
Example: Matrix (cont.)
void destroy_matrix(double **p)
/* Destroy a matrix. Notice, due to the
method by which this matrix was created,
the size of the matrix is not required.*/
{
free(p[0]);
free(p);
}

Chapter 4: Data Structures & Algorithms © HDC 2022.1 30


Extendable array
 Extend array to improve the disadvantage of fixed-size
array in C
 Size of the array is up to requirement. Elements are
added to the end of the array and indexes are updated
automatically
 Disadvantages:
 The array always occupies memory
 Adding new element results in copying all the old ones to new
positions
 Thus, a memory block should be allocated instead of
providing each element a memory slot

Chapter 4: Data Structures & Algorithms © HDC 2022.1 31


Extendable array
 Method
Each memory block:
newsize = K * oldsize;
 Usually, we choose K = 2
 Demonstration of realloc() usage

Chapter 4: Data Structures & Algorithms © HDC 2022.1 32


Example: Extendable array
/* Vector access operations. */
int push_back(int item);
int pop_back(void);
int* get_element(int index);
/* Manual resizing operations. */
int get_size(void);
int set_size(int size);
int get_capacity(void);
int set_capacity(int size);
Chapter 4: Data Structures & Algorithms © HDC 2022.1 33
Example: Extendable array (cont.)
#include "vector.h"
#include <stdlib.h>
#include <assert.h>

/* Private interface */
/* initial vector capacity */
static const int StartSize = 1;
/* geometric growth of vector capacity */
static const float GrowthRate = 1.5;

/* pointer to vector elements */


static int *data = NULL;
/* current size of vector */
static int vectorsize = 0;
/* current reserved memory for vector */
static int capacity = 0;

Chapter 4: Data Structures & Algorithms © HDC 2022.1 34


Example: Extendable array (cont.)
/* Add element to back of vector. Return index of new
element if successful, and -1 if fails. */
int push_back(int item) {
/* If out-of-space, allocate more. */
if (vectorsize == capacity) {
int newsize = (capacity == 0) ? StartSize :
(int)(capacity*GrowthRate + 1.0);
int *p = (int *)realloc(data, newsize*sizeof(int));
if (p == NULL)
return -1;
capacity = newsize; /* update data-structure */
data = p;
}
data[vectorsize] = item; /* We have enough room. */
return vectorsize++;
}
Chapter 4: Data Structures & Algorithms © HDC 2022.1 35
Example: Extendable array (cont.)
/* Return element from back of vector, and remove
it
from the vector. */
int pop_back(void) {
assert(vectorsize > 0);
return data[−−vectorsize];
}

/* Return pointer to the element at the specified


index. */
int* get_element(int index) {
assert(index >= 0 && index < vectorsize);
return data + index;
}

Chapter 4: Data Structures & Algorithms © HDC 2022.1 36


Example: Extendable array (cont.)
/* Manual size operations. */
int get_size(void) {return vectorsize;}
int get_capacity(void) {return capacity;}

/* Set vector size.


Return 0 if successful, -1 if fails. */
int set_size(int size) {
if (size > capacity) {
int *p = (int *) realloc(data, size*sizeof(int));
if (p == NULL)
return −1;
/* allocate succeeds, update data-structure */
capacity = size;
data = p;
}
vectorsize = size;
return 0;
}
Chapter 4: Data Structures & Algorithms © HDC 2022.1 37
Example: Extendable array (cont.)
/* Shrink or grow allocated memory reserve for array.
A size of 0 deletes the array. Return 0 if
successful, -1 if fails. */
int set_capacity(int size) {
if (size != capacity) {
int *p = (int *) realloc(data, size*sizeof(int));
if (p == NULL && size > 0)
return −1;
capacity = size;
data = p;
}
if (size < vectorsize)
vectorsize = size;
return 0;
}

Chapter 4: Data Structures & Algorithms © HDC 2022.1 38


Notes
 A pointer is used to handle dynamic array, it is not a dynamic array
 Allocate and deallocate memory space but not allocate and deallocate a
pointer
 Free memory space only once
int* p;
p[0] = 1;// never do it
new(p);// access violation!
p = new int[100];// OK
p[0] = 1;// OK
int* p2=p;// OK
delete[] p2;// OK
p[0] = 1;// access violation!
delete[] p;// very bad!
p = new int[50];// OK, new array
...

Chapter 4: Data Structures & Algorithms © HDC 2022.1 39


Allocate dynamic memory for a single
variable
 Purpose: Object can be created dynamically, while the program is
running, such as adding a new student to a list, drawing a new shape in
a schematic, inserting a component in a system, etc.
 Syntax:
int* p = new int;
*p = 1;
p[0]= 2;// the same as above
p[1]= 1;// access violation!
int* p2 = new int(1);// with initialization
delete p;
delete p2;
Student* ps= new Student;
ps->code = 1000;
...
delete ps;
 A single variable is not an array with one element
Chapter 4: Data Structures & Algorithms © HDC 2022.1 40
Example: using dynamic memory
Date* createDateList(int n) {
Date* p = new Date[n];
return p;
}
void main() {
int n;
cout << "Enter the number of your national holidays:";
cin >> n;
Date* date_list= createDateList(n);
for (int i=0; i < n; ++i) {
...
}
for (....) { cout << ....}
delete [] date_list;
}

Chapter 4: Data Structures & Algorithms © HDC 2022.1 41


Output argument is a pointer
void createDateList(int n, Date* &p) {
p = new Date[n];
}
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;
}

Chapter 4: Data Structures & Algorithms © HDC 2022.1 42


Summary on dynamic memory
 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

Chapter 4: Data Structures & Algorithms © HDC 2022.1 43


4.3 Vector structure
 Problem: how to represent a mathematical vector in C/C++
 Simple solution: normal dynamic array, but …
 Inconvenient use: a user must call allocation and deallocation
functions himself; number of array dimensions need to be included
as well
 Unsafe: a small mistake may cause a serious consequence
int n = 10;
double *v1,*v2, d;
v1 = (double*) malloc(n*sizeof(double));
v2 = (double*) malloc(n*sizeof(double));
d = scalarProd(v1,v2,n); // scalar_prod existed
d = v1 * v2;// OOPS!
v1.data[10] = 0;// OOPS!
free(v1);
free(v2);

Chapter 4: Data Structures & Algorithms © HDC 2022.1 44


Vector structure definition
 File name: vector.h
 Data structure:
struct Vector {
double *data;
int nelem;
};
 Declaration of its basic functions:
Vector createVector(int n, double init);
void destroyVector(Vector);
double getElem(Vector, int i);
void putElem(Vector, int i, double d);
Vector addVector(Vector, Vector);
Vector subVector(Vector, Vector);
double scalarProd(Vector, Vector);
...

Chapter 4: Data Structures & Algorithms © HDC 2022.1 45


Define basic functions
 File name: vector.cpp
#include <stdlib.h>
#include "vector.h"
Vector createVector(int n, double init) {
Vector v;
v.nelem= n;
v.data= (double*) malloc(n*sizeof(double));
while (n--) v.data[n] = init;
return v;
}
void destroyVector(Vector v) {
free(v.data);
}
double getElem(Vector v, int i) {
if (i < v.nelem&& i >= 0) return v.data[i];
return 0;
}

Chapter 4: Data Structures & Algorithms © HDC 2022.1 46


Define basic functions (cont.)
void putElem(Vector v, int i, double d) {
if (i >=0 && i < v.nelem) v.data[i] = d;
}
Vector addVector(Vector a, Vector b) {
Vector c = {0,0};
if (a.nelem== b.nelem) {
c = createVector(a.nelem,0.0);
for (inti=0; i < a.nelem; ++i)
c.data[i] = a.data[i] + b.data[i];
}
return c;
}
Vector subVector(Vector a, Vector b) {
Vector c = {0,0};
...
return c;
}

Chapter 4: Data Structures & Algorithms © HDC 2022.1 47


Usage
#include "vector.h"
void main() {
int n = 10;
Vector a, b, c;
a = createVector(10,1.0);
b = createVector(10,2.0);
c = addVector(a,b);
//...
destroyVector(a);
destroyVector(b);
destroyVector(c);
}
Chapter 4: Data Structures & Algorithms © HDC 2022.1 48
4.4 List structure
 Problem: create a structure to manage dynamic data
efficiently and flexibly, e.g.:
 Email
 Todo list
 Graphical objects in a figure
 Dynamic blocks in a simulation model (similar to SIMULINK)
 Requirements:
 The number of records/elements in the list changes frequently
 Add or delete data operations should be fast and simple
 Minimize memory usage

Chapter 4: Data Structures & Algorithms © HDC 2022.1 49


Using array?
 Number of elements in an array is actually fixed. Memory
space must be known when being allocated, it cannot be
extended or shrunk
 If memory space used is less than allocated one wasting
memory
 If memory space is full and more elements need to be
added, it is required to reallocate memory space and copy
the whole existing data to the new array time consuming
if the array size is large
 If an element which needs to be added/deleted is at the first
position or in the middle of the array, it is required to copy
and shift the rest of the data time consuming
Chapter 4: Data Structures & Algorithms © HDC 2022.1 50
4.4.1 Linked list
pHead

Item A Data A

Item B Data B

Item C Data C

Item X Data X

Item Y 0x00 Data Z

Chapter 4: Data Structures & Algorithms © HDC 2022.1 51


Linked list: Insert data
pHead pHead

pHead Data T Data A

Data A Data B

Data B Data T

Data C Data C

Data X Data X

0x00 Data Z 0x00 Data Z

At the beginning of the list In the middle of the list


Chapter 4: Data Structures & Algorithms © HDC 2022.1 52
Linked list: Delete data

pHead pHead

Data A Data A

Data B Data B

Data C Data C

Data X Data X

0x00 Data Z 0x00 Data Z

At the beginning of the list In the middle of the list


Chapter 4: Data Structures & Algorithms © HDC 2022.1 53
Summary
 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

Chapter 4: Data Structures & Algorithms © HDC 2022.1 54


Example: mail box
#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&);

Chapter 4: Data Structures & Algorithms © HDC 2022.1 55


Example: mail box (cont.)
#include "List.h"
void initMessageList(MessageList& l) {
l.pHead = 0;
}
void addMessage(MessageList& l, const string& sj,
conststring& ct) {
MessageItem* pItem = new MessageItem;
pItem->content = ct;
pItem->subject = sj;
pItem->pNext = l.pHead;
l.pHead = pItem;
}
void removeAllMessages(MessageList& l) {
MessageItem *pItem = l.pHead;
while (pItem != 0) {
MessageItem* pItemNext = pItem->pNext;
delete pItem;
pItem = pItemNext;
}
l.pHead = 0;
}

Chapter 4: Data Structures & Algorithms © HDC 2022.1 56


Example: mail box (cont.)
bool removeMessageBySubject(MessageList& l,
conststring& sj) {
MessageItem* pItem = l.pHead;
MessageItem* pItemBefore;
while (pItem != 0 && pItem->subject != sj) {
pItemBefore = pItem;
pItem = pItem->pNext;
}
if (pItem != 0) {
if (pItem == l.pHead)
l.pHead = 0;
else
pItemBefore->pNext = pItem->pNext;
delete pItem;
}
return pItem != 0;
}

Chapter 4: Data Structures & Algorithms © HDC 2022.1 57


Example: mail box usage (cont.)
#include <iostream>
#include "list.h"
using namespace std;
void main() {
MessageList myMailBox;
initMessageList(myMailBox);
addMessage(myMailBox,"Hi","Welcome, my friend!");
addMessage(myMailBox,"Test","Test my mailbox");
addMessage(myMailBox,"Lecture Notes","Programming Techniques");
removeMessageBySubject(myMailBox,"Test");
MessageItem* pItem = myMailBox.pHead;
while (pItem != 0) {
cout << pItem->subject << ":" << pItem->content << '\n';
pItem = pItem->pNext;
}
char c;
cin >> c;
removeAllMessages(myMailBox);
}

Chapter 4: Data Structures & Algorithms © HDC 2022.1 58


Homework
 Create a linked-list consisting of public holidays of a
year and description of each day (as string), so that
 A new public holiday can be added to the beginning of the list
 Search for the description of the day (input argument is a date
including day and month)
 Delete a public holiday at the beginning of the list
 Delete a public holiday in the middle of the list (input argument
is a date including day and month)
 Clear the whole list
 Write a program to demonstrate the usage of the above
list
Chapter 4: Data Structures & Algorithms © HDC 2022.1 59
4.4.2 More on lists
 Double Link List
 Stack
 Circular Buffer
 Hash table

Chapter 4: Data Structures & Algorithms © HDC 2022.1 60


Double Link List
 A Doubly Linked List (DLL) contains an extra pointer,
typically called previous pointer, together with next pointer
and data which are there in singly linked list.
struct List {
int item;
struct List *next;
struct List *prev;
};

 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

Chapter 4: Data Structures & Algorithms © HDC 2022.1 61


Double Link List example
struct List *insert_after(struct List *node, int item)
{
/* Allocate memory for new node. */
struct List *newnode = (struct List *)
malloc(sizeof(struct List));
if (newnode == NULL)
return NULL; /* allocation failed */
/* If list is not empty, splice new node into list. */
if (node) {
newnode−>next = node−>next;
node−>next = newnode;
}
else
newnode−>next = NULL;
newnode−>item = item;
return newnode;
}
Chapter 4: Data Structures & Algorithms © HDC 2022.1 62
typedef and Structure
 typedef is similar to #define but it is a C keyword
 It enables to create a new data type with a new name
typedef int Length;
Length len, maxlen;
Length lengths[50];
 typedef simplifies the use of structure in C (but not required
in C++)
typedef struct Point {
int x;
int y;
} Point;

Point pt1, pt2;

Chapter 4: Data Structures & Algorithms © HDC 2022.1 63


typedef and Link List
 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).
Chapter 4: Data Structures & Algorithms © HDC 2022.1 64
Usage of typedef
 typedef enable to hide the incompatible codes amongst
different microprocessors
 E.g.: in a 32 bit computer, we can write:
typedef short INT16;
typedef int INT32;
 In a 16 bit computer, they can be written as:
typedef int INT16;
typedef long INT32;

Chapter 4: Data Structures & Algorithms © HDC 2022.1 65


Usage of typedef
 typedef enables to write the program compatible
with different data types
typedef int ValueType;
typedef struct List {
ValueType item;
struct List *next;
} List;

List *insert_back(List *node, ValueType item);


List *insert_after(List *node, ValueType item);
 This is the most simple form of generic programming.

Chapter 4: Data Structures & Algorithms © HDC 2022.1 66


Stack

 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
Chapter 4: Data Structures & Algorithms © HDC 2022.1 67
Stack
typedef struct Stack {
double buffer[MAXSIZE]; /* Stack buffer. */
int count; /* Number of elements in stack. */
} Stack;

void push(Stack *s, double item) {


s->buffer[s->count++] = item;
}
double pop(Stack *s) {
return s->buffer[--s->count];
}
Chapter 4: Data Structures & Algorithms © HDC 2022.1 68
Circular Buffer
 An array with pre-defined size. It is also called “Ring
Buffer”
 It returns to the beginning position once it reaches the
last element
 It is also a queue but first element will be replaced by a
new one when the queue is full
 It is usually used in a real-time control system in which
many processes interacts with the object

Chapter 4: Data Structures & Algorithms © HDC 2022.1 69


Circular Buffer
1

1 2 3

6 7 8 9 3 4 5

6 7 8 9 A B 5

Chapter 4: Data Structures & Algorithms © HDC 2022.1 70


Circular Buffer

Chapter 4: Data Structures & Algorithms © HDC 2022.1 71


Software FIFO Implementation
(UART1 Receive)

Chapter 4: Data Structures & Algorithms © HDC 2022.1 72


Software FIFO Implementation
(UART1 Receive)

Chapter 4: Data Structures & Algorithms © HDC 2022.1 73


Software FIFO Implementation
(UART1 Receive)

Chapter 4: Data Structures & Algorithms © HDC 2022.1 74


Software FIFO Implementation
(UART1 Receive)

Chapter 4: Data Structures & Algorithms © HDC 2022.1 75


Circular Buffer
typedef struct CircBuf_t {
ValueType *array; /* Pointer to array of items */
int size; /* Maximum number of items in buffer */
int nelems; /* Current number of items in buffer */
int front; /* Index to front of buffer */
int back; /* Index to back of buffer */
} CircBuf;

Chapter 4: Data Structures & Algorithms © HDC 2022.1 76


Example
typedef double ValueType;
typedef struct CircBuf_t CircBuf;
/* create-destroy buffer */
CircBuf *create_buffer(int size);
void destroy_buffer(CircBuf *cb);
/* add-remove elements */
int add_item(CircBuf *cb, const ValueType *item);
int get_item(CircBuf *cb, ValueType *item);
/* query state */
int get_nitems(const CircBuf *cb);
int get_size(const CircBuf *cb);

Chapter 4: Data Structures & Algorithms © HDC 2022.1 77


Example
int add_item(CircBuf *cb, const ValueType *item) {
/* Add a new element to front of buffer.
Returns 0 for success, and -1 if buffer is full. */
if (cb−>nelems == cb−>size)
return −1;
cb−>array[cb−>front] = *item;
if (++cb−>front == cb−>size) /* wrap around */
cb−>front = 0;
++cb−>nelems;
return 0;
}

int get_item(CircBuf *cb, ValueType *item) {


/* Remove element from back of buffer, and assign it to *item.
Returns 0 for success, and -1 if buffer is empty. */
if (cb−>nelems == 0)
return −1;
−−cb−>nelems;
*item = cb−>array[cb−>back];
if (++cb−>back == cb−>size) /* wrap around */
cb−>back = 0;
return 0;
}

Chapter 4: Data Structures & Algorithms © HDC 2022.1 78


Hash Table
 Data is stored in an array format, where each data value
has its own unique index value
 Use for fast looking up if we know the index of desired
data
 The most common implementation is a data array
combined with link-list
 Basic Operations are Search, Insert, Delete

Chapter 4: Data Structures & Algorithms © HDC 2022.1 79


Example
typedef struct Dictionary_t Dictionary;
Dictionary *create_table(void);
void destroy_table(Dictionary *);
int add_word(Dictionary *, const char *key, const char
*defn);
char *find_word(const Dictionary *, const char *key);
void delete_word(Dictionary *, const char *key);

Chapter 4: Data Structures & Algorithms © HDC 2022.1 80


Example
#define HASHSIZE 101
struct Nlist {
char *word; /* search word */
char *defn; /* word definition */
struct Nlist *next; /* pointer to next entry in chain */
};

struct Dictionary t {
/* table is an array of pointers to entries */
struct Nlist *table[HASHSIZE];
};

static unsigned hash_function(const char *str) {


/* Hashing function converts a string to an index within hash table. */
const int HashValue = 31;
unsigned h;

for (h = 0; *str != ’\0’; ++str)


h = *str + HashValue * h;
return h % HASHSIZE;
}

Chapter 4: Data Structures & Algorithms © HDC 2022.1 81


Example
int add_word(Dictionary *dict, const char *key, const char *defn) {
/* Add new word to table. Replaces old definition if word already exists.
Return 0 if successful, and -1 is fails. */
unsigned i = hash function(key); /* get table index */
struct Nlist *pnode = dict−>table[i];
while (pnode && strcmp(pnode−>word, key) != 0) /* search chain */
pnode = pnode−>next;
if (pnode) { /* match found, replace definition */
char *str = allocate string(defn);
if (str == NULL) /* allocation fails, return fail and keep old defn */
return −1;
free(pnode−>defn);
pnode−>defn = str;
}
else { /* no match, add new entry to head of chain */
pnode = makenode(key, defn);
if (pnode == NULL)
return −1;
pnode−>next = dict−>table[i];
dict−>table[i] = pnode;
}
return 0;
}

Chapter 4: Data Structures & Algorithms © HDC 2022.1 82


Example
char *find_word(const Dictionary *dict, const char *key) {
/* Find definition for keyword.
Return NULL if key not found. */
unsigned i = hash function(key); /* get table index */
struct Nlist *pnode = dict−>table[i];
while (pnode && strcmp(pnode−>word, key) != 0)
pnode = pnode−>next; /* search index chain */
if (pnode) /* match found */
return pnode−>defn;
return NULL;
}

Chapter 4: Data Structures & Algorithms © HDC 2022.1 83


4.5 Sorting algorithms
 Objective: rearrange a given array or list elements
according to a comparison operator on the elements.
The comparison operator is used to decide the new
order of element in the respective data structure.
 Ascending order: from the smallest to the largest number
 Descending order: from the largest number to smallest number
 Problem statement:
 Input: given an array a of size n: a[0], a[1], …, a[n-1]
 Output: a permutation (reordering) of a (a’[0], a’[1], …, a’[n-
1] in such a way that:
• a’[0] <= a’[1] <= ... <= a’[n-1]

Chapter 4: Data Structures & Algorithms © HDC 2022.1 84


Sorting algorithms
 A number of applications in reality:
 Commercial computing. Government organizations, financial institutions, and
commercial enterprises organize much of this information by sorting it
 Search for information. Keeping data in sorted order makes it possible to
efficiently search through it. E.g.: sorting the number of access of websites
using a search engine like Google
 String processing algorithms are often based on sorting
 As fundamental algorithm to solve more complex problems such as numerical
computations, operations research, optimization, etc.
 Early sorting algorithms were proposed in 1950s such as bubble sort,
merge sort, quick sort, etc.; new ones are still being invented
 References:
 Cormen, Thomas H., and Thomas H. Cormen. 2001. Introduction to algorithms.
Cambridge, Mass: MIT Press.
 Robert Sedgewick and Kevin Wayne. 2011. Algorithms (4th. ed.). Addison-
Wesley Professional.
 Introduction of 4 algorithms: selection sort, merge sort, insertion sort
and quick sort 85
Chapter 4: Data Structures & Algorithms © HDC 2022.1
Sorting algorithms
 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]

Chapter 4: Data Structures & Algorithms © HDC 2022.1 86


4.5.1 Selection sort
 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

Chapter 4: Data Structures & Algorithms © HDC 2022.1 87


Find the smallest number
/* Yield location of smallest element
in a[k..n-1] */
/* Assumption: k < n */
/* Returns index of smallest, does not return the
smallest value itself */

int min_loc (int a[], int k, int n) {


/* a[pos] is smallest element found so far */
int j, pos;
pos = k;
for (j = k + 1; j < n; j = j + 1)
if (a[j] < a[pos])
pos = j;
return pos;
}
Chapter 4: Data Structures & Algorithms © HDC 2022.1 88
Selection sort
/* Sort a[0..n-1] in non-decreasing
order (rearrange
elements in a so that
a[0]<=a[1]<=…<=a[n-1] ) */

int sel_sort (int a[], int n) {


int k, m;
for (k = 0; k < n - 1; k = k + 1) {
m = min_loc(a,k,n);
swap(&a[k], &a[m]);
}
}

Chapter 4: Data Structures & Algorithms © HDC 2022.1 89


Example

Chapter 4: Data Structures & Algorithms © HDC 2022.1 90


Example

Chapter 4: Data Structures & Algorithms © HDC 2022.1 91


Example

Chapter 4: Data Structures & Algorithms © HDC 2022.1 92


Algorithm evaluation
 How many iterations do we need to sort n numbers?
 Before swapping position, it is needed to check through the
whole unsorted part of the array
 The length of unsorted part in the initial array is n
 The algorithm repeats n iterations of checking/swapping
 Total of steps is proportional to
 Conclusion: selection sort is pretty slow with large
arrays

Chapter 4: Data Structures & Algorithms © HDC 2022.1 93


Better algorithm?

 Algorithms with complexity of nlogn


 Merge sort
 Quick sort
 When array size grows, time taken to execute
algorithms with complexity of n2 is much higher than
that with nlogn complexity
Chapter 4: Data Structures & Algorithms © HDC 2022.1 94
4.5.2 Merge sort
 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

Chapter 4: Data Structures & Algorithms © HDC 2022.1 95


Merge sort

Chapter 4: Data Structures & Algorithms © HDC 2022.1 96


Merge sort

Chapter 4: Data Structures & Algorithms © HDC 2022.1 97


Merge sort

Chapter 4: Data Structures & Algorithms © HDC 2022.1 98


Merge sort

 We need n comparisons and copying data n times, thus


the workload is proportional to n
 However, this is not yet a sorting algorithm
 So how to do?

Chapter 4: Data Structures & Algorithms © HDC 2022.1 99


Merge sort implementation
 It is needed to know the position of pointers to merge
the respected arrays together
 At the starting point, each element in the array is a
pointer
 Merge sort:
 Merge each pair of 1-element arrays to be a 2 element array
 Merge each pair of 2-element arrays to be a 4 element array
 Merge each pair of 4-element arrays to be a 8 element array
 And so on until completing

Chapter 4: Data Structures & Algorithms © HDC 2022.1 100


Example

Chapter 4: Data Structures & Algorithms © HDC 2022.1 101


Example

 Sorting task is done


 Each merging step take a duration proportional to n
 How many time do we merge? It is 3 in this example
 In general, we need log2n merging steps
 When n =8:
 Total time consumed is proportional to nlog2n (or
nlogn).
Chapter 4: Data Structures & Algorithms © HDC 2022.1 102
In short

Chapter 4: Data Structures & Algorithms © HDC 2022.1 103


Any approach performed better
than nlogn?
 In general, the answer is NO
 However, in some special cases, we may do better
 E.g.: sorting exam papers based on the scores put the
papers into one of 10 piles with respect to their scores
required time is proportional to n.
 The performance of the algorithm can be evaluated via
mathematical calculation without using computer
 This specific area in mathematics is approximation theory.
There are a lot of interesting topics to be solved
 E.g.: The P versus NP problem: Whether every problem
whose solution can be quickly verified can also be solved
quickly
Chapter 4: Data Structures & Algorithms © HDC 2022.1 104
Efficiency
 The resources can be saved
 It is usually measured by execution duration and
memory space required
 A lot of details in programming has little or no effect on
efficiency
 It is often achieved by selecting the right algorithm
and/or data structure

Chapter 4: Data Structures & Algorithms © HDC 2022.1 105


4.5.3 Example of sorting algorithm
usage with complex data structure
 Revision of structure and array: A structure represent a
simple data record, computer program processes a set
of data records
 E.g.: student record, staff record, customer record, etc.
 In each case, there are a number of variables, thus it
would require to use an array of structure to store data

Chapter 4: Data Structures & Algorithms © HDC 2022.1 106


Example
#define MAX_NAME 40 typedef struct {
typedef struct { int hours, minutes;
char name [MAX_NAME+1]; double seconds;

int id; } time;

double score;
} student_record; typedef struct {
double x, y ;
} point;

Chapter 4: Data Structures & Algorithms © HDC 2022.1 107


Array of structure
 Each statement below shows the declaration of an array
in which each element is a structure
point corner_points[10];
time meeting_times[MAX_MEETINGS];
student_record tdh_34[MAX_STUDENTS];
 The fields of structure are accessed via the array
element and the field names
cse_142[i].name
corner_points[j+1].x
meeting_times[4].hours

Chapter 4: Data Structures & Algorithms © HDC 2022.1 108


Array of structure
point pentagon[5];
Array of structure pentagon

x
y
pentagon[1] – structure
x
y
x
y pentagon[4].x – a real number
x
y
x
y

Chapter 4: Data Structures & Algorithms © HDC 2022.1 109


Array of structure
student_record class[MAX_STUDENTS];
...
for (i = 0; i < nstudents; i = i + 1)
{
scanf(“%d %d”, &class[i].hw, &class[i].exams);
class[i].grade =
(double) (class[i].hw+class[i].exams) / 50.0;
}

Chapter 4: Data Structures & Algorithms © HDC 2022.1 110


Array element as argument
void draw_line (point p1, point p2) {.....}
...
point pentagon[5];
...
for (i = 0; i < 4; i = i + 1) {
draw_line(pentagon[i], pentagon[i+1]);
}
draw_line(pentagon[4], pentagon[0]);

Chapter 4: Data Structures & Algorithms © HDC 2022.1 111


Revision on passing a value
 A structure variable is passed with its value
 All the fields of the variable are copied to initialize the
argument

point midpoint (point a; point b) {...}


int main (void) {
point p1, p2, m; /* declare 3 points */
...
m = midpoint(p1, p2);
}

Chapter 4: Data Structures & Algorithms © HDC 2022.1 112


Revision on passing an array
 An array of element is an array
 When the array is used as a parameter in function call,
it needs to be passed by using reference
 The reference is actually an alias of the input parameter
int avg (student_rec class_db[MAX_N]) {...}
int main (void) {
student_rec ktlt_k50[MAX_N];
int average;
....
average = avg(ktlt_k50); /*by reference*/
}
Chapter 4: Data Structures & Algorithms © HDC 2022.1 113
Sorting an array of structure
David Kathryn Sarah Phil Casey
920915 901028 900317 920914 910607
2.9 4.0 3.9 2.8 3.6

Phil David Casey Sarah Kathryn


920914 920915 910607 900317 901028
2.8 2.9 3.6 3.9 4.0

typedef struct {
char name[MAX_NAME + 1];
int id;
double score;
} StudentRecord;

Chapter 4: Data Structures & Algorithms © HDC 2022.1 114


Revision of selection sort
int min_loc (int a[ ], int k, int n) {
int j, pos; pos = k;
for (j = k + 1; j < n; j = j + 1)
if (a[j] < a[pos])
pos = j;
return pos;
}
void swap (int *x, int *y);
void sel_sort (int a[ ], int n) {
int k, m;
for (k = 0; k < n - 1; k = k + 1) {
m = min_loc(a,k,n);
swap(&a[k], &a[m]);
}
}
Chapter 4: Data Structures & Algorithms © HDC 2022.1 115
Sorting an array of structure
 Initially, it is required to identify the field to sort by
 E.g.: sort by scores
 Change the data type of the array to StudentRecord
 Rewrite the code for comparison in min_loc function
 Write swap function for StudentRecord

Chapter 4: Data Structures & Algorithms © HDC 2022.1 116


Sorting an array of structure
int min_loc (StudentRecord a[ ], int k, int n) {
int j, pos; pos = k;
for (j = k + 1; j < n; j = j + 1)
if (a[j].score < a[pos].score)
pos = j;
return pos;
}

void swap (StudentRecord *x, StudentRecord *y);


void sel_sort (StudentRecord a[ ], int n) {
int k, m;
for (k = 0; k < n - 1; k = k + 1) {
m = min_loc(a,k,n);
swap(&a[k], &a[m]);
}
}

Chapter 4: Data Structures & Algorithms © HDC 2022.1 117


Sorting by alphabetic order
David Kathryn Sarah Phil Casey
920915 901028 900317 920914 910607
2.9 4.0 3.9 2.8 3.6

Casey David Kathryn Phil Sarah


910607 920915 901028 920914 900317
3.6 2.9 4.0 2.8 3.9

typedef struct {
char name[MAX_NAME + 1];
int id;
double score;
} StudentRecord;

 It is required to write a function for comparing two strings


Chapter 4: Data Structures & Algorithms © HDC 2022.1 118
String compare revision
 “Alice” < “Bob”
 “Dave” < “David”
 “Rob” < “Robert”
#include <string.h>
int strcmp(char str1[ ], char str2[ ]);

 Returning value is
 A negative number if str1 < str2
 Zero if str1 = str2
 A positive number if str1 > str2

Chapter 4: Data Structures & Algorithms © HDC 2022.1 119


Sorting by alphabetic order
int min_loc (StudentRecord a[ ], int k, int n) {
int j, pos; pos = k;
for (j = k + 1; j < n; j = j + 1)
if (0 > strcmp(a[j].name, a[pos].name))
pos = j;
return pos;
}
void swap (StudentRecord *x, StudentRecord *y);
void sel_sort (StudentRecord a[ ], int n) {
int k, m;
for (k = 0; k < n - 1; k = k + 1) {
m = min_loc(a,k,n);
swap(&a[k], &a[m]);
}
}

Chapter 4: Data Structures & Algorithms © HDC 2022.1 120


Thinking in data structure
 If we want to store information of a song in a computer
 What kind of information needs to be stored?
 How to organize the information?
 How to implement in C?
 And if
 We need information of a CD
 Or information of a set of CDs

Chapter 4: Data Structures & Algorithms © HDC 2022.1 121


4.5.4 Insertion sort

/* sort student records a[0..size-1] in */


/* ascending order by score */
void sort (student_record a[ ], int size)
{
int j;
for (j = 1; j < size; j = j + 1)
insert(a, j);
}
Chapter 4: Data Structures & Algorithms © HDC 2022.1 122
Insertion sort
/* given that a[0..j-1] is sorted, move a[j]
to the correct location so that that a[0..j]
is sorted by score */
void insert (student_record a[ ], int j) {
int i;
student_record temp;
temp = a[j];
for (i = j; i > 0 &&
a[i-1].score > temp.score; i = i-1) {
a[i] = a[i-1];
}
a[i] = temp;
}
Chapter 4: Data Structures & Algorithms © HDC 2022.1 123
Insertion sort
/* given that a[0..j-1] is sorted, move a[j] to
the correct location so that that a[0..j] is
sorted by score */
void insert (student_record a[ ], int j) {
int i;
student_record temp;
temp = a[j];
for (i = j; i > 0 &&
strcmp(a[i-1].name, temp.name) > 0;
i = i-1) {
a[i] = a[i-1];
}
a[i] = temp;
}

Chapter 4: Data Structures & Algorithms © HDC 2022.1 124


4.5.5 Quick sort - Example
 Sorting the following integer array

40 20 10 80 100 50 7 30 60

Chapter 4: Data Structures & Algorithms © HDC 2022.1 125


Select the ‘pivot’ element
 Choose the element to be compared with: e.g. select the
first element

40 20 10 80 60 50 7 30 100

Chapter 4: Data Structures & Algorithms © HDC 2022.1 126


Divide the array into smaller ones
 Given number of elements, dividing the array into 2:
 The first array includes elements the pivot one
 The other include elements the pivot one

7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]

<= data[pivot] > data[pivot]

Chapter 4: Data Structures & Algorithms © HDC 2022.1 127


Recursion: Quicksort Sub-arrays

7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]

<= data[pivot] > data[pivot]

Chapter 4: Data Structures & Algorithms © HDC 2022.1 128


Quicksort Analysis
 Assume that keys are random, uniformly distributed.
 What is best case running time?
 Recursion:
1. Partition splits array in two sub-arrays of size n/2
2. Quicksort each sub-array
 Depth of recursion tree? O(log2n)
 Number of accesses in partition? O(n)

7 20 10 30 7 20 10 7 10 20

Chapter 4: Data Structures & Algorithms © HDC 2022.1 129


Quicksort Analysis
 Assume that all elements are randomly distributed
 Shortest duration: O(n log2n)
 How about the worst case scenario?

Chapter 4: Data Structures & Algorithms © HDC 2022.1 130


Quicksort: the worst case
 Assumed that all the elements are distributed
ascendingly

pivot_index = 0 2 4 10 12 13 50 57 63 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]

Chapter 4: Data Structures & Algorithms © HDC 2022.1 131


Another approach to choose the
pivot
 Select the mean of 3 elements in the array
 data[0], data[n/2], and data[n-1].
 Use this mean value as the pivot

Chapter 4: Data Structures & Algorithms © HDC 2022.1 132


Optimizing Quicksort
 Find the pivot element
 If the size is smaller or equal to 3:
 One element: do nothing
 If there is 2 elements: if(data[first] > data[second]) swap them
 If there is 3 elements: homework
 Implementation of quicksort can be done by using
recursion (homework)

Chapter 4: Data Structures & Algorithms © HDC 2022.1 133


END OF CHAPTER 4

Chapter 4: Data Structures & Algorithms © HDC 2022.1 134

You might also like