ITEC2150 Sort
ITEC2150 Sort
1
1. Implement GCD(m,n) and test it with gcd(8,12) and gcd (24, 6)
2. Write a recursive method to compute the following series and test
it with m(7) and m(13)
m(i) = 1 + ½ + 1/3 + ….+ 1/i
3. Write a recursive method that display a string input reversel
public static int bin2Dec(string bunaryString)
4. Write a recursive method that parse binary number as a string into
decimal integer. Test it with a driver program
public static int bin2Dec(string bunaryString)
2
WHY STUDY SORTING?
Sorting is a classic subject in computer science. There are three
reasons for studying sorting algorithms.
5
animation
INSERTION SORT ANIMATION
https://liveexample.pearsoncmg.com/dsanimation/Insertio
nSortNeweBook.html
6
animation
INSERTION SORT
int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted
2 9 5 4 8 1 6
2 9 5 4 8 1 6
2 5 9 4 8 1 6
2 4 5 9 8 1 6
2 4 5 8 9 1 6
1 2 4 5 8 9 6
1 2 4 5 6 8 9
7
HOW TO INSERT?
8
FROM IDEA TO SOLUTION
for (int i = 1; i < list.length; i++) {
insert list[i] into a sorted sublist list[0..i-1] so that
list[0..i] is sorted
}
list[0]
list[0] list[1]
9
FROM IDEA TO SOLUTION
for (int i = 1; i < list.length; i++) {
insert list[i] into a sorted sublist list[0..i-1] so that
list[0..i] is sorted
}
Expand
double currentElement = list[i];
int k;
for (k = i - 1; k >= 0 && list[k] > currentElement; k--) {
list[k + 1] = list[k];
}
// Insert the current element into list[k + 1]
list[k + 1] = currentElement;
InsertionSort Run
10
Open word or txt file and do the followings. You will submit the
file as a classwork of 04/14/2020.
11
BUBBLE SORT
BubbleSort Run
12
BUBBLE SORT ANIMATION
https://liveexample.pearsoncmg.com/dsanimation/BubbleSortNeweBoo
k.html
13
Continue on the file for classwork
1. Describe how an bubble sort works? What is the time
complexity of the bubble sort?
2. Using the example on slide 13, show hoe to apply an bubble
sort on (45, 11, 50, 59, 60,2,4,7,10).
3. If a list is already sorted, how many comparison will the bubble
sort method perform?
14
MERGE SORT
n/2
4
15
MergeSort Run
MERGE SORT
mergeSort(list):
firstHalf = mergeSort(firstHalf);
secondHalf = mergeSort(secondHalf);
list = merge(firstHalf, secondHalf);
16
MERGE TWO SORTED LISTS
18
MERGE SORT TIME
The first T(n/2) is the time for sorting the first half of the array and
the second T(n/2) is the time for sorting the second half. To merge
two subarrays, it takes at most n-1 comparisons to compare the
elements from the two subarrays and n moves to move elements to
the temporary array. So, the total time is 2n-1. Therefore,
IB M
864 24
1024 0124 * 1024 10240
19
1. Describe how an Merge sort works? What is the time
complexity of the merge sort?
2. Using the example on slide 14, show hoe to apply an merge sort
on (45, 11, 50, 59, 60,2,4,7,10).
3. What is wrong if line 6-15 of slide 20, red colored lines, are
replaced by the following code?
20
public class MergeSort { while (current1 < list1.length && current2 < list2.length) {
/** The method for sorting the numbers */ if (list1[current1] < list2[current2])
public static void mergeSort(int[] list) { temp[current3++] = list1[current1++];
if (list.length > 1) { else
// Merge sort the first half temp[current3++] = list2[current2++];
int[] firstHalf = new int[list.length / 2]; }
System.arraycopy(list, 0, firstHalf, 0, list.length / 2);
mergeSort(firstHalf); while (current1 < list1.length)
temp[current3++] = list1[current1++];
// Merge sort the second half
int secondHalfLength = list.length - list.length / 2; while (current2 < list2.length)
int[] secondHalf = new int[secondHalfLength]; temp[current3++] = list2[current2++];
System.arraycopy(list, list.length / 2, }
secondHalf, 0, secondHalfLength);
mergeSort(secondHalf); /** A test method */
public static void main(String[] args) {
// Merge firstHalf with secondHalf into list int[] list = {2, 3, 2, 5, 6, 1, -2, 3, 14, 12};
merge(firstHalf, secondHalf, list); mergeSort(list);
} for (int i = 0; i < list.length; i++)
} System.out.print(list[i] + " ");
}
/** Merge two sorted lists */ }
public static void merge(int[] list1, int[] list2, int[] temp) {
int current1 = 0; // Current index in list1
int current2 = 0; // Current index in list2
int current3 = 0; // Current index in temp
21
QUICK SORT
Quick sort, developed by C. A. R. Hoare (1962), works as follows: The
algorithm selects an element, called the pivot, in the array. Divide the
array into two parts such that all the elements in the first part are less
than or equal to the pivot and all the elements in the second part are
greater than the pivot. Recursively apply the quick sort algorithm to
the first part and then the second part.
22
QUICK SORT
23
PARTITION
Animation for
partition
QuickSort Run 24
QUICK SORT TIME
To partition an array of n elements, it takes n-1 comparisons and n
moves in the worst case. So, the time required for partition is O(n).
25
WORST-CASE TIME
In the worst case, each time the pivot divides the
array into one big subarray with the other empty.
The size of the big subarray is one less than the
one before divided. The algorithm requires
time:
26
BEST-CASE TIME
27
AVERAGE-CASE TIME
28
1. Describe how an Quicksort works? What is the time
complexity of the quick sort?
2. Why is quick sort more space efficient than quick sort?
2. Using the example on slide 22, show hoe to apply an
quick sort on (45, 11, 50, 59, 60,2,4,7,10).
3. If line 37-38 of slide 29, red colored line, in the quick
sort program is removed, will it still Give a counter
example to show that it will not work.
29
public class QuickSort { while (high > first && list[high] >= pivot)
public static void quickSort(int[] list) { high--;
quickSort(list, 0, list.length - 1);
}
// Swap pivot with list[high]
if (pivot > list[high]) {
private static void quickSort(int[] list, int first, int last) {
list[first] = list[high];
if (last > first) {
list[high] = pivot;
int pivotIndex = partition(list, first, last);
return high;
quickSort(list, first, pivotIndex - 1);
}
quickSort(list, pivotIndex + 1, last);
else {
}
return first;
}
}
}
/** Partition the array list[first..last] */
private static int partition(int[] list, int first, int last) {
/** A test method */
int pivot = list[first]; // Choose the first element as the pivot
public static void main(String[] args) {
int low = first + 1; // Index for forward search
int[] list = {2, 3, 2, 5, 6, 1, -2, 3, 14, 12};
int high = last; // Index for backward search quickSort(list);
for (int i = 0; i < list.length; i++)
while (high > low) { System.out.print(list[i] + " ");
// Search forward from left }
while (low <= high && list[low] <= pivot) }
low++;
31
COMPLETE BINARY TREE
A binary tree is complete if every level of the tree is full
except that the last level may not be full and all the leaves
on the last level are placed left-most. For example, in the
following figure, the binary trees in (a) and (b) are
complete, but the binary trees in (c) and (d) are not
complete. Further, the binary tree in (a) is a heap, but the
binary tree in (b) is not a heap, because the root (39) is less
than its right child (42).
32
SEE HOW A HEAP WORKS
https://liveexample.pearsoncmg.com/dsanimation/HeapeBook.html
33
REPRESENTING A HEAP
For a node at position i, its left child is at position 2i+1 and
its right child is at position 2i+2, and its parent is at index
(i-1)/2. For example, the node for element 39 is at position
4, so its left child (element 14) is at 9 (2*4+1), its right child
(element 33) is at 10 (2*4+2), and its parent (element 42) is
at 1 ((4-1)/2).
34
ADDING ELEMENTS TO THE HEAP
35
REBUILD THE HEAP AFTER ADDING A NEW
NODE
36
REMOVING THE ROOT AND REBUILD THE TREE
Removing root 62 from the heap
37
REMOVING THE ROOT AND REBUILD THE TREE
Move 9 to root
38
REMOVING THE ROOT AND REBUILD THE TREE
Swap 9 with 59
39
REMOVING THE ROOT AND REBUILD THE TREE
Swap 9 with 44
40
REMOVING THE ROOT AND REBUILD THE TREE
Swap 9 with 30
41
THE HEAP CLASS
Heap
42
HEAP SORT
HeapSort Run
43
HEAP SORT TIME
Let h denote the height for a heap of n elements.
Since a heap is a complete binary tree, the first
level has 1 node, the second level has 2 nodes, the
kth level has 2(k-1) nodes, the (h-1)th level has
2(h-2) nodes, and the hth level has at least one node
and at most 2(h-1) nodes. Therefore,
44
1. What is a complete binary tree? What is heap? Describe how to
remove the root from a heap and how to add a new object to a
heap.
2. What is the return value from invoking the remove method if the
heap is empty?
3. Add the element 4,5,1,2,9,3 into a heap in this order. Draw the
diagrams to show the heap after each element us added.
4. What is the time complexity of inserting a new element into a
heap, and what is the time complexity of deleting an element
from a heap?
5. Show the heap after the root in the heap in the following is
removed. 44, 42,30, 32, 39, 17, 13, 22, 29, 14, 33, 9
6. Show the step creating a heap using 45, 11, 50, 59, 60, 2, 4, 7, 10
7. Next page
45
7. Given the following heap, show the steps of removing all nodes from the heap. Draw
complete tree first.
62. 42,
8. Which of the following statements are wrong?
8. Heap<Object> heap1 = new Heap<>()
9. Heap<Number> heap2 = new Heap<>()
10. Heap<BigInteger> heap2 = new Heap<>()
11. Heap<Calendar> heap2 = new Heap<>()
12. Heap<String> heap2 = new Heap<>()
9. What is the height of nonempty heap? What is the height of a heap with 16, 17, and
512 elements? If the height of a heap is 5, what is the maximum number of nodes in the
heap?
46
BUCKET SORT AND RADIX SORT
All sort algorithms discussed so far are general sorting algorithms that
work for any types of keys (e.g., integers, strings, and any comparable
objects). These algorithms sort the elements by comparing their keys.
The lower bound for general sorting algorithms is O(nlogn). So, no
sorting algorithms based on comparisons can perform better than
O(nlogn). However, if the keys are small integers, you can use bucket
sort without having to compare the keys.
47
BUCKET SORT
The bucket sort algorithm works as follows. Assume the keys are in the
range from 0 to N-1. We need N buckets labeled 0, 1, ..., and N-1. If
an element’s key is i, the element is put into the bucket i. Each bucket
holds the elements with the same key value. You can use an ArrayList to
implement a bucket.
48
RADIX SORT
Sort 331, 454, 230, 34, 343, 45, 59, 453, 345, 231, 9
230, 331, 231, 343, 453, 454, 34, 45, 345, 59, 9
49
9, 34, 45, 59, 230, 231, 331, 343, 345, 453, 454
RADIX SORT ANIMATION
https://liveexample.pearsoncmg.com/dsanimation/RadixSorteBook.ht
ml
50
1. Can you sort a list of string using a bucket sort?
2. Show how the radix sort works using the number
454, 34, 23, 43, 74, 66, and 76
51
Homework
1. Write the following two generic methods using a bubble
sort. The first method sorts the elements using the
Comparable interface, and the second uses the Comparater
interface.
public static <E extends Comparable<E>>
void bubbleSort(E[] list)
public static <E> void bubbleSort(E[] list,
comparator<? Super E> Comparator)
2. The quick sort algorithm presented in the book select the
first elements in the list as a pivot, Revise it by selecting the
median among the first middle and last elements in the list.
52
EXTERNAL SORT
All the sort algorithms discussed in the preceding sections assume
that all data to be sorted is available at one time in internal
memory such as an array. To sort data stored in an external file,
you may first bring data to the memory, then sort it internally.
However, if the file is too large, all data in the file cannot be
brought to memory at one time.
CreateLargeFile Run
53
SortLargeFile Run
PHASE I
Repeatedly bring data from the file to an array, sort the array using
an internal sorting algorithm, and output the data from the array to
a temporary file.
54
PHASE II
Merge a pair of sorted segments (e.g., S1 with S2, S3 with S4, ...,
and so on) into a larger sorted segment and save the new segment
into a new temporary file. Continue the same process until one sorted
segment results.
55
IMPLEMENTING PHASE II
Each merge step merges two sorted segments to form a new segment. The new
segment doubles the number elements. So the number of segments is reduced by
half after each merge step. A segment is too large to be brought to an array in
memory. To implement a merge step, copy half number of segments from file f1.dat
to a temporary file f2.dat. Then merge the first remaining segment in f1.dat with
the first segment in f2.dat into a temporary file named f3.dat.
56
IMPLEMENTING PHASE II
57