Professional Documents
Culture Documents
Complexity Analysis
Recursion
Sorting Algorithms
Abstract Data Type
Complexity Analysis
ALGORITHM
PROPERTIES OF AN ALGORITHM
It must be correct.
-
It must terminate.
EFFICIENCY OF AN ALGORITHM:
Space
o the amount of memory used
o directly calculated based on the declaration
of variables (sizeof or bytes used)
Speed
o the approximated time needed for a
program to finish its execution
o depends on the CPU, Memory, Input size,
software, algorithm
o algorithms running time (programs
running time)
Ease in implementation (algorithms basic
operation count)
CASES: Hold the size of the program fixed
Best case: input that requires the fewest basic
operations to obtain results
ALGORITHM ANALYSIS
Frequency Count
this refers to the no. of times a statement or
group of statements are executed within the
entire duration of the program.
Running Time Big O Notation
one method of describing the running time of
an alogrithm.
The notation that describes the lower bound
is called the Omega Notation
The Big-Oh describes the upper bound for
the running time of an algorithm
provides a way to simplify analysis by getting
rid of irrelevant information
RULES IN DETERMINING:
1. An I/O statement, assignment start will take
one unit of time O(1)
2. In a conditional statement, the running time is
equal to the maximum running time between
executing the statements when the condition
is satisfied and when it is not. depends
Recursion
-
ADVANTAGES/VALUES OF RECURSION:
Recursion can be used to replace loops.
Recursively defined data structures, like lists,
are very well-suited to processing by recursive
procedures and functions.
A recursive procedure is mathematically more
elegant than one using loops.
Sometimes procedures that would be tricky to
write using a loop are straightforward using
recursion.
On the book:
TYPES OF RECURSIVE CALL:
Directly Recursive a function that calls itself
Indirectly Recursive a function that calls
another function and eventually results in the
original function call
Tail recursive function recursive function in
which the last statement executed is the
recursive call
Infinite recursion the case where every
recursive call results in another recursive call.
STEPS IN DESIGNING A RECURSIVE FUNCTION:
1. Understanding problem requirements
2. Determine limiting condition
3. Identify base cases
4. Provide direct solution to each base case
5. Identify general case(s)
6. Provide solution to general cases in terms of
smaller versions of itself (recursion)
Sorting Algorithms
Sorting is the process of arranging data in a certain
order, either in ascending or descending.
THINGS TO MEASURE IN SORT ALGO:
Passes / Runs
Number of Comparisons done
-
SORTING TECHNIQUES
BIN/BUCKET SORT
requires the amount of nos. to be w/in a
predetermined range
RADIX SORT
arranges by the place values of the elements,
arranging from the smallest to largest and
vice versa (by a revised bucket system).
BUBBLE SORT
the most common in terms of implementation
Procedure:
test only two numbers at a time , starting with
the first two numbers
if the top is smaller, leave as is. If top is larger,
switch the 2 nos. go down one no. and
compare that no. with the no. that follows it.
Continue this process until no switch has been
made in an entire pass through the list.
SELECTION SORT
Locates / selects the smallest / largest
element first, then. place that element in its
most appropriate position
INSERTION SORT
inserts the elements in its proper position
in the array
SHELL SORT
Named after Donald Shell
Also known as the diminishing increment sort
MERGE SORT
can work with nos. of unlimited duplication
and unspecified size
determine the middle of the initial array
each sub-array is split into two until each subarrays have two and one elements each.
A single comparison is performed on the twoelement array
Merge by comparing elements in each array,
one at a time.
QUICK SORT
most efficient
choose a partitioning element called the pivot
one part contains elements that are less than
or equal to the pivot, the other part contains
elements that are greater than the pivot
new pivots are chosen until arrays contain
only 1 or 2 elements perform necessary
comparisons
TIME COMPLEXITY OF SORTING ALGOS:
BUCKET
RADIX
BUBBLE
SELECTION
INSERTION
SHELL
MERGE
QUICK
WORST
2
O(n )
O(n)
2
O(n )
2
O(n )
2
O(n )
2
O(n )
O(nlog2n)
2
O(n )
AVE
O(n)
O(n)
2
O(n )
2
O(n )
2
O(n )
2
O(n )
O(nlog2n)
O(nlog2n)
BEST
O(n)
O(n)
O(n)
2
O(n )
O(n)
O(n)
O(nlog2n)
O(nlog2n)
3.
Implementation
Pointers
STATIC AND DYNAMIC ALLOCATION
Static Allocation
o Binds memory space to variables at
compile time (stack)
o Static variables
Dynamic Allocation
Binds memory space to variable during
runtime (heap)
o Dynamic Variables
o
POINTER
a variable that is the address of another
variable. They are used to access dynamic
variables
Two operators are used to work with pointers:
the ampersand (&) means the address of
and the asterisk (*) the indirection operator
means contents of address.
A pointer is declared using the data type of
the variable it points to plus an asterisk (*)
before its name.
BASIC POINTER OPERATIONS
Dereferencing and Indirection
Assignment: Pointer variables can be assigned
the values of other pointer variables that are
bound to the same type.
Comparison: We can compare pointer
variables that are bound to the same type
using relational operators like == and !=.
Reviewer Compiled by Donald
6.
REPRESENTATION:
int n=100;
class LS
{
char letter_string [n];
int length;
void append(char c);
void remove(int n);
void print(int i);
void makenull()
int empty();
int full(); };
1. APPEND
Pre: LS is not full
Post: a new char is added at the tail end of the LS
& likewise the gap is closed
2. REMOVE
Pre: not empty
Post: one character should have been removed
from the LS
3. PRINT
Pre: LS is not empty/none
Post: LS is printed
4. MAKENULL
Pre: 0 length ls. Length/none
Post: All elements were deleted
5. EMPTY
Pre: 0length ls /none
Post: returned TRUE if LS is empty, otherwise
returned false
6. FULL
Pre: LS must be full/none
Post: returned true if LS is full, otherwise returned
false
IMPLEMENTATION:
void LS :: append (char c){
if (LS.length != n-1){
LS.length = LS.length + 1;
LS.letter_string[LS.length]= c;
}
else if (LS.length == n-1)
cout<<the letterstring is full;
}
void LS :: print (int i) {
if (LS.length<= -1)
cout<<empty;
else
for( i=0; i<=LS.length; i++)
cout<<LS.letter_string[i];
else if (LS.length<n)
cout<<position is beyond the length;
else{
for (int i=n;i<LS.length; i++)
LS.letter_string[i] = LS.letter_string [i+1];
LS.length = LS.length-1;}
}
void LS::makenull (){
if (LS.length> -1)
LS.length= -1;
else
cout<<theres no character in LS;
}
int LS ::empty (){
if (LS.length<0)
else
return 1;
return 0; }
return 1;
return 0;}
else
list newnode;
newnode = (list)malloc(sizeof(struct node));
newnode->value = num;
newnode->link = *head;
*head = newnode;
}
void delhead(list *head){
list ptr;
if(*head!=NULL){
ptr=*head;
*head=ptr->link;
free(ptr);
}
else
cout<<"Nothing to delete!!!";
cout<<"Nothing to delete!!!";}
cout<<ptr->value;
}
void makenull(list *head)
{ *head = NULL; }
int empty (list *head)
{ if (*head !=NULL)
return 0;
else
return 1;
}
DOUBLY LINKED LIST
Each node has 2 reference field, one to the
successor and the one to the predecessor.
CIRCULAR LINKED LIST
Lists where nodes form a ring
g.
int main(){
for(int i = n; i < p.Last; i++)
printf(Hello World!);
} //Count: p.Last - n
h.
int main(){
for(int i = n; i <= p.Last; i++)
printf(Hello World!);
} //Count: p.Last - n + 1
i.
int main(){
for(int i = 1; i <= p.Last-n; i++)
for(int k = 2; k < p.Last-1; k++)
printf(Hello po);
} //Count: (p.Last n 1)(p.Last 3)
BIG-OH NOTATION
for(int i = 0; i <= n; i++)
cout << sigaw;
//O(n)
int y = 4; //O(1)
int x;
//O(0)
X = X + 1 //O(1)
RECURSION SIMULATION
A. Linear
int rose(int x){
if(x == 0)
return x;
else
return x%10 + rose(x/10);
}
let x be 12345
1
rose(12345)
2
rose(1234)
3
rose(123)
4
rose(12)
5
rose(1)
6
rose(0)
= 5 + rose(1234)
= 4 + rose(123)
= 3 + rose(12)
= 2 + rose(1)
= 1 +rose(0)
=0
Then,
Since rose(0) = 0,
substitute to the equation of rose(1)
5
rose(1)
= 1 + rose(0) //substitute
1+0
5
rose(1) = 1
Then,
4
rose(12)
4
rose(12)
Then,
3
rose(123)
3
rose(123)
Then,
2
rose(1234)
2
rose(1234)
2 + rose(1) //substitute
2+1
3
Then,
1
rose(12345)
1
=
=
3 + rose(12) //substitute
3+3
6
= 4 + rose(123) //substitute
4+6
= 10
rose(12345)
Let i be 5
= 5 + rose(1234) //substitute
5 + 10
= 15
Tail
Output
frog(5) 5
frog(4) 4
frog(3) 3
frog(2) 2
frog(1) 1
frog(0) //none
Output: 54321
C.
frog(4)
frog(3)
frog(2)
frog(1)
frog(0)
Because 0 > 0 is false
Nontail
frog(5)
frog(4)
frog(3)
frog(2)
frog(1)
frog(0)
Stack
5
4
3
2
1
----------
Stack
frog(5)
5
frog(4)
4
frog(3)
3
frog(2)
2
frog(1)
1
frog(0) ---------Output: 12345
Output
frog(4)
frog(3)
frog(2)
frog(1)
frog(0)
----------
endl
Output
5
4
3
2
1
endl
frog(4)
frog(3)
frog(2)
frog(1)
frog(0)
----------
SORTING SIMULATION
BUBBLE SORT (ASCENDING)
1ST PASS:
.
3RD PASS:
TOTAL COMPARISON: 3 + 3 + 3 + 3 = 12
COMPARISON: 3
COMPARISON: 3
2ND PASS:
4TH PASS:
COMPARISON: 3
PASSES
: 4=n
COMPARISON: 3
Reviewer Compiled by Donald
MIN
MIN = 8
MIN
4
4
MIN
MIN
COMPARISONS: 2
COMPARISONS: 3
MIN
MIN
SWAP!
MIN
MIN
COMPARISONS: 1
SWAP!
MIN
MIN
MIN
3RD PASS:
MIN = 4
MIN
MIN = 4
2ND PASS
MIN
4TH PASS:
MIN = 8
NO SWAP! NUMBER IS IN PLACE.
MIN
MIN
MIN
COMPARISON: 0
.
COMPARISON: 3 + 2 + 1 + 0 = 6
PASSES:
4=n
1ST PASS
COMPARISON = 0
2ND PASS
4TH PASS
COMPARISON = 1
4 > 5 IS FALSE, THEREFORE, THERES NO SWAP
NO SWAP = NO COMPARISON (FOR INSERTION)
COMPARISON = 3
TOTAL COMP: 0 + 1 + 1 + 3 = 5
PASSES = 4 = n
COMPARISON = 1
10
4 3
7 9
8 1
8 1
COMPARISON CHECK!
4 3
7 9
COMPARISON = 2
3 4
COMPARISON CHECK!
7 9
3 4 7 9
1 8
1 8
1 3 4 7 9 8
1 2 3 4 7 9 8
2
2
COMPARISION = 1
3
3
4
4
11
POINTERS SIMULATION
passed value
z += *x;
*x = *y + z;
13
t=z
13
z = *y
13
*y = *x
13
13
*x = t
13
NEW VALUE:
a = 7; b = 13; c = 3
Line 3: x = &c; y = &b; z = a;
X
passed value
13
z += *x;
13
10
*x = *y + z;
23
13
10
t=z
23
13
10
10
z = *y
23
13
13
10
passed value
*y = *x
23
23
13
10
z += *x;
*x = t
10
23
13
10
*x = *y + z;
t=z
z = *y
*y = *x
*x = t
NEW VALUE:
a = 4; b = 6; c = 3
NEW VALUE:
a = 7; b = 23; c = 10
Output:
VALUE OF VV: 20
Line 1: 4 6 3
Line 2: 7 13 3
Line 3: 7 23 10
EXERCISE: Determine the output
void CC(int *a, int b)
int t;
t = *a;
*a = *a + b / 2;
b = ++(*a);
*a = t + 1;
}
int VV(int *x, int z, int *y)
{
*x += *y;
(*x)++;
*y = z;
return (*x + *y + z);
}
main(){
int a = 4, b = 5, c = 6;
cout << VALUE OF VV: <<VV(&b, a, &c);
cout << endl;
cout << Line 1: << a << << b << << c;
CC(&a, c);
cout << endl;
cout << Line 2: << a << << b << << c;
CC(&a, VV(&b, a, &c)); //note: evaluate VV first
cout << endl;
cout << Line 3: << a << << b << << c;
}
Output:
VALUE OF VV: 20
Line 1: 4 12 4
Line 2: 5 12 4
Line 3: 6 17 5
12