Searching and Sorting - rana-tarabishi.com · Selection Sort Insertion Sort ... * Sorts the...
Transcript of Searching and Sorting - rana-tarabishi.com · Selection Sort Insertion Sort ... * Sorts the...
1
Searching and Sorting
(Section 9)
College of Computer Science and Engineering
Taibah University
S1, 1438
Algorithms and Data Structures(CS211 & CS202)
Lecture Scope
Searching algorithms
Linear Search
Binary Search
Several sorting algorithms, including:
Selection Sort
Insertion Sort
Bubble Sort
Heap Sort
Quick Sort
Merge Sort
Complexity of the search and sort algorithms2
2
Searching
3
Linear search
Binary search
Searching
Searching is the process of finding a target element
among a group of items (the search pool), or
determining that it isn't there
This requires repetitively comparing the target to
candidates in the search pool
An efficient search performs no more comparisons
than it has to
4
3
Searching (cont…)
We'll define the algorithms such that they can search any set of
objects, therefore we will search objects that implement the
Comparable interface
Recall that the compareTo method returns an integer that specifies
the relationship between two objects:
obj1.compareTo(obj2)
This call returns a number less than, equal to, or greater than 0 if
obj1 is less than, equal to, or greater than obj2, respectively
5
Linear Search
A linear search simply examines each item in the search pool,
one at a time, until either the target is found or until the pool is
exhausted
This approach does not assume the items in the search pool
are in any particular order
6
4
/**
* Searches the specified array of objects using a linear search
* algorithm.
*
* @param data the array to be searched
* @param min the integer representation of the minimum value
* @param max the integer representation of the maximum value
* @param target the element being searched for
* @return true if the desired element is found
*/
public static <T>
boolean linearSearch(T[] data, int min, int max, T target)
{
int index = min;
boolean found = false;
while (!found && index <= max)
{
found = data[index].equals(target);
index++;
}
return found;
}7
Linear Search - implementation
Binary Search
If the search pool is sorted, then we can be more efficient than a
linear search
A binary search eliminates large parts of the search pool with each
comparison
Instead of starting the search at one end, we begin in the middle
If the target isn't found, we know that if it is in the pool at all, it is in
one half or the other
We can then jump to the middle of that half, and continue similarly
8
5
Binary Search (cont…)
A binary search algorithm is often implemented
recursively
Each recursive call searches a smaller portion of
the search pool
The base case is when there are no more viable
candidates
At any point there may be two “middle” values, in
which case the first is used
9
Binary Search (cont…)
For example, find the number 29 in the following sorted list of
numbers:
8 15 22 29 36 54 55 61 70 73 88
First, compare the target to the middle value 54
We now know that if 29 is in the list, it is in the front half of the list
With one comparison, we’ve eliminated half of the data
Then compare to 22, eliminating another quarter of the data, etc.
10
6
found = BinarySearch(info, 25, 0, 14 );
item fromLoc toLoc
indexes
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
info
0 2 4 6 8 10 12 14 16 18 20 22 24 26 28
16 18 20 22 24 26 28
24 26 28
24
NOTE: denotes element examined 11
Binary Search - Example
public boolean BinarySearch (int info[ ], int item, int fromLoc, int toLoc)
// Pre: info [ fromLoc . . toLoc ] sorted in ascending order
// Post: Function value = ( item in info [ fromLoc . . toLoc] )
{ int mid ;
if ( fromLoc > toLoc ) // base case -- not found
return false ;
else {
mid = ( fromLoc + toLoc ) / 2 ;
if ( info[mid] == item ) // base case-- found at mid
return true ;
else if ( item < info[mid] ) // search lower half
return BinarySearch ( info, item, fromLoc, mid-1 ) ;
else // search upper half
return BinarySearch( info, item, mid + 1, toLoc ) ;
}
}
12
Binary Search - Implementation
7
/**
* Searches the specified array of objects using a binary search
* algorithm.
*
* @param data the array to be searched
* @param min the integer representation of the minimum value
* @param max the integer representation of the maximum value
* @param target the element being searched for
* @return true if the desired element is found
*/
public static <T extends Comparable<T>>
boolean binarySearch(T[] data, int min, int max, T target)
{
boolean found = false;
int midpoint = (min + max) / 2; // determine the midpoint
if (data[midpoint].compareTo(target) == 0)
found = true;
else if (data[midpoint].compareTo(target) > 0)
{
if (min <= midpoint - 1)
found = binarySearch(data, min, midpoint - 1, target);
}
else if (midpoint + 1 <= max)
found = binarySearch(data, midpoint + 1, max, target);
return found;
}13
Binary Search - Implementation (cont…)
Comparing Search Algorithms
The expected case for finding an element with
a linear search is n/2, which is O(n)
Worst case is also O(n)
The worst case for binary search is (log2n) / 2
comparisons
Which makes binary search O(log n)
Keep in mind that for binary search to work, the
elements must be already sorted14
8
Sorting
15
1. Selection sort
2. Insertion sort
3. Bubble sort
4. Heap sort
5. Quick sort
6. Merge sort
Sorting
Sorting is the process of arranging a group of items
into a defined order based on particular criteria
Many sorting algorithms have been designed
Sequential sorts require approximately n2 comparisons
to sort n elements
Logarithmic sorts typically require nlog2n comparisons
to sort n elements
16
9
Selection Sort
Selection sort orders a list of values by repetitively putting a
particular value into its final position
More specifically:
find the smallest value in the list
switch it with the value in the first position
find the next smallest value in the list
switch it with the value in the second position
repeat until all values are in their proper places
17
1
Selection Sort (cont…)
18
10
selectionsort(data[ ])
for i = 0 to data.length-2
select the smallest element among
data[i], …, data[data.length-1];
swap it with data[i];
Algorithm:
Selection Sort (cont…)
19
/**
* Sorts the specified array of integers using the selection
* sort algorithm.
*
* @param data the array to be sorted
*/
public static <T extends Comparable<T>>
void selectionSort(T[] data)
{
int min;
T temp;
for (int index = 0; index < data.length-1; index++)
{
min = index;
for (int scan = index+1; scan < data.length; scan++)
if (data[scan].compareTo(data[min])<0)
min = scan;
swap(data, min, index);
}
}
20
Selection Sort - Implementation
11
/**
* Swaps to elements in an array. Used by various sorting algorithms.
*
* @param data the array in which the elements are swapped
* @param index1 the index of the first element to be swapped
* @param index2 the index of the second element to be swapped
*/
private static <T extends Comparable<T>>
void swap(T[] data, int index1, int index2)
{
T temp = data[index1];
data[index1] = data[index2];
data[index2] = temp;
}
21
Selection Sort - Implementation (cont…)
Selection Sort - Implementation (cont…)
22
public static void selectionSort(int[] A,int length)
{
for (int i=0; i < (length -1); i++)
{
for (int j = i+1; j < length; j++)
{
if (A[i] > A[j])
{
//... Exchange elements
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
}
}
The same implementation with some differences in the code
12
5
3
4
1
2 1
3
4
2
5
1
2
4
3
5
1
2
3
4
5
1
2
3
4
5
1
2
3
4
5
2
5
1
3
4
23
Selection Sort - Example
(random)
best case avg caseworst case
comparisons O(n2) O(n2) O(n2)
movements 0 O(n) O(n)
24
Selection Sort: Computational Complexity
(ordered)
13
Insertion Sort
Insertion sort orders a values by repetitively inserting a particular
value into a sorted subset of the list
More specifically:
consider the first item to be a sorted sublist of length 1
insert the second item into the sorted sublist, shifting the first item if
needed
insert the third item into the sorted sublist, shifting the other items as
needed
repeat until all values have been inserted into their proper positions
25
2
Insertion Sort (cont…)
26
14
insertionsort(data[ ])
for i = 1 to data.length-1
tmp = data[i];
move all elements data[j] greater than tmp by one position;
place tmp in its proper position;
27
Insertion Sort (cont…)
Algorithm:
/**
* Sorts the specified array of objects using an insertion
* sort algorithm.
*
* @param data the array to be sorted
*/
public static <T extends Comparable<T>>
void insertionSort(T[] data)
{
for (int index = 1; index < data.length; index++)
{
T key = data[index];
int position = index;
// shift larger values to the right
while (position > 0 && data[position-1].compareTo(key) > 0)
{
data[position] = data[position-1];
position--;
}
data[position] = key;
}
} 28
Insertion Sort - Implementation
15
Insertion Sort - Implementation (cont…)
29
public static void insertionSort(int[] A,int length) {
for(int i = 1; i < length; i++)
{
int j = i;
int key = A[i];
while ((j > 0) && (A[j-1] > key))
{
A[j] = A[j-1];
j--;
}
A[j] = key;
}
}
The same implementation with some differences in the code
For example, consider this sorted array containing of eight sorted entries
Suppose we want to insert 14 into this array leaving the resulting array sorted
Starting at the back, if the number is greater than 14, copy it to the right
Once an entry less than 14 is found, insert 14 into the resulting vacancy
5 7 12 19 21 26 33 40 14 9 18 21 2
Insertion Sort - Example 1
30
16
2
5
3
4
2
5
1
3
4
5
2
5
3
4
5
2
5
3
4
2
5
2 1
2
5
4
5
3
1
2
3
55
4
1
2
3
4
5
1
11
3
1
2
5
4
5
3
4
1
2
3
5
5
45
31
Insertion Sort - Example 2
(ordered) (reversed)(random)
best case avg case worst case
comparisons O(n) O(n2) O(n2)
movements O(n) O(n2) O(n2)
32
Insertion Sort: Computational Complexity
17
Bubble sort orders a list of values by repetitively
comparing neighboring elements and swapping their
positions if necessary
Suppose we have an array of data which is unsorted:
Starting at the front, traverse the array, find the largest item,
and move (or bubble) it to the top
With each subsequent iteration, find the next largest item
and bubble it up towards the top of the array
It is also significantly worse than insertion sort
Bubble Sort
33
3
bubblesort(data[ ])
for i = 0 to data.length-2
for j = data.length-1 downto i+1
if elements in positions j and j-1 are out of order
swap them;
34
Algorithm:
Bubble Sort (cont…)
18
Bubble Sort - Example
Consider the unsorted array to the right
We start with the element in the first location,
and move forward:
if the current and next items are in order,
continue with the next item, otherwise
swap the two entries
35
Bubble Sort - Example (cont…)
After one loop, the largest element
is in the last location
Repeat the procedure
36
Now the two largest elements are
at the end
Repeat again
19
Bubble Sort - Example (cont…)
37
With this loop, 5 and 7 are
swapped
Finally, we swap the last two
entries to order them
At this point, we have a
sorted array
Bubble Sort - Implementation
Starting with the first item, assume that it is the largest
Compare it with the second item:
If the first is larger, swap the two,
Otherwise, assume that the second item is the largest
Continue up the array, either swapping or redefining the largest item
After one pass, the largest item must be the last in the list
Start at the front again:
the second pass will bring the second largest element into the second
last position
Repeat n – 1 times, after which, all entries will be in place
38
20
/**
* Sorts the specified array of objects using a bubble sort
* algorithm.
*
* @param data the array to be sorted
*/
public static <T extends Comparable<T>>
void bubbleSort(T[] data)
{
int position, scan;
T temp;
for (position = data.length - 1; position >= 0; position--)
{
for (scan = 0; scan <= position - 1; scan++)
{
if (data[scan].compareTo(data[scan+1]) > 0)
swap(data, scan, scan + 1);
}
}
}
39
Bubble Sort - Implementation (cont…)
Bubble Sort - Implementation (cont…)
40
public static void bubbleSort( int a[], int length )
{
for(int i = 0; i < (length-1); i++)
{
for(int j = length-1; j > i; j--)
{
if(a[j-1] > a[j])
{
//... Exchange elements
int temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
}
}
}
}
The same implementation with some differences in the code
21
2
3
4
1
5
5
3
4
1
2 1
2
4
3
5
1
2
3
4
5
1
2
3
4
5
1
2
3
4
5
2
5
1
3
4
1
2
3
4
5
41
Bubble Sort - Example
best case avg case worst case
comparisons O(n2) O(n2) O(n2)
movements 0 O(n2) O(n2)
(ordered) (random) (reversed)
42
Bubble Sort: Computational Complexity
22
insertion best case avg case worst case
comparisons O(n) O(n2) O(n2)
movements O(n) O(n2) O(n2)
selection best case avg case worst case
comparisons O(n2) O(n2) O(n2)
movements 0 O(n) O(n)
bubble best case avg case worst case
comparisons O(n2) O(n2) O(n2)
movements 0 O(n2) O(n2)
43
Basic Sorting Algorithms: Comparison
Heap:
A heap is a binary tree that satisfies these special SHAPE and
ORDER properties:
Its shape must be a complete binary tree.
For each node in the heap, the value stored in that node is
greater than or equal to the value in each of its children.
Heap Sort 4
44
23
Max-Heap & Min-Heap:
A heap can also be a min-heap, in which the node is less than
or equal to its children.
This means that the root of a max-heap contains the largest
element, whereas the root of a min-heap contains the smallest.
For the heapsort algorithm, we use max-heaps.
Min-heaps commonly implement priority queues.
Heap Sort (cont…)
45
70
60
40 30
12
8
root
The largest element in a max-heap is always found in the root node
10
Heap Sort (cont…)
46
24
Heap Sort Approach
First, make the unsorted array into a heap by satisfying the order
property. Then repeat the steps below until there are no more
unsorted elements.
Take the root (maximum) element off the heap by swapping it into
its correct place in the array at the end of the unsorted elements.
Reheap the remaining unsorted elements. (This puts the next-
largest element into the root position).
Heap Sort (cont…)
47
heapsort(data[ ])
transform data[ ] into a heap;
for i = data.length-1 downto 2
swap the root with the element in position i;
restore the heap property for the tree data[0], …, data[i-1];
48
Heap Sort (cont…)
Algorithm:
25
The heap can be stored in an array
Additionally, because arrays start at 0 (we started at entry 1 for binary heaps) ,
we need different formulas for the children and parent
we can easily compute the indices of its parent, left child, and right child:
Its left child is in A[i*2+1]
Its right child is in A[i*2+2]
Its parent is in A[(i-1)/2]49
Heap Sort (cont…)
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
70
60
12
40
30
8
10
values
70
0
60
1
40
3
30
4
12
2
8
5
root
10
6
Storing the data into an array as a heap structure
Heap Sort - Example
50
26
After creating the original heap
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
70
60
12
40
30
6
10
values
70
0
60
1
40
3
30
4
12
2
6
5
root
10
6
Heap Sort - Example (cont…)
51
Swap root element into last place in unsorted array
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
70
60
12
40
30
6
10
values
70
0
60
1
40
3
30
4
12
2
6
5
root
10
6
Heap Sort - Example (cont…)
52
27
After swapping root element into its place
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
10
0
60
1
40
3
30
4
12
2
6
5
root
70
6
10
60
12
40
30
6
70 NO NEED TO CONSIDER AGAIN
Heap Sort - Example (cont…)
53
After reheaping remaining unsorted elements
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
60
0
40
1
10
3
30
4
12
2
6
5
root
70
6
60
40
12
10
30
6
70
Heap Sort - Example (cont…)
54
28
Swap root element into last place in unsorted array
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
60
0
40
1
10
3
30
4
12
2
6
5
root
70
6
60
40
12
10
30
6
70
Heap Sort - Example (cont…)
55
After swapping root element into its place
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
6
0
40
1
10
3
30
4
12
2
root
70
6
6
40
12
10
30
60
70 NO NEED TO CONSIDER AGAIN
60
5
Heap Sort - Example (cont…)
56
29
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
40
0
30
1
10
3
6
4
12
2
root
70
6
40
30
12
10
6
60
70
60
5
After reheaping remaining unsorted elements
Heap Sort - Example (cont…)
57
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
40
0
30
1
10
3
6
4
12
2
root
70
6
40
30
12
10
6
60
70
60
5
Swap root element into last place in unsorted array
Heap Sort - Example (cont…)
58
30
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
6
0
30
1
10
3
12
2
root
70
6
60
5
After swapping root element into its place
40
4
6
30
12
10
40
60
70 NO NEED TO CONSIDER AGAIN
Heap Sort - Example (cont…)
59
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
30
0
10
1
6
3
12
2
root
70
6
60
5
40
4
30
10
12
6
40
60
70
After reheaping remaining unsorted elements
Heap Sort - Example (cont…)
60
31
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
30
0
10
1
6
3
12
2
root
70
6
60
5
40
4
30
10
12
6
40
60
70
Swap root element into last place in unsorted array
Heap Sort - Example (cont…)
61
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
6
0
10
1
12
2
root
70
6
60
5
40
4
After swapping root element into its place
6
10
12
30
40
60
70
30
3
NO NEED TO CONSIDER AGAIN
Heap Sort - Example (cont…)
62
32
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
12
0
10
1
6
2
root
70
6
60
5
40
4
12
10
6
30
40
60
70
30
3
After reheaping remaining unsorted elements
Heap Sort - Example (cont…)
63
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
12
0
10
1
6
2
root
70
6
60
5
40
4
12
10
6
30
40
60
70
30
3
Swap root element into last place in unsorted array
Heap Sort - Example (cont…)
64
33
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
6
0
10
1
root
70
6
60
5
40
4
30
3
After swapping root element into its place
NO NEED TO CONSIDER AGAIN
12
2
6
10
12
30
40
60
70
Heap Sort - Example (cont…)
65
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
10
0
6
1
root
70
6
60
5
40
4
30
3
12
2
10
6
12
30
40
60
70
After reheaping remaining unsorted elements
Heap Sort - Example (cont…)
66
34
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
10
0
6
1
root
70
6
60
5
40
4
30
3
12
2
10
6
12
30
40
60
70
Swap root element into last place in unsorted array
Heap Sort - Example (cont…)
67
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
root
70
6
60
5
40
4
30
3
12
2
After swapping root element into its place
6
10
12
30
40
60
70
10
1
6
0
ALL ELEMENTS ARE SORTED
Heap Sort - Example (cont…)
68
35
How many comparisons?
24
0
60
1
30
3
40
4
12
2
8
5
root
10
6
15
7
6
8
18
9
In reheap down, an
element is compared with
its 2 children (and swapped
with the larger). But only
one element at each level
makes this comparison,
and a complete binary tree
with N nodes has only
O(log2N) levels.70
10
Heap Sort - Example (cont…)
69
70
Heap Sort - Another Example
36
71
Heap Sort - Another Example (cont…)
Heap Sort of N elements: How many comparisons?
(N/2) * O(log N) compares to create original heap
(N-1) * O(log N) compares for the sorting loop
= O ( N * log N) compares total
72
Heap Sort - Algorithm Analysis
37
DCsorting(data[ ])
partition data[ ] into data1[ ] and data2[];
DCsorting(data1[ ]);
DCsorting(data2[ ]);
merge data1[] and data2[] into data[ ];
quicksort
mergesort
73
Divide-and-Conquer Sorting
Algorithm:
Quick Sort
Quick sort orders values by partitioning the list around
one element, then sorting each partition
More specifically:
choose one element in the list to be the partition element
organize the elements so that all elements less than the
partition element are to the left and all greater are to the right
apply the quick sort algorithm (recursively) to both partitions
74
5
38
A . . Z
A . . L M . . Z
A . . F G . . L M . . R S . . Z
Quick Sort (cont…)
75
quicksort(data[ ])
if data.length > 1
choose pivot;
while there are elements left in data
include element either in
data1[ ] = {el: el ≤ pivot};
or in data2[ ] = {el: el ≥ pivot};
quicksort(data1[ ]);
quicksort(data2[ ]);
par
titi
on
ing
76
Quick Sort (cont…)
Algorithm:
39
Quick Sort (cont…)
There are different strategy for selecting the split
value (pivot):
First element
Last element
Randomly
Median of three
77
Quick Sort (cont…)
For example, given
we can select the middle entry, 44, and sort the remaining entries
into two groups, those less than 44 and those greater than 44:
Notice that 44 is now in the correct location if the list was sorted
Proceed by applying the algorithm to the first six and last eight entries
80 38 95 84 66 10 79 44 26 87 96 12 43 81 3
38 10 26 12 43 3 44 80 95 84 66 79 87 96 81
78
40
Quick Sort - Median-of-three
It is difficult to find the median so consider another
strategy:
Choose the median of the first, middle, and last entries in
the list
This will usually give a better approximation of the
actual median
79
Quick Sort - Median-of-three (cont…)
Sorting the elements based on 44 results in two sub-lists,
each of which must be sorted (again, using quicksort)
Select the 26 to partition the first sub-list:
Select 81 to partition the second sub-list:
Continue doing so until the appropriate entries you find are
actually in order
80
41
/**
* Sorts the specified array of objects using the quick sort algorithm.
*
* @param data the array to be sorted
*/
public static <T extends Comparable<T>>
void quickSort(T[] data)
{
quickSort(data, 0, data.length - 1);
}
81
Quick Sort - Implementation
/**
* Recursively sorts a range of objects in the specified array using the
* quick sort algorithm.
*
* @param data the array to be sorted
* @param min the minimum index in the range to be sorted
* @param max the maximum index in the range to be sorted
*/
private static <T extends Comparable<T>>
void quickSort(T[] data, int min, int max)
{
if (min < max)
{
// create partitions
int indexofpartition = partition(data, min, max);
// sort the left partition (lower values)
quickSort(data, min, indexofpartition - 1);
// sort the right partition (higher values)
quickSort(data, indexofpartition + 1, max);
}
}82
Quick Sort - Implementation (cont…)
42
/**
* Used by the quick sort algorithm to find the partition.
*
* @param data the array to be sorted
* @param min the minimum index in the range to be sorted
* @param max the maximum index in the range to be sorted
*/
private static <T extends Comparable<T>>
int partition(T[] data, int min, int max)
{
T partitionelement;
int left, right;
int middle = (min + max) / 2;
// use the middle data value as the partition element
partitionelement = data[middle];
// move it out of the way for now
swap(data, middle, min);
left = min;
right = max;
83
Quick Sort - Implementation (cont…)
while (left < right)
{
// search for an element that is > the partition element
while (left < right && data[left].compareTo(partitionelement) <= 0)
left++;
// search for an element that is < the partition element
while (data[right].compareTo(partitionelement) > 0)
right--;
// swap the elements
if (left < right)
swap(data, left, right);
}
// move the partition element into place
swap(data, min, right);
return right;
}
84
Quick Sort - Implementation (cont…)
43
N For first call, when each of N elements is compared to
the split value
2 * N/2 For the next pair of calls, when N/2 elements in each “half”
of the original array are compared to their own split values.
4 * N/4 For the four calls when N/4 elements in each “quarter” of
original array are compared to their own split values.
.
.
.
HOW MANY SPLITS CAN OCCUR?
Quick Sort of N Elements: How many comparisons?
85
It depends on the order of the original array elements!
If each split divides the subarray approximately in half, there
will be only log2N splits, and QuickSort is O(N*log2N).
But, if the original array was sorted to begin with, the recursive
calls will split up the array into parts of unequal length, with
one part empty, and theother part containing all the rest of the
array except for split value itself. In this case, there can be as
many as N-1 splits, and QuickSort is O(N2).
Quick Sort of N Elements: How many splits can occur?
86
44
Merge Sort
Merge sort orders values by recursively dividing the list in half
until each sub-list has one element, then recombining
More specifically:
divide the list into two roughly equal parts
recursively divide each part in half, continuing until a part contains
only one element
merge the two parts into one sorted list
continue to merge parts as the recursion unfolds
This is the a divide-and-conquer algorithm87
6
Merge Sort (cont…)
Dividing lists in half repeatedly:
88
45
Merge Sort (cont…)
Merging sorted elements
89
mergesort(data[ ], first, last)
if first < last
mid = (first + last) / 2;
mergesort(data[ ], first, mid);
mergesort(data[ ], mid+1, last);
merge(data[ ], first, last);
partitioning
merging
90
Merge Sort (cont…)
Algorithm:
46
2 4 9 5 5 7 3 6 2
2 4 9 5 5 7 3 6 2
2 4 9
2 4 9 5
6 2
2 2 3 4 5 5 6 7 9
5 5 7 3
5
2 4
2 4 9 5 5
2 4 5 5 9
3 7 2 6
2 3 6 7
6 27 3
2 4
91
Merge Sort - Example 1
92
Merge Sort - Example 2
47
/**
* Sorts the specified array of objects using the merge sort
* algorithm.
*
* @param data the array to be sorted
*/
public static <T extends Comparable<T>>
void mergeSort(T[] data)
{
mergeSort(data, 0, data.length - 1);
}
/**
* Recursively sorts a range of objects in the specified array using the
* merge sort algorithm.
*
* @param data the array to be sorted
* @param min the index of the first element
* @param max the index of the last element
*/
private static <T extends Comparable<T>>
void mergeSort(T[] data, int min, int max)
{
if (min < max)
{
int mid = (min + max) / 2;
mergeSort(data, min, mid);
mergeSort(data, mid+1, max);
merge(data, min, mid, max);
}
}
93
Merge Sort - Implementation (cont…)
/**
* Merges two sorted subarrays of the specified array.
*
* @param data the array to be sorted
* @param first the beginning index of the first subarray
* @param mid the ending index fo the first subarray
* @param last the ending index of the second subarray
*/
@SuppressWarnings("unchecked")
private static <T extends Comparable<T>>
void merge(T[] data, int first, int mid, int last)
{
T[] temp = (T[])(new Comparable[data.length]);
int first1 = first, last1 = mid; // endpoints of first subarray
int first2 = mid+1, last2 = last; // endpoints of second subarray
int index = first1; // next index open in temp array
// Copy smaller item from each subarray into temp until one
// of the subarrays is exhausted
while (first1 <= last1 && first2 <= last2)
{
if (data[first1].compareTo(data[first2]) < 0)
{
temp[index] = data[first1];
first1++;
}
94
Merge Sort - Implementation (cont…)
48
else
{
temp[index] = data[first2];
first2++;
}
index++;
}
// Copy remaining elements from first subarray, if any
while (first1 <= last1)
{
temp[index] = data[first1];
first1++;
index++;
}
// Copy remaining elements from second subarray, if any
while (first2 <= last2)
{
temp[index] = data[first2];
first2++;
index++;
}
// Copy merged data into original array
for (index = first; index <= last; index++)
data[index] = temp[index];
}
95
Merge Sort - Implementation (cont…)
Merging Example
Consider the two sorted arrays and an empty array
Define three indices at the start of each array
96
49
Merging Example (cont…)
We compare 2 and 3: 2 < 3
Copy 2 down
Increment the corresponding indices
97
Merging Example (cont…)
We compare 3 and 7
Copy 3 down
Increment the corresponding indices
98
50
Merging Example (cont…)
We compare 5 and 7
Copy 5 down
Increment the appropriate indices
99
Merging Example (cont…)
We compare 18 and 7
Copy 7 down
Increment...
100
51
Merging Example (cont…)
We compare 18 and 12
Copy 12 down
Increment...
101
Merging Example (cont…)
We compare 18 and 16
Copy 16 down
Increment...
102
52
Merging Example (cont…)
We compare 18 and 33
Copy 18 down
Increment...
103
Merging Example (cont…)
We compare 21 and 33
Copy 21 down
Increment...
104
53
Merging Example (cont…)
We compare 24 and 33
Copy 24 down
Increment...
105
Merging Example (cont…)
We would continue until we have passed beyond the limit of one of the
two arrays
After this, we simply copy over all remaining entries in the non-empty
array
106
54
The entire array can be subdivided into halves only log2N times.
Each time it is subdivided, function Merge is called to re-combine
the halves.
Function Merge uses a temporary array to store the merged
elements.
Merging is O(N) because it compares each element in the
subarrays.
Copying elements back from the temporary array to the values
array is also O(N).
MERGE SORT IS O(N*log2N).
Merge Sort of N elements: How many comparisons?
107
Comparing Sorts
Selection sort, insertion sort, and bubble sort use different
techniques, but are all O(n2)
They are all based in a nested loop approach
In quick sort, if the partition element divides the elements in half,
each recursive call operates on about half the data
The act of partitioning the elements at each level is O(n)
The effort to sort the entire list is O(n log n)
It could deteriorate to O(n2) if the partition element is poorly chosen
108
55
Comparing Sorts (cont…)
Merge sort divides the list repeatedly in half, which
results in the O(log n) portion
The act of merging is O(n)
So the efficiency of merge sort is O(n log n)
Selection, insertion, and bubble sorts are called
quadratic sorts
Quick sort and merge sort are called logarithmic sorts
109
Summary
Insertion Sort:
Insert new entries into growing sorted lists
Actual and average case run time: O(n2)
From the description of bubble sort, it sounds as if it is as good as insertion sort
it has the same asymptotic behavior in practice, however, it is significantly worse
it is also much more difficult to code...
Selection sort, insertion sort, and bubble sort use different techniques, but are all O(n2)
We have seen heap sort algorithm - O(n ln(n)) :
Convert the unsorted list into a max-heap as complete array
Pop the top n times and place that object into the vacancy at the end
It requires O(1) additional memory—it is truly in-place
It is a nice algorithm; however, we will see two other faster n ln(n) algorithms; however:
Merge sort requires O(n) additional memory
Quick sort requires O(ln(n)) additional memory110
56
Summary (cont…)
We covered quicksort:
On average faster than heap sort or merge sort
Uses a pivot to partition the objects
Using the median of three pivots is a reasonably means of finding the pivot
Average run time of O(n ln(n)) and O(ln(n)) memory
Worst case run time of O(n2) and O(n) memory
We covered mergesort:
This is the a divide-and-conquer algorithm
Each time it is subdivided, function Merge is called to re-combine the halves.
Function Merge uses a temporary array to store the merged elements.
So the efficiency of merge sort is O(n log n)
Merge sort requires O(n) additional memory
111
References
Java Software Structures - Designing and Using Data Structures, 4th
edition, Lewis and Chase.
Data Structures & Algorithms in Java, 4th edition, Drozdek.
Data Structures and Problem Solving Using Java, 4th edition, Weiss.
Slides at: https://ece.uwaterloo.ca/~dwharder/aads/Lecture_materials/
Douglas Wilhelm Harder, Mmath, University of Waterloo.
Slides at: http://faculty.kfupm.edu.sa/ICS/jauhar/ics202/
112
57
Any Question
???
113