Sei sulla pagina 1di 20

Week 11: Merge, Quick Sort and

Tree Data Structure


AAIN Eka Karyawati
Computer Science, UNUD
Divide and Conquer
• Recursive in structure
– Divide the problem into sub-problems that
are similar to the original but smaller in size
– Conquer the sub-problems by solving
them recursively. If they are small enough,
just solve them in a straightforward
manner.
– Combine the solutions to create a solution
to the original problem
An Example: Merge Sort
Sorting Problem: Sort a sequence of n
elements into non-decreasing order.

• Divide: Divide the n-element sequence to be


sorted into two subsequences of n/2
elements each
• Conquer: Sort the two subsequences
recursively using merge sort.
• Combine: Merge the two sorted
subsequences to produce the sorted answer.
Merge Sort – Example
Original Sequence Sorted Sequence
18 26 32 6 43 15 9 1 1 6 9 15 18 26 32 43

18 26 32 6 43 15 9 1 6 18 26 32 1 9 15 43
43

18 26 32 6 43 15 9 1 18 26 6 32 15 43 1 9

18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1

18 26 32 6 43 15 9 1

Comp 122
Merge Sort Function in C
void MergeSort(int data[], int l, int r)
{
if (l < r)
{
int m = (l+r)/2;
MergeSort(data, l, m);
MergeSort(data, m+1, r);
Merge(data, l, m, r);
}
}
Merge of Merge Sort Function in C
void Merge(int data[], int l, int m, int r) {
int i, j, k; int n1 = m - l + 1; int n2 = r - m; int
L[n1], R[n2];
for (i = 0; i < n1; i++) L[i] = data[l + i];
for (j = 0; j < n2; j++) R[j] = data[m + 1+ j];
i=0; j=0; k=l;
while (i < n1 && j < n2){
if (L[i] <= R[j]) {data[k] = L[i]; i++;}
else {data[k] = R[j]; j++;}
k++; }
while (i < n1){data[k] = L[i]; i++; k++;}
while (j < n2){data[k] = R[j]; j++; k++;}
}
Quick Sort: Main Idea
1. If the number of elements in S is 0 or 1, then
return (base case).
2. Pick any element v in S (called the pivot).
3. Partition the elements in S except v into two
disjoint groups:
1. S1 = {x S – {v} | x  v}
2. S2 = {x S – {v} | x  v}
4. Return {QuickSort(S1) + v + QuickSort(S2)}
Quick Sort: Example
Example of Quick Sort...
Issues To Consider
• How to pick the pivot?
– Many methods (discussed later)
• How to partition?
– Several methods exist.
– The one we consider is known to give good results and
to be easy and efficient.
– We discuss the partition strategy first.
Partitioning Strategy
• pivot = A[right]
• We want to partition array A[left .. right].
• Let i = left-1, j = left)

5 5 7 4 19 3 12 6

i j
Partitioning Strategy
• Want to have
– A[k]  pivot, for k < i
– A[k]  pivot, for k > j
• While j <= right-1
– Move j right, skipping over elements greater than the pivot
– If A[j]  pivot 
increment i, then
A[i] and A[j] is swapped
Quick Sort Function in C
void QuickSort(int data[], int l, int r) {

}
Tree Terminology
Root node (R): the topmost Degree of a tree: the maximum
node in the tree. If R = NULL, number of its node.
then it means the tree is empty. Subtree: tree consisting of a node
Sub-trees: T1, T2, and T3 are and its descendants
called the sub-trees of R.
Leaf node: a that has no
children is called the leaf node
or the terminal node.
Path: a sequence of consecutive
edges is called a path.
Ancestor node : an ancestor
of a node is any predecessor
node on the path from root to
that node.
Types of Trees
Binary Tree
Traversing a Binary Tree
Pre-Order Traversal
In-Order Traversal
Post-Order Traversal
Group Assignments
1. Searching by Hash Table (Array)
2. Sorting by Merge Sort (Single Linked List)
3. Sorting by Quick Sort (Single Linked List)
4. Searching by Binary Tree (Double Linked
List)
5. Searching by Graph-DFS (Array)
6. Searching by Graph-DFS (Single Linked List)
7. Searching by Graph-BFS (Array)
8. Searching by Graph-BFS (Single Linked List)
9. Converting Infix to Prefix and Infix to Postfix
Expression Using Stack (Single Linked List)

Potrebbero piacerti anche