CPU 1142 DATA STRUCTURES AND ALGORITHMS - Day …Internal sorting algorithms are of 7 types. 1...
Transcript of CPU 1142 DATA STRUCTURES AND ALGORITHMS - Day …Internal sorting algorithms are of 7 types. 1...
CPU 1142DATA STRUCTURES AND ALGORITHMS
Day School 6
M. K. A. AriyaratneB [email protected]
T 0112 881225
Department of Mathematics and Computer ScienceFaculty of Natural Sciences
The Open University of Sri LankaSri Lanka
Sept 19, 2014
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 1 / 45
Summary
Summary
We have seen the meaning of the term Data Structures and we
have learned few Data Structures; Array, Singly Linked List,
Circular Linked List, Doubly Linked List, Stack, Queue, Tree,
Binary Tree and Graphs.
Then we started learning Algorithms.
We discussed features of algorithms and few problem solving
techniques that are useful when designing algorithms.1 Divide and Conquer method
2 Dynamic Programming
3 Greedy Method
4 Linear Programming
We also studied the importance of analyzing algorithms.
We have seen how to analyze algorithms in terms of space
complexity and time complexity.M. K. A. Ariyaratne Day School 6 Sept 19, 2014 2 / 45
Introduction
Introduction
Now we know few Data Structures and importance of analyzing
algorithms.
Today Let’s try to learn a new programming technique and few
Sorting Algorithms.
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 3 / 45
Introduction
How is the Evaluation
CPU 1142 DATE
NBT 1 21 - 08 - 2014
NBT 2 25 - 09 - 2014
PT 15 - 09 - 2014
Final Examination .....
Contact Details:
T : 0112 881225
m : http://www.mkaariyaratne.com
: http://ousl.nodes.lk/
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 4 / 45
Recursion
LESSON 17
Recursion
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 5 / 45
Recursion
Introduction
In computer science, recursion is a way of solving problems and is
one of the main programming techniques.
We have seen that repetition can be achieved by writing loops,
such as for loops and while loops.
Another way to achieve repetition is through recursion, which
occurs when a function calls itself.
Recursion is a method where the solution to a problem depends on
solutions to smaller instances of the same problem.
Most computer programming languages support recursion by
allowing a function to call itself within the program text.
In this lesson we will be studying basic recursive procedures, types
of recursion, pros and cons of using recursion and so on.
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 6 / 45
Recursion
Types of Recursion
There are two types of recursion
1 Direct Recursion
2 Indirect Recursion
Direct Recursion is where a function calls itself.
Indirect recursion is where a function A calls another function B,
Fuction B calls function C and so on until eventually the function
A is called.int abc(){
----------------------abc();-----------end
}
int abc(){
xyz();}int xyz(){
abc();}
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 7 / 45
Recursion
Properties of Recursive Algorithms
In order to develop successful recursive programs, there are few
requirements to be fulfilled.
1 Recursive function should not infinity call to itself.
2 For at least one argument, the recursive function f must be defined
in terms without f .
3 There must be a way to get out from the sequence of recursive calls.
4 Recursive algorithm must eventually reduce to one or more simple,
non recursive cases.
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 8 / 45
Recursion
Simple recursive programs
Let’s look at a mathematical definition of a recursive function.
fact(n) =
1, if n = 0.
n× fact(n− 1), otherwise.(1)
This can be written in C as follows
int fact (int n)
{if (n == 0)
return 1;
else
return n * fact(n-1);
}M. K. A. Ariyaratne Day School 6 Sept 19, 2014 9 / 45
Recursion
Simple recursive programs
Another well known recursive functions are Fibonacci number
series and Triangular numbers.
nth tri number =
{1, if n = 1.
n + (n− 1)th tri number, otherwise.(2)
nthfib number =
0, if n = 0.
1, if n = 1.
(n− 1)th fib num + (n− 2)th fib num, otherwise.
(3)
We can use recursion when implementing data structures too.
That is where you implement Linked lists, Binary tress using
pointers.
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 10 / 45
Recursion
Recursion Vs. Iteration
Recursion is a good programming technique, but not always the
best.
Here we will give some reasons for why recursion is not good
always.
1 Recursive algorithms need more stack space than iterative
algorithms.
2 It makes inefficient utilization of memory.
3 It slows down execution speed.
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 11 / 45
Internal Sorting
LESSON 18
Internal Sorting
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 12 / 45
Internal Sorting
Introduction
The process of sorting a list of items in a specific way such as in
ascending or in descending order is a very fundamental process,
done very frequently.
The evolution of computer sorting algorithms parallels the
evolution of computers.
To sort items, computers need a considerable amount of running
time. Therefore selecting the most appropriate sorting algorithm
for the situation, is important.There are many computer sorting algorithms in the world andthey are classified in to two main categories.
1 Internal Sorting : Sorting is take place in the main memory of the
computer2 External Sorting : When the set of items to be sorted too large to
be fit in to main memory, data to be sorted use secondary storage
devices.
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 13 / 45
Internal Sorting
Introduction(2)
Main memory: The main memory of the computer is also knownas RAM, standing for Random Access Memory.The computer canmanipulate only data that is in main memory. Therefore, everyprogram you execute and every file you access must be copied froma storage device into main memory. This has volatile memory.
Secondary Memory: This is a non-volatile memory (does not losethe data when the device is powered down) that it is not directlyaccessible by the CPU. E.g.Flash memory, Hard Drive.
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 14 / 45
Internal Sorting
Introduction(3)
Internal sorting algorithms are of 7 types.
1 Bubble Sort
2 Insertion Sort
3 Quick Sort
4 Heap Sort
5 Merge Sort
6 Radix Sort
7 Selection sort
One example for the external sorting is external Merge sort.
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 15 / 45
Internal Sorting
Internal Sorting Algorithms
As stated before, these algorithms are used to sort data when the
size of the data set fits to the main memory of the computer.
These algorithms are also known as in-place algorithms.
That is because when we do sorting using these algorithms, the
rearrangement process occur entirely within the sequence of the
data set with one or two additional locations to store elements
temporarily.
It is difficult to decide what is the best algorithm and it is based
on the circumstances.
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 16 / 45
Internal Sorting
Sorting by Exchange - Bubble Sort
We will first understand the nature of each sorting algorithm using
an understandable example.
Assume we have set of people in a line.
We need to sort them according to their height; smallest to tallest.
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 17 / 45
Internal Sorting
Sorting by Exchange - Bubble Sort(2)
The bubble sort is notoriously slow, but it is conceptually the
simplest of the sorting algorithms and for that reason, is a good
beginning for our exploration of sorting techniques.
In bubble sort, each element is compared with its adjacent element.
If the first element is larger than the second element, then the
positions are interchanged. Then you go one step front and repeat
the process.
The algorithm gets its name from the way smaller elements
“bubbles” to the top of the list.
Let’s apply Bubble Sort on the Set of unsorted people in the line
that we saw earlier.
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 18 / 45
Internal Sorting
Sorting by Exchange - Bubble Sort(3)
1 Compare two players
2 If the one in the left is
taller, swap them
3 Move one position right
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 19 / 45
Internal Sorting
Bubble Sort on Set of Integers
Let’s see how bubble sort works on a set of Integers
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 20 / 45
Internal Sorting
Bubble Sort - C code
void bubbleSort(int a[], int n)
{int i,j,tmp;
for (i=0; i<n-1; i++)
{for (j=0; j<n-1; j++)
{if (a[j] > a[j+1]) /* compare the two neighbors */
{tmp = a[j]; /* swap a[j] and a[j+1] */
a[j] = a[j+1];
a[j+1] = tmp;
}}}
}M. K. A. Ariyaratne Day School 6 Sept 19, 2014 21 / 45
Internal Sorting
Quick Sort
Quick sort is undoubtedly the most popular sorting algorithm and
is the most efficient algorithm in internal sorting.
Basically, the quick sort algorithm operates by partitioning an
array into two sub arrays and then calling itself recursively to
quick sort each of these sub arrays.
This is another divide-and-conquer algorithm.
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 22 / 45
Internal Sorting
Quick Sort(2)
Following are the basic steps of the quick sort algorithm.
Pick an element, say P (the pivot) from the data set.
Re-arrange the elements into 3 sub-blocks,
1 those less than or equal to (<=)P(the left-block S1)
2 P(the only element in the middle-block)
3 those greater than or equal to (>=)P(the right-block S2)
Repeat the process recursively for the left and right sub blocks.
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 23 / 45
Internal Sorting
Quick Sort(3)
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 24 / 45
Internal Sorting
Quick Sort(4)
The main idea is to find the “right” position for the pivot element
P.
After each “pass”, the pivot element, P, should be “in place”.
Eventually, the elements are sorted since each pass puts at least
one element (i.e., P) into its final position.
Issue : How to choose the pivot P?
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 25 / 45
Internal Sorting
Quick Sort(5)
Now let’s see how to carry out a quick sort on set of integers.
First we select a pivot value from the data set.
Then we do the comparison as follows.
The process should repeat to the partitions as well.
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 26 / 45
Internal Sorting
Quick Sort - Start
Start with all datain an array, andconsider it unsorted
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 27 / 45
Internal Sorting
Quick Sort - Step 1
Step 1Select a pivot(it is arbitrary)
We will select the first
element, as presented in the
original algorithm by
C.A.R. Hoare in 1962.
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 28 / 45
Internal Sorting
Quick Sort - Step 2
Step 2Start process ofdividing data intoLEFT and RIGHTgroups:
The LEFT group willhave elements less thanthe pivot.
The RIGHT group willhave elements greater thanthe pivot.
Use markers left & right
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 29 / 45
Internal Sorting
Quick Sort - Step 3
Step 3
If left element belongs toLEFT, then incrementleft index.
If right element belongs to
RIGHT, then decrementright index.
Exchange when you findelements that belong tothe other group.
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 30 / 45
Internal Sorting
Quick Sort - Step 4
Step 4
Element 33 belongsto RIGHT group
Element 22 belongsto LEFT group
Exchange the twoelements.
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 31 / 45
Internal Sorting
Quick Sort - Step 5
Step 5
After the exchange,
increment left marker,
decrement right marker
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 32 / 45
Internal Sorting
Quick Sort - Step 6
Step 6
Element 35 belongsto RIGHT group
Element 12 belongsto LEFT group
Exchange the two elements
Increment leftand decrement right.
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 33 / 45
Internal Sorting
Quick Sort - Step 7
Step 7
Element 29 belongsto RIGHT group
Element 19 belongsto LEFT group
Exchange the two elements
Increment leftand decrement right.
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 34 / 45
Internal Sorting
Quick Sort - Step 8
Step 8
When the left and right
markers pass each other,
we are done with the
partition task.
Swap the right with pivot.
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 35 / 45
Internal Sorting
Quick Sort - Step 9
Step 9
Apply Quicksort
to the LEFT and
RIGHT groups,
recursively.
Assemble parts when done
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 36 / 45
Internal Sorting
Quick Sort - Selecting the Pivot
Use the first element as pivot1 if the input is random, then this is ok2 if the input is presorted (or in reverse order)
all the elements go into LEFT (or RIGHT)Choose the pivot randomly
1 generally safe2 random number generation can be expensive
Therefore, Randomly choosing a pivot point, rather than using theleft most element is recommended, if the data to be sorted is notrandom.A compromise solution is to find the median of the first, last, andmiddle elements of the array, and use this for the pivot. This iscalled the median-of-three approach.
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 37 / 45
Internal Sorting by Selection
LESSON 20
Internal Sorting bySelection
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 38 / 45
Internal Sorting by Selection
Sorting by Selection - Selection Sort
The selection sort improves the bubble sort by reducing the
number of swaps necessary.
Algorithm starts from the first element of the array and scans
through the array for a smaller element than the first element.
If found a smaller element, it will be swapped with the first
element.
Then algorithm goes to the second element and scan the array to
find a smaller element than the second element. (Now we have
nothing to do with the first element).
If found we swap the second element with that smaller element.
This process is repeated until the whole array is sorted.
Let’s apply Selection Sort on the Set of unsorted people in the line
that we saw earlier.M. K. A. Ariyaratne Day School 6 Sept 19, 2014 39 / 45
Internal Sorting by Selection
Selection Sort Algorithm(2)
1 Making a pass through all the people
and selecting the shortest one.
2 This shortest player is then swapped
with the first player at position 0.
Now the leftmost player is sorted and
won’t need to be moved again.
3 The next time you pass down the row
of players, you start at player 2
(position 1), and, find the minimum,
swap it with player 2. This process
continues until all the players are
sorted.
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 40 / 45
Internal Sorting by Selection
Selection Sort on Set of Integers
Let’s see how selection sort works on a set of Integers
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 41 / 45
Internal Sorting by Selection
Selection Sort C code
void selectionSort(int numbers[], int array size){
int i, j;int min, temp;for (i = 0; i < array size-1; i++){
min = i;for (j = i+1; j < array size-1; j++){if (numbers[j] < numbers[min])min = j;
}temp = numbers[i];numbers[i] = numbers[min];numbers[min] = temp;
}}
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 42 / 45
Summary
Summary
We have completed 19 Lessons.
To day we learned Recursion and few internal sorting algorithms
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 43 / 45
References
References
If you are interested in Data Structures, visit the following
resources.
1 Data Structures and Algorithms in java by Robert Lafore.
2 http://www.programmingsimplified.com/c-program-examples
3 http://www.cse.ust.hk/~horner/comp104/
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 44 / 45
Thank You
M. K. A. Ariyaratne Day School 6 Sept 19, 2014 45 / 45