SlideShare a Scribd company logo
1 of 43
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
1
Chapter 23 Sorting
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
2
why study sorting?
Sorting is a classic subject in computer science. There are three
reasons for studying sorting algorithms.
– First, sorting algorithms illustrate many creative
approaches to problem solving and these approaches can
be applied to solve other problems.
– Second, sorting algorithms are good for practicing
fundamental programming techniques using selection
statements, loops, methods, and arrays.
– Third, sorting algorithms are excellent examples to
demonstrate algorithm performance.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
3
what data to sort?
The data to be sorted might be integers, doubles, characters, or
objects. §7.8, “Sorting Arrays,” presented selection sort and
insertion sort for numeric values. The selection sort algorithm
was extended to sort an array of objects in §11.5.7, “Example:
Sorting an Array of Objects.” The Java API contains several
overloaded sort methods for sorting primitive type values and
objects in the java.util.Arrays and java.util.Collections class. For
simplicity, this section assumes:
 data to be sorted are integers,
 data are sorted in ascending order, and
 data are stored in an array. The programs can be easily
modified to sort other types of data, to sort in descending
order, or to sort data in an ArrayList or a LinkedList.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
4
Insertion Sort
int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted
The insertion sort
algorithm sorts a list
of values by
repeatedly inserting
an unsorted element
into a sorted sublist
until the whole list
is sorted.
2 9 5 4 8 1 6
Step 1: Initially, the sorted sublist contains the
first element in the list. Insert 9 into the sublist.
2 9 5 4 8 1 6
Step2: The sorted sublist is {2, 9}. Insert 5 into
the sublist.
2 5 9 4 8 1 6
Step 3: The sorted sublist is {2, 5, 9}. Insert 4
into the sublist.
2 4 5 9 8 1 6
Step 4: The sorted sublist is {2, 4, 5, 9}. Insert 8
into the sublist.
2 4 5 8 9 1 6
Step 5: The sorted sublist is {2, 4, 5, 8, 9}. Insert
1 into the sublist.
1 2 4 5 8 9 6
Step 6: The sorted sublist is {1, 2, 4, 5, 8, 9}.
Insert 6 into the sublist.
1 2 4 5 6 8 9
Step 7: The entire list is now sorted.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
5
http://www.cs.armstrong.edu/liang/animation/web/Insertio
nSort.html
Insertion Sort Animation
animation
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
6
Insertion Sort
2 9 5 4 8 1 6
2 9 5 4 8 1 6
2 5 9 4 8 1 6
2 4 5 8 9 1 6
1 2 4 5 8 9 6
2 4 5 9 8 1 6
1 2 4 5 6 8 9
int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted
animation
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
7
How to Insert?
The insertion sort
algorithm sorts a list
of values by
repeatedly inserting
an unsorted element
into a sorted sublist
until the whole list
is sorted.
[0] [1] [2] [3] [4] [5] [6]
2 5 9 4
list Step 1: Save 4 to a temporary variable currentElement
[0] [1] [2] [3] [4] [5] [6]
2 5 9
list Step 2: Move list[2] to list[3]
[0] [1] [2] [3] [4] [5] [6]
2 5 9
list Step 3: Move list[1] to list[2]
[0] [1] [2] [3] [4] [5] [6]
2 4 5 9
list Step 4: Assign currentElement to list[1]
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
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]
list[0] list[1] list[2]
list[0] list[1] list[2] list[3]
list[0] list[1] list[2] list[3] ...
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
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;
Run
InsertSort
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
10
Bubble Sort
2 5 9 4 8 1
2 5 4 9 8 1
2 5 4 8 9 1
2 5 4 8 1 9
(a) 1st pass
2 4 5 8 1 9
2 4 5 8 1 9
2 4 5 1 8 9
(b) 2nd pass
2 4 5 1 8 9
2 4 1 5 8 9
(c) 3rd pass
2 1 4 5 8 9
(d) 4th pass
2 9 5 4 8 1
(e) 5th pass
2 5 4 8 1 9 2 4 5 1 8 9 2 4 1 5 8 9 1 2 4 5 8 9
2
2
1
2
...
)
2
(
)
1
(
2
n
n
n
n 







Bubble sort time: O(n2)
Run
BubbleSort
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
11
for (int k = 1; k < list.length && needNextPass; k++) {
// Array may be sorted and next pass not needed
needNextPass = false;
for (int i = 0; i < list.length - k; i++) {
if (list[i] > list[i + 1]) {
// Swap list[i] with list[i + 1]
int temp = list[i];
list[i] = list[i + 1];
list[i + 1] = temp;
needNextPass = true; // Next pass still needed
}
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
12
Bubble Sort Animation
http://www.cs.armstrong.edu/liang/animation/web/BubbleSort.html
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
13
Merge Sort
2 9 5 4 8 1 6 7
2 9 5 4 8 1 6 7
split
2 9
split
5 4
2
split
9 5 4
8 1 6 7
8 1 6 7
2 9
merge
4 5 1 8 6 7
2 4 5 9 1 6 7 8
1 2 4 5 6 7 8 9
merge
merge
divide
conquer
Run
MergeSort
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
14
Merge Sort
mergeSort(list):
firstHalf = mergeSort(firstHalf);
secondHalf = mergeSort(secondHalf);
list = merge(firstHalf, secondHalf);
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
15
Merge Two Sorted Lists
2 4 5 9
current1
1
1 6 7 8
current2
current3
(a) After moving 1 to temp (b) After moving all the
elements in list2 to temp
to temp
2 4 5 9
current1
1 2 4 5 6 7 8 9
1 6 7 8
current2
current3
(c) After moving 9 to
temp
2 4 5 9
current1
1 2 4 5 6 7 8
1 6 7 8
current2
current3
Animation for Merging Two Sorted Lists
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
16
Merge Sort Time
Let T(n) denote the time required for sorting an
array of n elements using merge sort. Without loss
of generality, assume n is a power of 2. The merge
sort algorithm splits the array into two subarrays,
sorts the subarrays using the same algorithm
recursively, and then merges the subarrays. So,
mergetime
n
T
n
T
n
T 

 )
2
(
)
2
(
)
(
)
(
)
2
(
)
2
(
)
( n
O
n
T
n
T
n
T 


Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
17
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.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
18
Quick Sort
5 2 9 3 8 4 0 1 6 7
pivot
(a) The original array
4 2 1 3 0 5 8 9 6 7
pivot
(b)The original array is partitioned
0 2 1 3 4
(c) The partial array (4 2 1 3 0) is
partitioned
0 2 1 3 (d) The partial array (0 2 1 3) is
partitioned
1 2 3
pivot
pivot
pivot
(e) The partial array (2 1 3) is
partitioned
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
19
Partition 5 2 9 3 8 4 0 1 6 7
pivot low high
(a) Initialize pivot, low, and high
5 2 9 3 8 4 0 1 6 7
pivot low high
(b) Search forward and backward
5 2 1 3 8 4 0 9 6 7
pivot low high
(c) 9 is swapped with 1
5 2 1 3 8 4 0 9 6 7
pivot low high
(d) Continue search
5 2 1 3 0 4 8 9 6 7
pivot low high
(e) 8 is swapped with 0
5 2 1 3 0 4 8 9 6 7
pivot low high
(f) when high < low, search is over
4 2 1 3 0 5 8 9 6 7
pivot
(g) pivot is in the right place
The index of the pivot is returned
Animation for
partition
Run
QuickSort
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
20
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).
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
21
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:
)
(
1
2
...
)
2
(
)
1
( 2
n
O
n
n 






)
( 2
n
O
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
22
Best-Case Time
In the best case, each time the pivot divides the
array into two parts of about the same size. Let
T(n) denote the time required for sorting an array
of elements using quick sort. So,
)
log
(
)
2
(
)
2
(
)
( n
n
O
n
n
T
n
T
n
T 



Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
23
Average-Case Time
On the average, each time the pivot will not
divide the array into two parts of the same size
nor one empty part. Statistically, the sizes of the
two parts are very close. So the average time is
O(nlogn). The exact average-case analysis is
beyond the scope of this book.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
24
Heap
Heap is a useful data structure for designing efficient
sorting algorithms and priority queues. A heap is a binary
tree with the following properties:
It is a complete binary tree.
Each node is greater than or equal to any of its children.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
25
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).
22 29 14 33
32 39
42
22 29 14
32 42
39
22 14 33
32 39
42
22 29
32
42
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
26
See How a Heap Works
http://www.cs.armstrong.edu/liang/animation/web/Heap.html
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
27
Adding Elements to the Heap
Adding 3, 5, 1, 19, 11, and 22 to a heap, initially empty
3
(a) After adding 3 (b) After adding 5 (c) After adding 1
(d) After adding 19
3
19
5 1
(e) After adding 11
3 5
19
11 1
(f) After adding 22
3 5 1
22
11 19
5
3
5
3 1
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
28
Rebuild the heap after adding a new node
Adding 88 to the heap
(a) Add 88 to a heap
3 5 1 88
22
11 19
(b) After swapping 88 with 19
3 5 1 19
22
11 88
(b) After swapping 88 with 22
3 5 1 19
88
11 22
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
29
Removing the Root and Rebuild the Tree
22 29 14 33 30 17 9
32 39 44 13
42 59
62
Removing root 62 from the heap
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
30
Removing the Root and Rebuild the Tree
22 29 14 33 30 17
32 39 44 13
42 59
9
Move 9 to root
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
31
Removing the Root and Rebuild the Tree
22 29 14 33 30 17
32 39 44 13
42 9
59
Swap 9 with 59
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
32
Removing the Root and Rebuild the Tree
22 29 14 33 30 17
32 39 9 13
42 44
59
Swap 9 with 44
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
33
Removing the Root and Rebuild the Tree
22 29 14 33 9 17
32 39 30 13
42 44
59
Swap 9 with 30
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
34
The Heap Class
Heap
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
35
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).
22 29 14 33 30 17 9
32 39 44 13
42 59
62
62 42 59 32 39 44 13 22 29 14 33 30 17 9
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10][11][12][13]
[10][11]
parent left
right
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
36
/** Add a new object into the heap */
public void add(E newObject) {
list.add(newObject); // Append to the heap
int currentIndex = list.size() - 1; // The index of the last node
while (currentIndex > 0) {
int parentIndex = (currentIndex - 1) / 2;
// Swap if the current object is greater than its parent
if (list.get(currentIndex).compareTo(
list.get(parentIndex)) > 0) {
E temp = list.get(currentIndex);
list.set(currentIndex, list.get(parentIndex));
list.set(parentIndex, temp);
}
else
break; // the tree is a heap now
currentIndex = parentIndex;
}
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
37
/** Remove the root from the heap */
public E remove() {
if (list.size() == 0) return null;
E removedObject = list.get(0);
list.set(0, list.get(list.size() - 1)); //copy the last object to the root
list.remove(list.size() - 1); //remove the last object
int currentIndex = 0;
while (currentIndex < list.size()) {
int leftChildIndex = 2 * currentIndex + 1;
int rightChildIndex = 2 * currentIndex + 2;
// Find the maximum between two children
if (leftChildIndex >= list.size()) break; // The tree is a heap
int maxIndex = leftChildIndex;
if (rightChildIndex < list.size()) {
if (list.get(maxIndex).compareTo(list.get(rightChildIndex)) < 0) {
maxIndex = rightChildIndex;
}
}
// Swap if the current node is less than the maximum
if (list.get(currentIndex).compareTo(list.get(maxIndex)) < 0) {
E temp = list.get(maxIndex);
list.set(maxIndex, list.get(currentIndex));
list.set(currentIndex, temp);
currentIndex = maxIndex;
}
else
break; // The tree is a heap
}
return removedObject;
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
38
Heap Sort
Run
HeapSort
public static <E extends Comparable<E>> void heapSort(E[] list) {
// Create a Heap of integers
Heap<E> heap = new Heap<>();
// Add elements to the heap
for (int i = 0; i < list.length; i++)
heap.add(list[i]);
// Remove elements from the heap
for (int i = list.length - 1; i >= 0; i--)
list[i] = heap.remove();
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
39
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,
1
2
2
2
2
...
2
1
2
...
2
1 










 h
h
h
n
1
2
1
2 1




 h
h
n
1
)
1
log(
)
1
log( 



 n
h
n
h
h
n 2
1
2 1



 h
h
n 2
log
)
1
log(
2
log 1




h
n
h 


 )
1
log(
1
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
40
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.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
41
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.
…
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
42
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
9, 230, 331, 231, 34, 343, 45, 345, 453, 454, 59
9, 34, 45, 59, 230, 231, 331, 343, 345, 453, 454
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
43
Radix Sort Animation
http://www.cs.armstrong.edu/liang/animation/web/RadixSort.html

More Related Content

Similar to Lecture_14Sorting.pptx

Similar to Lecture_14Sorting.pptx (20)

14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
Python - Lecture 10
Python - Lecture 10Python - Lecture 10
Python - Lecture 10
 
Java™ (OOP) - Chapter 6: "Arrays"
Java™ (OOP) - Chapter 6: "Arrays"Java™ (OOP) - Chapter 6: "Arrays"
Java™ (OOP) - Chapter 6: "Arrays"
 
Autoboxing and unboxing
Autoboxing and unboxingAutoboxing and unboxing
Autoboxing and unboxing
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rath
 
Computer notes data structures - 9
Computer notes   data structures - 9Computer notes   data structures - 9
Computer notes data structures - 9
 
06slide
06slide06slide
06slide
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Queue
QueueQueue
Queue
 
sorting.pptx
sorting.pptxsorting.pptx
sorting.pptx
 
Queue- 8 Queen
Queue- 8 QueenQueue- 8 Queen
Queue- 8 Queen
 
U3.stack queue
U3.stack queueU3.stack queue
U3.stack queue
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
Sorting
SortingSorting
Sorting
 
lecture-k-sorting.ppt
lecture-k-sorting.pptlecture-k-sorting.ppt
lecture-k-sorting.ppt
 
3.ppt
3.ppt3.ppt
3.ppt
 
Lecture k-sorting
Lecture k-sortingLecture k-sorting
Lecture k-sorting
 
Icom4015 lecture13-f16
Icom4015 lecture13-f16Icom4015 lecture13-f16
Icom4015 lecture13-f16
 
Merge sort analysis and its real time applications
Merge sort analysis and its real time applicationsMerge sort analysis and its real time applications
Merge sort analysis and its real time applications
 

Recently uploaded

Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...Call Girls in Nagpur High Profile
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 

Recently uploaded (20)

Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 

Lecture_14Sorting.pptx

  • 1. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 23 Sorting
  • 2. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 2 why study sorting? Sorting is a classic subject in computer science. There are three reasons for studying sorting algorithms. – First, sorting algorithms illustrate many creative approaches to problem solving and these approaches can be applied to solve other problems. – Second, sorting algorithms are good for practicing fundamental programming techniques using selection statements, loops, methods, and arrays. – Third, sorting algorithms are excellent examples to demonstrate algorithm performance.
  • 3. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 what data to sort? The data to be sorted might be integers, doubles, characters, or objects. §7.8, “Sorting Arrays,” presented selection sort and insertion sort for numeric values. The selection sort algorithm was extended to sort an array of objects in §11.5.7, “Example: Sorting an Array of Objects.” The Java API contains several overloaded sort methods for sorting primitive type values and objects in the java.util.Arrays and java.util.Collections class. For simplicity, this section assumes:  data to be sorted are integers,  data are sorted in ascending order, and  data are stored in an array. The programs can be easily modified to sort other types of data, to sort in descending order, or to sort data in an ArrayList or a LinkedList.
  • 4. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 4 Insertion Sort int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted The insertion sort algorithm sorts a list of values by repeatedly inserting an unsorted element into a sorted sublist until the whole list is sorted. 2 9 5 4 8 1 6 Step 1: Initially, the sorted sublist contains the first element in the list. Insert 9 into the sublist. 2 9 5 4 8 1 6 Step2: The sorted sublist is {2, 9}. Insert 5 into the sublist. 2 5 9 4 8 1 6 Step 3: The sorted sublist is {2, 5, 9}. Insert 4 into the sublist. 2 4 5 9 8 1 6 Step 4: The sorted sublist is {2, 4, 5, 9}. Insert 8 into the sublist. 2 4 5 8 9 1 6 Step 5: The sorted sublist is {2, 4, 5, 8, 9}. Insert 1 into the sublist. 1 2 4 5 8 9 6 Step 6: The sorted sublist is {1, 2, 4, 5, 8, 9}. Insert 6 into the sublist. 1 2 4 5 6 8 9 Step 7: The entire list is now sorted.
  • 5. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 5 http://www.cs.armstrong.edu/liang/animation/web/Insertio nSort.html Insertion Sort Animation animation
  • 6. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 6 Insertion Sort 2 9 5 4 8 1 6 2 9 5 4 8 1 6 2 5 9 4 8 1 6 2 4 5 8 9 1 6 1 2 4 5 8 9 6 2 4 5 9 8 1 6 1 2 4 5 6 8 9 int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted animation
  • 7. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 7 How to Insert? The insertion sort algorithm sorts a list of values by repeatedly inserting an unsorted element into a sorted sublist until the whole list is sorted. [0] [1] [2] [3] [4] [5] [6] 2 5 9 4 list Step 1: Save 4 to a temporary variable currentElement [0] [1] [2] [3] [4] [5] [6] 2 5 9 list Step 2: Move list[2] to list[3] [0] [1] [2] [3] [4] [5] [6] 2 5 9 list Step 3: Move list[1] to list[2] [0] [1] [2] [3] [4] [5] [6] 2 4 5 9 list Step 4: Assign currentElement to list[1]
  • 8. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 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] list[0] list[1] list[2] list[0] list[1] list[2] list[3] list[0] list[1] list[2] list[3] ...
  • 9. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 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; Run InsertSort
  • 10. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 10 Bubble Sort 2 5 9 4 8 1 2 5 4 9 8 1 2 5 4 8 9 1 2 5 4 8 1 9 (a) 1st pass 2 4 5 8 1 9 2 4 5 8 1 9 2 4 5 1 8 9 (b) 2nd pass 2 4 5 1 8 9 2 4 1 5 8 9 (c) 3rd pass 2 1 4 5 8 9 (d) 4th pass 2 9 5 4 8 1 (e) 5th pass 2 5 4 8 1 9 2 4 5 1 8 9 2 4 1 5 8 9 1 2 4 5 8 9 2 2 1 2 ... ) 2 ( ) 1 ( 2 n n n n         Bubble sort time: O(n2) Run BubbleSort
  • 11. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 11 for (int k = 1; k < list.length && needNextPass; k++) { // Array may be sorted and next pass not needed needNextPass = false; for (int i = 0; i < list.length - k; i++) { if (list[i] > list[i + 1]) { // Swap list[i] with list[i + 1] int temp = list[i]; list[i] = list[i + 1]; list[i + 1] = temp; needNextPass = true; // Next pass still needed } }
  • 12. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 12 Bubble Sort Animation http://www.cs.armstrong.edu/liang/animation/web/BubbleSort.html
  • 13. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 13 Merge Sort 2 9 5 4 8 1 6 7 2 9 5 4 8 1 6 7 split 2 9 split 5 4 2 split 9 5 4 8 1 6 7 8 1 6 7 2 9 merge 4 5 1 8 6 7 2 4 5 9 1 6 7 8 1 2 4 5 6 7 8 9 merge merge divide conquer Run MergeSort
  • 14. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 14 Merge Sort mergeSort(list): firstHalf = mergeSort(firstHalf); secondHalf = mergeSort(secondHalf); list = merge(firstHalf, secondHalf);
  • 15. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 15 Merge Two Sorted Lists 2 4 5 9 current1 1 1 6 7 8 current2 current3 (a) After moving 1 to temp (b) After moving all the elements in list2 to temp to temp 2 4 5 9 current1 1 2 4 5 6 7 8 9 1 6 7 8 current2 current3 (c) After moving 9 to temp 2 4 5 9 current1 1 2 4 5 6 7 8 1 6 7 8 current2 current3 Animation for Merging Two Sorted Lists
  • 16. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 16 Merge Sort Time Let T(n) denote the time required for sorting an array of n elements using merge sort. Without loss of generality, assume n is a power of 2. The merge sort algorithm splits the array into two subarrays, sorts the subarrays using the same algorithm recursively, and then merges the subarrays. So, mergetime n T n T n T    ) 2 ( ) 2 ( ) ( ) ( ) 2 ( ) 2 ( ) ( n O n T n T n T   
  • 17. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 17 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.
  • 18. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 18 Quick Sort 5 2 9 3 8 4 0 1 6 7 pivot (a) The original array 4 2 1 3 0 5 8 9 6 7 pivot (b)The original array is partitioned 0 2 1 3 4 (c) The partial array (4 2 1 3 0) is partitioned 0 2 1 3 (d) The partial array (0 2 1 3) is partitioned 1 2 3 pivot pivot pivot (e) The partial array (2 1 3) is partitioned
  • 19. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 19 Partition 5 2 9 3 8 4 0 1 6 7 pivot low high (a) Initialize pivot, low, and high 5 2 9 3 8 4 0 1 6 7 pivot low high (b) Search forward and backward 5 2 1 3 8 4 0 9 6 7 pivot low high (c) 9 is swapped with 1 5 2 1 3 8 4 0 9 6 7 pivot low high (d) Continue search 5 2 1 3 0 4 8 9 6 7 pivot low high (e) 8 is swapped with 0 5 2 1 3 0 4 8 9 6 7 pivot low high (f) when high < low, search is over 4 2 1 3 0 5 8 9 6 7 pivot (g) pivot is in the right place The index of the pivot is returned Animation for partition Run QuickSort
  • 20. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 20 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).
  • 21. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 21 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: ) ( 1 2 ... ) 2 ( ) 1 ( 2 n O n n        ) ( 2 n O
  • 22. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 22 Best-Case Time In the best case, each time the pivot divides the array into two parts of about the same size. Let T(n) denote the time required for sorting an array of elements using quick sort. So, ) log ( ) 2 ( ) 2 ( ) ( n n O n n T n T n T    
  • 23. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 23 Average-Case Time On the average, each time the pivot will not divide the array into two parts of the same size nor one empty part. Statistically, the sizes of the two parts are very close. So the average time is O(nlogn). The exact average-case analysis is beyond the scope of this book.
  • 24. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 24 Heap Heap is a useful data structure for designing efficient sorting algorithms and priority queues. A heap is a binary tree with the following properties: It is a complete binary tree. Each node is greater than or equal to any of its children.
  • 25. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 25 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). 22 29 14 33 32 39 42 22 29 14 32 42 39 22 14 33 32 39 42 22 29 32 42
  • 26. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 26 See How a Heap Works http://www.cs.armstrong.edu/liang/animation/web/Heap.html
  • 27. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 27 Adding Elements to the Heap Adding 3, 5, 1, 19, 11, and 22 to a heap, initially empty 3 (a) After adding 3 (b) After adding 5 (c) After adding 1 (d) After adding 19 3 19 5 1 (e) After adding 11 3 5 19 11 1 (f) After adding 22 3 5 1 22 11 19 5 3 5 3 1
  • 28. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 28 Rebuild the heap after adding a new node Adding 88 to the heap (a) Add 88 to a heap 3 5 1 88 22 11 19 (b) After swapping 88 with 19 3 5 1 19 22 11 88 (b) After swapping 88 with 22 3 5 1 19 88 11 22
  • 29. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 29 Removing the Root and Rebuild the Tree 22 29 14 33 30 17 9 32 39 44 13 42 59 62 Removing root 62 from the heap
  • 30. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 30 Removing the Root and Rebuild the Tree 22 29 14 33 30 17 32 39 44 13 42 59 9 Move 9 to root
  • 31. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 31 Removing the Root and Rebuild the Tree 22 29 14 33 30 17 32 39 44 13 42 9 59 Swap 9 with 59
  • 32. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 32 Removing the Root and Rebuild the Tree 22 29 14 33 30 17 32 39 9 13 42 44 59 Swap 9 with 44
  • 33. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 33 Removing the Root and Rebuild the Tree 22 29 14 33 9 17 32 39 30 13 42 44 59 Swap 9 with 30
  • 34. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 34 The Heap Class Heap
  • 35. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 35 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). 22 29 14 33 30 17 9 32 39 44 13 42 59 62 62 42 59 32 39 44 13 22 29 14 33 30 17 9 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10][11][12][13] [10][11] parent left right
  • 36. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 36 /** Add a new object into the heap */ public void add(E newObject) { list.add(newObject); // Append to the heap int currentIndex = list.size() - 1; // The index of the last node while (currentIndex > 0) { int parentIndex = (currentIndex - 1) / 2; // Swap if the current object is greater than its parent if (list.get(currentIndex).compareTo( list.get(parentIndex)) > 0) { E temp = list.get(currentIndex); list.set(currentIndex, list.get(parentIndex)); list.set(parentIndex, temp); } else break; // the tree is a heap now currentIndex = parentIndex; } }
  • 37. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 37 /** Remove the root from the heap */ public E remove() { if (list.size() == 0) return null; E removedObject = list.get(0); list.set(0, list.get(list.size() - 1)); //copy the last object to the root list.remove(list.size() - 1); //remove the last object int currentIndex = 0; while (currentIndex < list.size()) { int leftChildIndex = 2 * currentIndex + 1; int rightChildIndex = 2 * currentIndex + 2; // Find the maximum between two children if (leftChildIndex >= list.size()) break; // The tree is a heap int maxIndex = leftChildIndex; if (rightChildIndex < list.size()) { if (list.get(maxIndex).compareTo(list.get(rightChildIndex)) < 0) { maxIndex = rightChildIndex; } } // Swap if the current node is less than the maximum if (list.get(currentIndex).compareTo(list.get(maxIndex)) < 0) { E temp = list.get(maxIndex); list.set(maxIndex, list.get(currentIndex)); list.set(currentIndex, temp); currentIndex = maxIndex; } else break; // The tree is a heap } return removedObject; }
  • 38. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 38 Heap Sort Run HeapSort public static <E extends Comparable<E>> void heapSort(E[] list) { // Create a Heap of integers Heap<E> heap = new Heap<>(); // Add elements to the heap for (int i = 0; i < list.length; i++) heap.add(list[i]); // Remove elements from the heap for (int i = list.length - 1; i >= 0; i--) list[i] = heap.remove(); }
  • 39. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 39 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, 1 2 2 2 2 ... 2 1 2 ... 2 1             h h h n 1 2 1 2 1      h h n 1 ) 1 log( ) 1 log(      n h n h h n 2 1 2 1     h h n 2 log ) 1 log( 2 log 1     h n h     ) 1 log( 1
  • 40. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 40 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.
  • 41. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 41 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. …
  • 42. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 42 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 9, 230, 331, 231, 34, 343, 45, 345, 453, 454, 59 9, 34, 45, 59, 230, 231, 331, 343, 345, 453, 454
  • 43. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 43 Radix Sort Animation http://www.cs.armstrong.edu/liang/animation/web/RadixSort.html