Analysis of Algorithms
CS 477/677
Sorting – Part A
Instructor: George Bebis
(Chapter 2)
2
The Sorting Problem
• Input:
– A sequence of n numbers a1, a2, . . . , an
• Output:
– A permutation (reordering) a1’, a2’, . . . , an’ of the input
sequence such that a1’ ≤ a2’ ≤ · · · ≤ an’
3
Structure of data
4
Why Study Sorting Algorithms?
• There are a variety of situations that we can
encounter
– Do we have randomly ordered keys?
– Are all keys distinct?
– How large is the set of keys to be ordered?
– Need guaranteed performance?
• Various algorithms are better suited to some of
these situations
5
Some Definitions
• Internal Sort
– The data to be sorted is all stored in the computer’s
main memory.
• External Sort
– Some of the data to be sorted might be stored in
some external, slower, device.
• In Place Sort
– The amount of extra space required to sort the data is
constant with the input size.
6
Stability
• A STABLE sort preserves relative order of records with
equal keys
Sorted on first key:
Sort file on second key:
Records with key value
3 are not in order on
first key!!
7
Insertion Sort
• Idea: like sorting a hand of playing cards
– Start with an empty left hand and the cards facing
down on the table.
– Remove one card at a time from the table, and insert
it into the correct position in the left hand
• compare it with each of the cards already in the hand, from
right to left
– The cards held in the left hand are sorted
• these cards were originally the top cards of the pile on the
table
8
To insert 12, we need to
make room for it by moving
first 36 and then 24.
Insertion Sort
9
Insertion Sort
10
Insertion Sort
11
Insertion Sort
5 2 4 6 1 3
input array
left sub-array right sub-array
at each iteration, the array is divided in two sub-arrays:
sorted unsorted
12
Insertion Sort
13
INSERTION-SORT
Alg.: INSERTION-SORT(A)
for j ← 2 to n
do key ← A[ j ]
Insert A[ j ] into the sorted sequence A[1 . . j -1]
i ← j - 1
while i > 0 and A[i] > key
do A[i + 1] ← A[i]
i ← i – 1
A[i + 1] ← key
• Insertion sort – sorts the elements in place
a8a7a6a5a4a3a2a1
1 2 3 4 5 6 7 8
key
14
Loop Invariant for Insertion Sort
Alg.: INSERTION-SORT(A)
for j ← 2 to n
do key ← A[ j ]
Insert A[ j ] into the sorted sequence A[1 . . j -1]
i ← j - 1
while i > 0 and A[i] > key
do A[i + 1] ← A[i]
i ← i – 1
A[i + 1] ← key
Invariant: at the start of the for loop the elements in A[1 . . j-1]
are in sorted order
15
Proving Loop Invariants
• Proving loop invariants works like induction
• Initialization (base case):
– It is true prior to the first iteration of the loop
• Maintenance (inductive step):
– If it is true before an iteration of the loop, it remains true before
the next iteration
• Termination:
– When the loop terminates, the invariant gives us a useful
property that helps show that the algorithm is correct
– Stop the induction when the loop terminates
16
Loop Invariant for Insertion Sort
• Initialization:
– Just before the first iteration, j = 2:
the subarray A[1 . . j-1] = A[1],
(the element originally in A[1]) – is
sorted
17
Loop Invariant for Insertion Sort
• Maintenance:
– the while inner loop moves A[j -1], A[j -2], A[j -3],
and so on, by one position to the right until the proper
position for key (which has the value that started out in
A[j]) is found
– At that point, the value of key is placed into this
position.
18
Loop Invariant for Insertion Sort
• Termination:
– The outer for loop ends when j = n + 1  j-1 = n
– Replace n with j-1 in the loop invariant:
• the subarray A[1 . . n] consists of the elements originally in
A[1 . . n], but in sorted order
• The entire array is sorted!
jj - 1
Invariant: at the start of the for loop the elements in A[1 . . j-1]
are in sorted order
19
Analysis of Insertion Sort
cost times
c1 n
c2 n-1
0 n-1
c4 n-1
c5
c6
c7
c8 n-1
 
n
j jt2
 

n
j jt2
)1(
 

n
j jt2
)1(
    )1(11)1()1()( 8
2
7
2
6
2
5421   
nctctctcncncncnT
n
j
j
n
j
j
n
j
j
INSERTION-SORT(A)
for j ← 2 to n
do key ← A[ j ]
Insert A[ j ] into the sorted sequence A[1 . . j -1]
i ← j - 1
while i > 0 and A[i] > key
do A[i + 1] ← A[i]
i ← i – 1
A[i + 1] ← key
tj: # of times the while statement is executed at iteration j
20
Best Case Analysis
• The array is already sorted
– A[i] ≤ key upon the first time the while loop test is run
(when i = j -1)
– tj = 1
• T(n) = c1n + c2(n -1) + c4(n -1) + c5(n -1) + c8(n-1)
= (c1 + c2 + c4 + c5 + c8)n + (c2 + c4 + c5 + c8)
= an + b = (n)
“while i > 0 and A[i] > key”
    )1(11)1()1()( 8
2
7
2
6
2
5421   
nctctctcncncncnT
n
j
j
n
j
j
n
j
j
21
Worst Case Analysis
• The array is in reverse sorted order
– Always A[i] > key in while loop test
– Have to compare key with all elements to the left of the j-th
position  compare with j-1 elements  tj = j
a quadratic function of n
• T(n) = (n2) order of growth in n2
1 2 2
( 1) ( 1) ( 1)
1 ( 1)
2 2 2
n n n
j j j
n n n n n n
j j j
  
  
        
)1(
2
)1(
2
)1(
1
2
)1(
)1()1()( 8765421 











 nc
nn
c
nn
c
nn
cncncncnT
cbnan  2
“while i > 0 and A[i] > key”
    )1(11)1()1()( 8
2
7
2
6
2
5421   
nctctctcncncncnT
n
j
j
n
j
j
n
j
j
using we have:
22
Comparisons and Exchanges in
Insertion Sort
INSERTION-SORT(A)
for j ← 2 to n
do key ← A[ j ]
Insert A[ j ] into the sorted sequence A[1 . . j -1]
i ← j - 1
while i > 0 and A[i] > key
do A[i + 1] ← A[i]
i ← i – 1
A[i + 1] ← key
cost times
c1 n
c2 n-1
0 n-1
c4 n-1
c5
c6
c7
c8 n-1
 
n
j jt2
 

n
j jt2
)1(
 

n
j jt2
)1(
 n2/2 comparisons
 n2/2 exchanges
23
Insertion Sort - Summary
• Advantages
– Good running time for “almost sorted” arrays (n)
• Disadvantages
– (n2) running time in worst and average case
–  n2/2 comparisons and exchanges
24
Bubble Sort (Ex. 2-2, page 38)
• Idea:
– Repeatedly pass through the array
– Swaps adjacent elements that are out of order
• Easier to implement, but slower than Insertion
sort
1 2 3 n
i
1329648
j
25
Example
1329648
i = 1 j
3129648
i = 1 j
3219648
i = 1 j
3291648
i = 1 j
3296148
i = 1 j
3296418
i = 1 j
3296481
i = 1 j
3296481
i = 2 j
3964821
i = 3 j
9648321
i = 4 j
9684321
i = 5 j
9864321
i = 6 j
9864321
i = 7
j
26
Bubble Sort
Alg.: BUBBLESORT(A)
for i  1 to length[A]
do for j  length[A] downto i + 1
do if A[j] < A[j -1]
then exchange A[j]  A[j-1]
1329648
i = 1 j
i
27
Bubble-Sort Running Time
Thus,T(n) = (n2)
2
2
1 1 1
( 1)
( )
2 2 2
n n n
i i i
n n n n
where n i n i n
  

        
Alg.: BUBBLESORT(A)
for i  1 to length[A]
do for j  length[A] downto i + 1
do if A[j] < A[j -1]
then exchange A[j]  A[j-1]
T(n) = c1(n+1) + 
n
i
in
1
)1(c2 c3 

n
i
in
1
)( c4 

n
i
in
1
)(
= (n) + (c2 + c2 + c4) 

n
i
in
1
)(
Comparisons:  n2/2
Exchanges:  n2/2
c1
c2
c3
c4
28
Selection Sort (Ex. 2.2-2, page 27)
• Idea:
– Find the smallest element in the array
– Exchange it with the element in the first position
– Find the second smallest element and exchange it with
the element in the second position
– Continue until the array is sorted
• Disadvantage:
– Running time depends only slightly on the amount of
order in the file
29
Example
1329648
8329641
8349621
8649321
8964321
8694321
9864321
9864321
30
Selection Sort
Alg.: SELECTION-SORT(A)
n ← length[A]
for j ← 1 to n - 1
do smallest ← j
for i ← j + 1 to n
do if A[i] < A[smallest]
then smallest ← i
exchange A[j] ↔ A[smallest]
1329648
31
n2/2
comparisons
Analysis of Selection Sort
Alg.: SELECTION-SORT(A)
n ← length[A]
for j ← 1 to n - 1
do smallest ← j
for i ← j + 1 to n
do if A[i] < A[smallest]
then smallest ← i
exchange A[j] ↔ A[smallest]
cost times
c1 1
c2 n
c3 n-1
c4
c5
c6
c7 n-1




1
1
)1(
n
j
jn




1
1
)(
n
j
jn




1
1
)(
n
j
jn
n
exchanges
   
1 1 1
2
1 2 3 4 5 6 7
1 1 2
( ) ( 1) ( 1) ( 1) ( )
n n n
j j j
T n c c n c n c n j c n j c n j c n n
  
  
                
CENG 213 Data Structures
Sorting Algorithms
CENG 213 Data Structures
Sorting
• Sorting is a process that organizes a collection of data into either ascending or
descending order.
• An internal sort requires that the collection of data fit entirely in the
computer’s main memory.
• We can use an external sort when the collection of data cannot fit in the
computer’s main memory all at once but must reside in secondary storage such
as on a disk.
• We will analyze only internal sorting algorithms.
• Any significant amount of computer output is generally arranged in some
sorted order so that it can be interpreted.
• Sorting also has indirect uses. An initial sort of the data can significantly
enhance the performance of an algorithm.
• Majority of programming projects use a sort somewhere, and in many cases,
the sorting cost determines the running time.
• A comparison-based sorting algorithm makes ordering decisions only on the
basis of comparisons.
CENG 213 Data Structures
Sorting Algorithms
• There are many sorting algorithms, such as:
– Selection Sort
– Insertion Sort
– Bubble Sort
– Merge Sort
– Quick Sort
• The first three are the foundations for faster
and more efficient algorithms.
CENG 213 Data Structures
Selection Sort
• The list is divided into two sublists, sorted and unsorted,
which are divided by an imaginary wall.
• We find the smallest element from the unsorted sublist and
swap it with the element at the beginning of the unsorted
data.
• After each selection and swapping, the imaginary wall
between the two sublists move one element ahead,
increasing the number of sorted elements and decreasing
the number of unsorted ones.
• Each time we move one element from the unsorted sublist
to the sorted sublist, we say that we have completed a sort
pass.
• A list of n elements requires n-1 passes to completely
rearrange the data.
CENG 213 Data Structures
23 78 45 8 32 56
8 78 45 23 32 56
8 23 45 78 32 56
8 23 32 78 45 56
8 23 32 45 78 56
8 23 32 45 56 78
Original List
After pass 1
After pass 2
After pass 3
After pass 4
After pass 5
Sorted Unsorted
CENG 213 Data Structures
Selection Sort (cont.)
template <class Item>
void selectionSort( Item a[], int n) {
for (int i = 0; i < n-1; i++) {
int min = i;
for (int j = i+1; j < n; j++)
if (a[j] < a[min]) min = j;
swap(a[i], a[min]);
}
}
template < class Object>
void swap( Object &lhs, Object &rhs )
{
Object tmp = lhs;
lhs = rhs;
rhs = tmp;
}
CENG 213 Data Structures
Selection Sort -- Analysis
• In general, we compare keys and move items (or exchange items)
in a sorting algorithm (which uses key comparisons).
 So, to analyze a sorting algorithm we should count the
number of key comparisons and the number of moves.
• Ignoring other operations does not affect our final result.
• In selectionSort function, the outer for loop executes n-1 times.
• We invoke swap function once at each iteration.
 Total Swaps: n-1
 Total Moves: 3*(n-1) (Each swap has three moves)
CENG 213 Data Structures
Selection Sort – Analysis (cont.)
• The inner for loop executes the size of the unsorted part minus 1
(from 1 to n-1), and in each iteration we make one key
comparison.
 # of key comparisons = 1+2+...+n-1 = n*(n-1)/2
 So, Selection sort is O(n2)
• The best case, the worst case, and the average case of the
selection sort algorithm are same.  all of them are O(n2)
– This means that the behavior of the selection sort algorithm does not depend on the
initial organization of data.
– Since O(n2) grows so rapidly, the selection sort algorithm is appropriate only for
small n.
– Although the selection sort algorithm requires O(n2) key comparisons, it only
requires O(n) moves.
– A selection sort could be a good choice if data moves are costly but key
comparisons are not costly (short keys, long records).
CENG 213 Data Structures
Comparison of N, logN and N2
N O(LogN) O(N2)
16 4 256
64 6 4K
256 8 64K
1,024 10 1M
16,384 14 256M
131,072 17 16G
262,144 18 6.87E+10
524,288 19 2.74E+11
1,048,576 20 1.09E+12
1,073,741,824 30 1.15E+18
CENG 213 Data Structures
Insertion Sort
• Insertion sort is a simple sorting algorithm that is
appropriate for small inputs.
– Most common sorting technique used by card players.
• The list is divided into two parts: sorted and
unsorted.
• In each pass, the first element of the unsorted part
is picked up, transferred to the sorted sublist, and
inserted at the appropriate place.
• A list of n elements will take at most n-1 passes to
sort the data.
CENG 213 Data Structures
Original List
After pass 1
After pass 2
After pass 3
After pass 4
After pass 5
23 78 45 8 32 56
23 78 45 8 32 56
23 45 78 8 32 56
8 23 45 78 32 56
8 23 32 45 78 56
8 23 32 45 56 78
Sorted Unsorted
CENG 213 Data Structures
Insertion Sort Algorithm
template <class Item>
void insertionSort(Item a[], int n)
{
for (int i = 1; i < n; i++)
{
Item tmp = a[i];
for (int j=i; j>0 && tmp < a[j-1]; j--)
a[j] = a[j-1];
a[j] = tmp;
}
}
CENG 213 Data Structures
Insertion Sort – Analysis
• Running time depends on not only the size of the array but also
the contents of the array.
• Best-case:  O(n)
– Array is already sorted in ascending order.
– Inner loop will not be executed.
– The number of moves: 2*(n-1)  O(n)
– The number of key comparisons: (n-1)  O(n)
• Worst-case:  O(n2)
– Array is in reverse order:
– Inner loop is executed i-1 times, for i = 2,3, …, n
– The number of moves: 2*(n-1)+(1+2+...+n-1)= 2*(n-1)+ n*(n-1)/2  O(n2)
– The number of key comparisons: (1+2+...+n-1)= n*(n-1)/2  O(n2)
• Average-case:  O(n2)
– We have to look at all possible initial data organizations.
• So, Insertion Sort is O(n2)
CENG 213 Data Structures
Analysis of insertion sort
• Which running time will be used to characterize this
algorithm?
– Best, worst or average?
• Worst:
– Longest running time (this is the upper limit for the algorithm)
– It is guaranteed that the algorithm will not be worse than this.
• Sometimes we are interested in average case. But there are
some problems with the average case.
– It is difficult to figure out the average case. i.e. what is average
input?
– Are we going to assume all possible inputs are equally likely?
– In fact for most algorithms average case is same as the worst case.
CENG 213 Data Structures
Bubble Sort
• The list is divided into two sublists: sorted and
unsorted.
• The smallest element is bubbled from the unsorted
list and moved to the sorted sublist.
• After that, the wall moves one element ahead,
increasing the number of sorted elements and
decreasing the number of unsorted ones.
• Each time an element moves from the unsorted
part to the sorted part one sort pass is completed.
• Given a list of n elements, bubble sort requires up
to n-1 passes to sort the data.
CENG 213 Data Structures
Bubble Sort
23 78 45 8 32 56
8 23 78 45 32 56
8 23 32 78 45 56
8 23 32 45 78 56
8 23 32 45 56 78
Original List
After pass 1
After pass 2
After pass 3
After pass 4
CENG 213 Data Structures
Bubble Sort Algorithm
template <class Item>
void bubleSort(Item a[], int n)
{
bool sorted = false;
int last = n-1;
for (int i = 0; (i < last) && !sorted; i++){
sorted = true;
for (int j=last; j > i; j--)
if (a[j-1] > a[j]{
swap(a[j],a[j-1]);
sorted = false; // signal exchange
}
}
}
CENG 213 Data Structures
Bubble Sort – Analysis
• Best-case:  O(n)
– Array is already sorted in ascending order.
– The number of moves: 0  O(1)
– The number of key comparisons: (n-1)  O(n)
• Worst-case:  O(n2)
– Array is in reverse order:
– Outer loop is executed n-1 times,
– The number of moves: 3*(1+2+...+n-1) = 3 * n*(n-1)/2  O(n2)
– The number of key comparisons: (1+2+...+n-1)= n*(n-1)/2  O(n2)
• Average-case:  O(n2)
– We have to look at all possible initial data organizations.
• So, Bubble Sort is O(n2)
CENG 213 Data Structures
Mergesort
• Mergesort algorithm is one of two important divide-and-conquer
sorting algorithms (the other one is quicksort).
• It is a recursive algorithm.
– Divides the list into halves,
– Sort each halve separately, and
– Then merge the sorted halves into one sorted array.
CENG 213 Data Structures
Mergesort - Example
CENG 213 Data Structures
Merge
const int MAX_SIZE = maximum-number-of-items-in-array;
void merge(DataType theArray[], int first, int mid, int last)
{
DataType tempArray[MAX_SIZE]; // temporary array
int first1 = first; // beginning of first subarray
int last1 = mid; // end of first subarray
int first2 = mid + 1; // beginning of second subarray
int last2 = last; // end of second subarray
int index = first1; // next available location in tempArray
for ( ; (first1 <= last1) && (first2 <= last2); ++index) {
if (theArray[first1] < theArray[first2]) {
tempArray[index] = theArray[first1];
++first1;
}
else {
tempArray[index] = theArray[first2];
++first2;
} }
CENG 213 Data Structures
Merge (cont.)
// finish off the first subarray, if necessary
for (; first1 <= last1; ++first1, ++index)
tempArray[index] = theArray[first1];
// finish off the second subarray, if necessary
for (; first2 <= last2; ++first2, ++index)
tempArray[index] = theArray[first2];
// copy the result back into the original array
for (index = first; index <= last; ++index)
theArray[index] = tempArray[index];
} // end merge
CENG 213 Data Structures
Mergesort
void mergesort(DataType theArray[], int first, int last) {
if (first < last) {
int mid = (first + last)/2; // index of midpoint
mergesort(theArray, first, mid);
mergesort(theArray, mid+1, last);
// merge the two halves
merge(theArray, first, mid, last);
}
} // end mergesort
CENG 213 Data Structures
Mergesort - Example
6 3 9 1 5 4 7 2
5 4 7 26 3 9 1
6 3 9 1 7 2
5 4
6 3 19 5 4 27
3 6 1 9 2 7
4 5
2 4 5 71 3 6 9
1 2 3 4 5 7 8 9
divide
dividedividedivide
dividedivide
divide
merge merge
merge
merge
merge merge
merge
CENG 213 Data Structures
Mergesort – Example2
CENG 213 Data Structures
Mergesort – Analysis of Merge
A worst-case instance of the merge step in mergesort
CENG 213 Data Structures
Mergesort – Analysis of Merge (cont.)
Merging two sorted arrays of size k
• Best-case:
– All the elements in the first array are smaller (or larger) than all the
elements in the second array.
– The number of moves: 2k + 2k
– The number of key comparisons: k
• Worst-case:
– The number of moves: 2k + 2k
– The number of key comparisons: 2k-1
...... ......
......
0 k-1 0 k-1
0 2k-1
CENG 213 Data Structures
Mergesort - Analysis
Levels of recursive calls to mergesort, given an array of eight items
CENG 213 Data Structures
Mergesort - Analysis
.
.
.
.
.
.
. . . . . . . . . . . . . . . . .
2m
2m-1 2m-1
2m-2 2m-2 2m-2 2m-2
20 20
level 0 : 1 merge (size 2m-1)
level 1 : 2 merges (size 2m-2)
level 2 : 4 merges (size 2m-3)
level m
level m-1 : 2m-1 merges (size 20)
CENG 213 Data Structures
Mergesort - Analysis
• Worst-case –
The number of key comparisons:
= 20*(2*2m-1-1) + 21*(2*2m-2-1) + ... + 2m-1*(2*20-1)
= (2m - 1) + (2m - 2) + ... + (2m – 2m-1) ( m terms )
= m*2m –
= m*2m – 2m – 1
Using m = log n
= n * log2n – n – 1
 O (n * log2n )



1
0
2
m
i
i
CENG 213 Data Structures
Mergesort – Analysis
• Mergesort is extremely efficient algorithm with respect
to time.
– Both worst case and average cases are O (n * log2n )
• But, mergesort requires an extra array whose size
equals to the size of the original array.
• If we use a linked list, we do not need an extra array
– But, we need space for the links
– And, it will be difficult to divide the list into half ( O(n) )
CENG 213 Data Structures
Quicksort
• Like mergesort, Quicksort is also based on
the divide-and-conquer paradigm.
• But it uses this technique in a somewhat opposite manner,
as all the hard work is done before the recursive calls.
• It works as follows:
1. First, it partitions an array into two parts,
2. Then, it sorts the parts independently,
3. Finally, it combines the sorted subsequences by
a simple concatenation.
CENG 213 Data Structures
Quicksort (cont.)
The quick-sort algorithm consists of the following three steps:
1. Divide: Partition the list.
– To partition the list, we first choose some element from the list
for which we hope about half the elements will come before
and half after. Call this element the pivot.
– Then we partition the elements so that all those with values
less than the pivot come in one sublist and all those with
greater values come in another.
2. Recursion: Recursively sort the sublists separately.
3. Conquer: Put the sorted sublists together.
CENG 213 Data Structures
Partition
• Partitioning places the pivot in its correct place position within the array.
• Arranging the array elements around the pivot p generates two smaller sorting
problems.
– sort the left section of the array, and sort the right section of the array.
– when these two smaller sorting problems are solved recursively, our bigger
sorting problem is solved.
CENG 213 Data Structures
Partition – Choosing the pivot
• First, we have to select a pivot element among the elements of the
given array, and we put this pivot into the first location of the
array before partitioning.
• Which array item should be selected as pivot?
– Somehow we have to select a pivot, and we hope that we will
get a good partitioning.
– If the items in the array arranged randomly, we choose a pivot
randomly.
– We can choose the first or last element as a pivot (it may not
give a good partitioning).
– We can use different techniques to select the pivot.
CENG 213 Data Structures
Partition Function
template <class DataType>
void partition(DataType theArray[], int first, int last,
int &pivotIndex) {
// Partitions an array for quicksort.
// Precondition: first <= last.
// Postcondition: Partitions theArray[first..last] such that:
// S1 = theArray[first..pivotIndex-1] < pivot
// theArray[pivotIndex] == pivot
// S2 = theArray[pivotIndex+1..last] >= pivot
// Calls: choosePivot and swap.
// place pivot in theArray[first]
choosePivot(theArray, first, last);
DataType pivot = theArray[first]; // copy pivot
CENG 213 Data Structures
Partition Function (cont.)
// initially, everything but pivot is in unknown
int lastS1 = first; // index of last item in S1
int firstUnknown = first + 1; //index of 1st item in unknown
// move one item at a time until unknown region is empty
for (; firstUnknown <= last; ++firstUnknown) {
// Invariant: theArray[first+1..lastS1] < pivot
// theArray[lastS1+1..firstUnknown-1] >= pivot
// move item from unknown to proper region
if (theArray[firstUnknown] < pivot) { // belongs to S1
++lastS1;
swap(theArray[firstUnknown], theArray[lastS1]);
} // else belongs to S2
}
// place pivot in proper position and mark its location
swap(theArray[first], theArray[lastS1]);
pivotIndex = lastS1;
} // end partition
CENG 213 Data Structures
Partition Function (cont.)
Invariant for the partition algorithm
CENG 213 Data Structures
Partition Function (cont.)
Initial state of the array
CENG 213 Data Structures
Partition Function (cont.)
Moving theArray[firstUnknown] into S1 by swapping it with
theArray[lastS1+1] and by incrementing both lastS1 and firstUnknown.
CENG 213 Data Structures
Partition Function (cont.)
Moving theArray[firstUnknown] into S2 by incrementing firstUnknown.
CENG 213 Data Structures
Partition Function (cont.)
Developing the first
partition of an array
when the pivot is the
first item
CENG 213 Data Structures
Quicksort Function
void quicksort(DataType theArray[], int first, int last) {
// Sorts the items in an array into ascending order.
// Precondition: theArray[first..last] is an array.
// Postcondition: theArray[first..last] is sorted.
// Calls: partition.
int pivotIndex;
if (first < last) {
// create the partition: S1, pivot, S2
partition(theArray, first, last, pivotIndex);
// sort regions S1 and S2
quicksort(theArray, first, pivotIndex-1);
quicksort(theArray, pivotIndex+1, last);
}
}
CENG 213 Data Structures
Quicksort – Analysis
Worst Case: (assume that we are selecting the first element as pivot)
– The pivot divides the list of size n into two sublists of sizes 0 and n-1.
– The number of key comparisons
= n-1 + n-2 + ... + 1
= n2/2 – n/2  O(n2)
– The number of swaps =
= n-1 + n-1 + n-2 + ... + 1
swaps outside of the for loop swaps inside of the for loop
= n2/2 + n/2 - 1  O(n2)
• So, Quicksort is O(n2) in worst case
CENG 213 Data Structures
Quicksort – Analysis
• Quicksort is O(n*log2n) in the best case and average case.
• Quicksort is slow when the array is sorted and we choose the first
element as the pivot.
• Although the worst case behavior is not so good, its average case
behavior is much better than its worst case.
– So, Quicksort is one of best sorting algorithms using key comparisons.
CENG 213 Data Structures
Quicksort – Analysis
A worst-case partitioning with quicksort
CENG 213 Data Structures
Quicksort – Analysis
An average-case partitioning with quicksort
CENG 213 Data Structures
Radix Sort
• Radix sort algorithm different than other sorting algorithms that
we talked.
– It does not use key comparisons to sort an array.
• The radix sort :
– Treats each data item as a character string.
– First it groups data items according to their rightmost
character, and put these groups into order w.r.t. this rightmost
character.
– Then, combine these groups.
– We, repeat these grouping and combining operations for all
other character positions in the data items from the rightmost
to the leftmost character position.
– At the end, the sort operation will be completed.
CENG 213 Data Structures
Radix Sort – Example
mom, dad, god, fat, bad, cat, mad, pat, bar, him original list
(dad,god,bad,mad) (mom,him) (bar) (fat,cat,pat) group strings by rightmost letter
dad,god,bad,mad,mom,him,bar,fat,cat,pat combine groups
(dad,bad,mad,bar,fat,cat,pat) (him) (god,mom) group strings by middle letter
dad,bad,mad,bar,fat,cat,pat,him,god,mom combine groups
(bad,bar) (cat) (dad) (fat) (god) (him) (mad,mom) (pat) group strings by first letter
bad,bar,cat,dad,fat,god,him,mad,mom,par combine groups (SORTED)
CENG 213 Data Structures
Radix Sort – Example
CENG 213 Data Structures
Radix Sort - Algorithm
radixSort(inout theArray:ItemArray, in n:integer, in d:integer)
// sort n d-digit integers in the array theArray
for (j=d down to 1) {
Initialize 10 groups to empty
Initialize a counter for each group to 0
for (i=0 through n-1) {
k = jth digit of theArray[i]
Place theArrar[i] at the end of group k
Increase kth counter by 1
}
Replace the items in theArray with all the items in group 0,
followed by all the items in group 1, and so on.
}
CENG 213 Data Structures
Radix Sort -- Analysis
• The radix sort algorithm requires 2*n*d moves to sort n strings
of d characters each.
 So, Radix Sort is O(n)
• Although the radix sort is O(n), it is not appropriate as a general-
purpose sorting algorithm.
– Its memory requirement is d * original size of data (because each group
should be big enough to hold the original data collection.)
– For example, we need 27 groups to sort string of uppercase letters.
– The radix sort is more appropriate for a linked list than an array. (we will not need
the huge memory in this case)
CENG 213 Data Structures
Comparison of Sorting Algorithms
1
Parallel Sorting Algorithms
2
Potential Speedup
O(nlogn) optimal sequential sorting algorithm
Best we can expect based upon a sequential sorting algorithm
using n processors is:
)(log
)log(
complexitytimeparalleloptimal nO
n
nnO

3
Form the basis of several, if not most, classical sequential sorting
algorithms.
Two numbers, say A and B, are compared between P0 and P1.
Compare-and-Exchange
Sorting Algorithms
A
MIN
B
MAX
P0 P1
4
Compare-and-Exchange Two Sublists
5
Odd-Even Transposition Sort - example
Parallel time complexity: Tpar = O(n) (for P=n)
6
Odd-Even Transposition Sort – Example (N >> P)
P0 P1 P2 P3
13 7 12 8 5 4 6 1 3 9 2 10
Local sort
7 12 13 4 5 8 1 3 6 2 9 10
O-E
4 5 7 8 12 13 1 2 3 6 9 10
E-O
4 5 7 1 2 3 8 12 13 6 9 10
O-E
1 2 3 4 5 7 6 8 9 10 12 13
E-O
SORTED: 1 2 3 4 5 6 7 8 9 10 12 13
Each PE gets n/p numbers. First, PEs sort n/p locally, then they run
odd-even trans. algorithm each time doing a merge-split for 2n/p numbers.
Time complexity: Tpar = (Local Sort) + (p merge-splits) +(p exchanges)
Tpar = (n/p)log(n/p) + p*(n/p) + p*(n/p) = (n/p)log(n/p) + 2n
7
Parallelizing Mergesort
8
Mergesort - Time complexity
)log(
**** log
log
:Sequential
nnOT
nnn
nT
seq
n
n
seq


2
2
2
2
2
21 2
2

 
)(
log
:Parallel
nOT
n
nnnn
T
par
n
kpar
4
22222
12
2222
2
210
210












9
Bitonic Sequence
A bitonic sequence is defined as a list with no more than one
LOCAL MAXIMUM and no more than one LOCAL MINIMUM.
(Endpoints must be considered - wraparound )
Bitonic Mergesort
10
A bitonic sequence is a list with no more than one LOCAL
MAXIMUM and no more than one LOCAL MINIMUM.
(Endpoints must be considered - wraparound )
This is ok!
1 Local MAX; 1 Local MIN
The list is bitonic!
This is NOT bitonic! Why?
1 Local MAX; 2 Local MINs
11
1. Divide the bitonic list into two equal halves.
2. Compare-Exchange each item on the first half
with the corresponding item in the second half.
Binary Split
Result:
Two bitonic sequences where the numbers in one sequence are all less
than the numbers in the other sequence.
Repeated application of binary split
Bitonic list:
24 20 15 9 4 2 5 8 | 10 11 12 13 22 30 32 45
Result after Binary-split:
10 11 12 9 4 2 5 8 | 24 20 15 13 22 30 32 45
If you keep applying the BINARY-SPLIT to each half repeatedly, you
will get a SORTED LIST !
10 11 12 9 . 4 2 5 8 | 24 20 15 13 . 22 30 32 45
4 2 . 5 8 10 11 . 12 9 | 22 20 . 15 13 24 30 . 32 45
4 . 2 5 . 8 10 . 9 12 .11 15 . 13 22 . 20 24 . 30 32 . 45
2 4 5 8 9 10 11 12 13 15 20 22 24 30 32 45
Q: How many parallel steps does it take to sort ?
A: log n
13
Compare-and-exchange moves smaller numbers of each pair to left
and larger numbers of pair to right.
Given a bitonic sequence,
recursively performing ‘binary split’ will sort the list.
Sorting a bitonic sequence
14
To sort an unordered sequence, sequences are merged into larger bitonic
sequences, starting with pairs of adjacent numbers.
By a compare-and-exchange operation, pairs of adjacent numbers
formed into increasing sequences and decreasing sequences. Pairs form a
bitonic sequence of twice the size of each original sequences.
By repeating this process, bitonic sequences of larger and larger lengths
obtained.
In the final step, a single bitonic sequence sorted into a single increasing
sequence.
Sorting an arbitrary sequence
15
Bitonic Sort
Figure 2: Six phases of Bitonic Sort on a hypercube of dimension 3
Step No.
1
2
3
4
5
6
Processor No.
000 001 010 011 100 101 110 111
L H H L L H H L
L L H H H H L L
L H L H H L H L
L L L L H H H H
L L H H L L H H
L H L H L H L H
16
Bitonic sort (for N = P)
P0 P1 P2 P3 P4 P5 P6 P7
000 001 010 011 100 101 110 111
K G J M C A N F
Lo Hi Hi Lo Lo Hi High Low
G K M J A C N F
L L H H H H L L
G J M K N F A C
L H L H H L H L
G J K M N F C A
L L L L H H H H
G F C A N J K M
L L H H L L H H
C A G F K J N M
A C F G J K M N
17
In general, with n = 2k, there are k phases, each of 1, 2, 3, …, k steps.
Hence the total number of steps is:
Number of steps (P=n)
)(log
)(logloglog
nO
nn
iT
ni
i
bitonic
par
2
1 2
1


 


18
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
Bitonic sort (for N >> P)
Bitonic sort (for N >> P)
P0 P1 P2 P3 P4 P5 P6 P7
000 001 010 011 100 101 110 111
2 7 4 13 6 9 4 18 5 12 1 7 6 3 14 11 6 8 4 10 5 2 15 17
Local Sort (ascending):
2 4 7 6 9 13 4 5 18 1 7 12 3 6 14 6 8 11 4 5 10 2 15 17
L H H L L H High Low
2 4 6 7 9 13 7 12 18 1 4 5 3 6 6 8 11 14 10 15 17 2 4 5
L L H H H H L L
2 4 6 1 4 5 7 12 18 7 9 13 10 15 17 8 11 14 3 6 6 2 4 5
L H L H H L H L
1 2 4 4 5 6 7 7 9 12 13 18 14 15 17 8 10 11 5 6 6 2 3 4
L L L L H H H H
1 2 4 4 5 6 5 6 6 2 3 4 14 15 17 8 10 11 7 7 9 12 13 18
L L H H L L H H
1 2 4 2 3 4 5 6 6 4 5 6 7 7 9 8 10 11 14 15 17 12 13 18
L H L H L H L H
1 2 2 3 4 4 4 5 5 6 6 6 7 7 8 9 10 11 12 13 14 15 17 18
20
Number of steps (for N >> P)
)log...321(
P
N
2
P
N
log
P
N
MergeBitonicParallelSortLocal
P
Tbitonic
par


)}
2
)log1(log
(2
P
N
{log
P
N PP 

)logloglogN(log
P
N 2
PPP 
)logN(log
P
N 2
PT bitonic
par 
21
Computational time complexity using P=n processors
• Odd-even transposition sort - O(n)
• Parallel mergesort - O(n)
unbalanced processor load and Communication
• Bitonic Mergesort - O(log2n) (** BEST! **)
• ??? Parallel Shearsort - O(n logn) (* covered later *)
• Parallel Rank sort - O(n) (for P=n) (* covered later *)
Parallel sorting - summary
22
• Two network structures have received special attention:
mesh and hypercube
Parallel computers have been built with these networks.
• However, it is of less interest nowadays because networks got
faster and clusters became a viable option.
• Besides, network architecture is often hidden from the user.
• MPI provides libraries for mapping algorithms onto meshes,
and one can always use a mesh or hypercube algorithm even if
the underlying architecture is not one of them.
Sorting on Specific Networks
23
The layout of a sorted sequence on a mesh could be row by row or
snakelike:
Two-Dimensional Sorting on a Mesh
24
Alternate row and column sorting until list is fully sorted.
Alternate row directions to get snake-like sorting:
Shearsort
On a n x n Mesh, it takes 2log n phases to sort n2 numbers.
Therefore:
Since sorting n2 numbers sequentially takes Tseq = O(n2 log n);
25
Shearsort – Time complexity
meshnxnaon)log( nnOT shearsort
par 
n
efficiency
nO
T
T
Speedup
par
seq
shearsort
1


However,
)nP(for)( 2
26
Number of elements that are smaller than each selected element is
counted. This count provides the position of the selected number, its
“rank” in the sorted list.
• First a[0] is read and compared with each of the other numbers,
a[1] … a[n-1], recording the number of elements less than a[0].
Suppose this number is x. This is the index of a[0] in the final
sorted list.
• The number a[0] is copied into the final sorted list b[0] … b[n-1],
at location b[x]. Actions repeated with the other numbers.
Overall sequential time complexity of rank sort: Tseq = O(n2)
(not a good sequential sorting algorithm!)
Rank Sort
27
for (i = 0; i < n; i++) { /* for each number */
x = 0;
for (j = 0; j < n; j++) /* count number less than it */
if (a[i] > a[j]) x++;
b[x] = a[i]; /* copy number into correct place */
}
*This code needs to be fixed if duplicates exist in the sequence.
Sequential code
sequential time complexity of rank sort: Tseq = O(n2)
28
One number is assigned to each processor.
Pi finds the final index of a[i] in O(n) steps.
forall (i = 0; i < n; i++) { /* for each no. in parallel*/
x = 0;
for (j = 0; j < n; j++) /* count number less than it */
if (a[i] > a[j]) x++;
b[x] = a[i]; /* copy no. into correct place */
}
Parallel time complexity, O(n), as good as any sorting algorithm so
far. Can do even better if we have more processors.
Parallel Rank Sort (P=n)
Parallel time complexity: Tpar = O(n) (for P=n)
29
Use n processors to find the rank of one element. The final count,
i.e. rank of a[i] can be obtained using a binary addition operation
(global sum  MPI_Reduce())
Parallel Rank Sort with P = n2
Time complexity
(for P=n2):
Tpar = O(log n)
Can we do it in O(1) ?

Sorting

  • 1.
    Analysis of Algorithms CS477/677 Sorting – Part A Instructor: George Bebis (Chapter 2)
  • 2.
    2 The Sorting Problem •Input: – A sequence of n numbers a1, a2, . . . , an • Output: – A permutation (reordering) a1’, a2’, . . . , an’ of the input sequence such that a1’ ≤ a2’ ≤ · · · ≤ an’
  • 3.
  • 4.
    4 Why Study SortingAlgorithms? • There are a variety of situations that we can encounter – Do we have randomly ordered keys? – Are all keys distinct? – How large is the set of keys to be ordered? – Need guaranteed performance? • Various algorithms are better suited to some of these situations
  • 5.
    5 Some Definitions • InternalSort – The data to be sorted is all stored in the computer’s main memory. • External Sort – Some of the data to be sorted might be stored in some external, slower, device. • In Place Sort – The amount of extra space required to sort the data is constant with the input size.
  • 6.
    6 Stability • A STABLEsort preserves relative order of records with equal keys Sorted on first key: Sort file on second key: Records with key value 3 are not in order on first key!!
  • 7.
    7 Insertion Sort • Idea:like sorting a hand of playing cards – Start with an empty left hand and the cards facing down on the table. – Remove one card at a time from the table, and insert it into the correct position in the left hand • compare it with each of the cards already in the hand, from right to left – The cards held in the left hand are sorted • these cards were originally the top cards of the pile on the table
  • 8.
    8 To insert 12,we need to make room for it by moving first 36 and then 24. Insertion Sort
  • 9.
  • 10.
  • 11.
    11 Insertion Sort 5 24 6 1 3 input array left sub-array right sub-array at each iteration, the array is divided in two sub-arrays: sorted unsorted
  • 12.
  • 13.
    13 INSERTION-SORT Alg.: INSERTION-SORT(A) for j← 2 to n do key ← A[ j ] Insert A[ j ] into the sorted sequence A[1 . . j -1] i ← j - 1 while i > 0 and A[i] > key do A[i + 1] ← A[i] i ← i – 1 A[i + 1] ← key • Insertion sort – sorts the elements in place a8a7a6a5a4a3a2a1 1 2 3 4 5 6 7 8 key
  • 14.
    14 Loop Invariant forInsertion Sort Alg.: INSERTION-SORT(A) for j ← 2 to n do key ← A[ j ] Insert A[ j ] into the sorted sequence A[1 . . j -1] i ← j - 1 while i > 0 and A[i] > key do A[i + 1] ← A[i] i ← i – 1 A[i + 1] ← key Invariant: at the start of the for loop the elements in A[1 . . j-1] are in sorted order
  • 15.
    15 Proving Loop Invariants •Proving loop invariants works like induction • Initialization (base case): – It is true prior to the first iteration of the loop • Maintenance (inductive step): – If it is true before an iteration of the loop, it remains true before the next iteration • Termination: – When the loop terminates, the invariant gives us a useful property that helps show that the algorithm is correct – Stop the induction when the loop terminates
  • 16.
    16 Loop Invariant forInsertion Sort • Initialization: – Just before the first iteration, j = 2: the subarray A[1 . . j-1] = A[1], (the element originally in A[1]) – is sorted
  • 17.
    17 Loop Invariant forInsertion Sort • Maintenance: – the while inner loop moves A[j -1], A[j -2], A[j -3], and so on, by one position to the right until the proper position for key (which has the value that started out in A[j]) is found – At that point, the value of key is placed into this position.
  • 18.
    18 Loop Invariant forInsertion Sort • Termination: – The outer for loop ends when j = n + 1  j-1 = n – Replace n with j-1 in the loop invariant: • the subarray A[1 . . n] consists of the elements originally in A[1 . . n], but in sorted order • The entire array is sorted! jj - 1 Invariant: at the start of the for loop the elements in A[1 . . j-1] are in sorted order
  • 19.
    19 Analysis of InsertionSort cost times c1 n c2 n-1 0 n-1 c4 n-1 c5 c6 c7 c8 n-1   n j jt2    n j jt2 )1(    n j jt2 )1(     )1(11)1()1()( 8 2 7 2 6 2 5421    nctctctcncncncnT n j j n j j n j j INSERTION-SORT(A) for j ← 2 to n do key ← A[ j ] Insert A[ j ] into the sorted sequence A[1 . . j -1] i ← j - 1 while i > 0 and A[i] > key do A[i + 1] ← A[i] i ← i – 1 A[i + 1] ← key tj: # of times the while statement is executed at iteration j
  • 20.
    20 Best Case Analysis •The array is already sorted – A[i] ≤ key upon the first time the while loop test is run (when i = j -1) – tj = 1 • T(n) = c1n + c2(n -1) + c4(n -1) + c5(n -1) + c8(n-1) = (c1 + c2 + c4 + c5 + c8)n + (c2 + c4 + c5 + c8) = an + b = (n) “while i > 0 and A[i] > key”     )1(11)1()1()( 8 2 7 2 6 2 5421    nctctctcncncncnT n j j n j j n j j
  • 21.
    21 Worst Case Analysis •The array is in reverse sorted order – Always A[i] > key in while loop test – Have to compare key with all elements to the left of the j-th position  compare with j-1 elements  tj = j a quadratic function of n • T(n) = (n2) order of growth in n2 1 2 2 ( 1) ( 1) ( 1) 1 ( 1) 2 2 2 n n n j j j n n n n n n j j j                )1( 2 )1( 2 )1( 1 2 )1( )1()1()( 8765421              nc nn c nn c nn cncncncnT cbnan  2 “while i > 0 and A[i] > key”     )1(11)1()1()( 8 2 7 2 6 2 5421    nctctctcncncncnT n j j n j j n j j using we have:
  • 22.
    22 Comparisons and Exchangesin Insertion Sort INSERTION-SORT(A) for j ← 2 to n do key ← A[ j ] Insert A[ j ] into the sorted sequence A[1 . . j -1] i ← j - 1 while i > 0 and A[i] > key do A[i + 1] ← A[i] i ← i – 1 A[i + 1] ← key cost times c1 n c2 n-1 0 n-1 c4 n-1 c5 c6 c7 c8 n-1   n j jt2    n j jt2 )1(    n j jt2 )1(  n2/2 comparisons  n2/2 exchanges
  • 23.
    23 Insertion Sort -Summary • Advantages – Good running time for “almost sorted” arrays (n) • Disadvantages – (n2) running time in worst and average case –  n2/2 comparisons and exchanges
  • 24.
    24 Bubble Sort (Ex.2-2, page 38) • Idea: – Repeatedly pass through the array – Swaps adjacent elements that are out of order • Easier to implement, but slower than Insertion sort 1 2 3 n i 1329648 j
  • 25.
    25 Example 1329648 i = 1j 3129648 i = 1 j 3219648 i = 1 j 3291648 i = 1 j 3296148 i = 1 j 3296418 i = 1 j 3296481 i = 1 j 3296481 i = 2 j 3964821 i = 3 j 9648321 i = 4 j 9684321 i = 5 j 9864321 i = 6 j 9864321 i = 7 j
  • 26.
    26 Bubble Sort Alg.: BUBBLESORT(A) fori  1 to length[A] do for j  length[A] downto i + 1 do if A[j] < A[j -1] then exchange A[j]  A[j-1] 1329648 i = 1 j i
  • 27.
    27 Bubble-Sort Running Time Thus,T(n)= (n2) 2 2 1 1 1 ( 1) ( ) 2 2 2 n n n i i i n n n n where n i n i n              Alg.: BUBBLESORT(A) for i  1 to length[A] do for j  length[A] downto i + 1 do if A[j] < A[j -1] then exchange A[j]  A[j-1] T(n) = c1(n+1) +  n i in 1 )1(c2 c3   n i in 1 )( c4   n i in 1 )( = (n) + (c2 + c2 + c4)   n i in 1 )( Comparisons:  n2/2 Exchanges:  n2/2 c1 c2 c3 c4
  • 28.
    28 Selection Sort (Ex.2.2-2, page 27) • Idea: – Find the smallest element in the array – Exchange it with the element in the first position – Find the second smallest element and exchange it with the element in the second position – Continue until the array is sorted • Disadvantage: – Running time depends only slightly on the amount of order in the file
  • 29.
  • 30.
    30 Selection Sort Alg.: SELECTION-SORT(A) n← length[A] for j ← 1 to n - 1 do smallest ← j for i ← j + 1 to n do if A[i] < A[smallest] then smallest ← i exchange A[j] ↔ A[smallest] 1329648
  • 31.
    31 n2/2 comparisons Analysis of SelectionSort Alg.: SELECTION-SORT(A) n ← length[A] for j ← 1 to n - 1 do smallest ← j for i ← j + 1 to n do if A[i] < A[smallest] then smallest ← i exchange A[j] ↔ A[smallest] cost times c1 1 c2 n c3 n-1 c4 c5 c6 c7 n-1     1 1 )1( n j jn     1 1 )( n j jn     1 1 )( n j jn n exchanges     1 1 1 2 1 2 3 4 5 6 7 1 1 2 ( ) ( 1) ( 1) ( 1) ( ) n n n j j j T n c c n c n c n j c n j c n j c n n                       
  • 32.
    CENG 213 DataStructures Sorting Algorithms
  • 33.
    CENG 213 DataStructures Sorting • Sorting is a process that organizes a collection of data into either ascending or descending order. • An internal sort requires that the collection of data fit entirely in the computer’s main memory. • We can use an external sort when the collection of data cannot fit in the computer’s main memory all at once but must reside in secondary storage such as on a disk. • We will analyze only internal sorting algorithms. • Any significant amount of computer output is generally arranged in some sorted order so that it can be interpreted. • Sorting also has indirect uses. An initial sort of the data can significantly enhance the performance of an algorithm. • Majority of programming projects use a sort somewhere, and in many cases, the sorting cost determines the running time. • A comparison-based sorting algorithm makes ordering decisions only on the basis of comparisons.
  • 34.
    CENG 213 DataStructures Sorting Algorithms • There are many sorting algorithms, such as: – Selection Sort – Insertion Sort – Bubble Sort – Merge Sort – Quick Sort • The first three are the foundations for faster and more efficient algorithms.
  • 35.
    CENG 213 DataStructures Selection Sort • The list is divided into two sublists, sorted and unsorted, which are divided by an imaginary wall. • We find the smallest element from the unsorted sublist and swap it with the element at the beginning of the unsorted data. • After each selection and swapping, the imaginary wall between the two sublists move one element ahead, increasing the number of sorted elements and decreasing the number of unsorted ones. • Each time we move one element from the unsorted sublist to the sorted sublist, we say that we have completed a sort pass. • A list of n elements requires n-1 passes to completely rearrange the data.
  • 36.
    CENG 213 DataStructures 23 78 45 8 32 56 8 78 45 23 32 56 8 23 45 78 32 56 8 23 32 78 45 56 8 23 32 45 78 56 8 23 32 45 56 78 Original List After pass 1 After pass 2 After pass 3 After pass 4 After pass 5 Sorted Unsorted
  • 37.
    CENG 213 DataStructures Selection Sort (cont.) template <class Item> void selectionSort( Item a[], int n) { for (int i = 0; i < n-1; i++) { int min = i; for (int j = i+1; j < n; j++) if (a[j] < a[min]) min = j; swap(a[i], a[min]); } } template < class Object> void swap( Object &lhs, Object &rhs ) { Object tmp = lhs; lhs = rhs; rhs = tmp; }
  • 38.
    CENG 213 DataStructures Selection Sort -- Analysis • In general, we compare keys and move items (or exchange items) in a sorting algorithm (which uses key comparisons).  So, to analyze a sorting algorithm we should count the number of key comparisons and the number of moves. • Ignoring other operations does not affect our final result. • In selectionSort function, the outer for loop executes n-1 times. • We invoke swap function once at each iteration.  Total Swaps: n-1  Total Moves: 3*(n-1) (Each swap has three moves)
  • 39.
    CENG 213 DataStructures Selection Sort – Analysis (cont.) • The inner for loop executes the size of the unsorted part minus 1 (from 1 to n-1), and in each iteration we make one key comparison.  # of key comparisons = 1+2+...+n-1 = n*(n-1)/2  So, Selection sort is O(n2) • The best case, the worst case, and the average case of the selection sort algorithm are same.  all of them are O(n2) – This means that the behavior of the selection sort algorithm does not depend on the initial organization of data. – Since O(n2) grows so rapidly, the selection sort algorithm is appropriate only for small n. – Although the selection sort algorithm requires O(n2) key comparisons, it only requires O(n) moves. – A selection sort could be a good choice if data moves are costly but key comparisons are not costly (short keys, long records).
  • 40.
    CENG 213 DataStructures Comparison of N, logN and N2 N O(LogN) O(N2) 16 4 256 64 6 4K 256 8 64K 1,024 10 1M 16,384 14 256M 131,072 17 16G 262,144 18 6.87E+10 524,288 19 2.74E+11 1,048,576 20 1.09E+12 1,073,741,824 30 1.15E+18
  • 41.
    CENG 213 DataStructures Insertion Sort • Insertion sort is a simple sorting algorithm that is appropriate for small inputs. – Most common sorting technique used by card players. • The list is divided into two parts: sorted and unsorted. • In each pass, the first element of the unsorted part is picked up, transferred to the sorted sublist, and inserted at the appropriate place. • A list of n elements will take at most n-1 passes to sort the data.
  • 42.
    CENG 213 DataStructures Original List After pass 1 After pass 2 After pass 3 After pass 4 After pass 5 23 78 45 8 32 56 23 78 45 8 32 56 23 45 78 8 32 56 8 23 45 78 32 56 8 23 32 45 78 56 8 23 32 45 56 78 Sorted Unsorted
  • 43.
    CENG 213 DataStructures Insertion Sort Algorithm template <class Item> void insertionSort(Item a[], int n) { for (int i = 1; i < n; i++) { Item tmp = a[i]; for (int j=i; j>0 && tmp < a[j-1]; j--) a[j] = a[j-1]; a[j] = tmp; } }
  • 44.
    CENG 213 DataStructures Insertion Sort – Analysis • Running time depends on not only the size of the array but also the contents of the array. • Best-case:  O(n) – Array is already sorted in ascending order. – Inner loop will not be executed. – The number of moves: 2*(n-1)  O(n) – The number of key comparisons: (n-1)  O(n) • Worst-case:  O(n2) – Array is in reverse order: – Inner loop is executed i-1 times, for i = 2,3, …, n – The number of moves: 2*(n-1)+(1+2+...+n-1)= 2*(n-1)+ n*(n-1)/2  O(n2) – The number of key comparisons: (1+2+...+n-1)= n*(n-1)/2  O(n2) • Average-case:  O(n2) – We have to look at all possible initial data organizations. • So, Insertion Sort is O(n2)
  • 45.
    CENG 213 DataStructures Analysis of insertion sort • Which running time will be used to characterize this algorithm? – Best, worst or average? • Worst: – Longest running time (this is the upper limit for the algorithm) – It is guaranteed that the algorithm will not be worse than this. • Sometimes we are interested in average case. But there are some problems with the average case. – It is difficult to figure out the average case. i.e. what is average input? – Are we going to assume all possible inputs are equally likely? – In fact for most algorithms average case is same as the worst case.
  • 46.
    CENG 213 DataStructures Bubble Sort • The list is divided into two sublists: sorted and unsorted. • The smallest element is bubbled from the unsorted list and moved to the sorted sublist. • After that, the wall moves one element ahead, increasing the number of sorted elements and decreasing the number of unsorted ones. • Each time an element moves from the unsorted part to the sorted part one sort pass is completed. • Given a list of n elements, bubble sort requires up to n-1 passes to sort the data.
  • 47.
    CENG 213 DataStructures Bubble Sort 23 78 45 8 32 56 8 23 78 45 32 56 8 23 32 78 45 56 8 23 32 45 78 56 8 23 32 45 56 78 Original List After pass 1 After pass 2 After pass 3 After pass 4
  • 48.
    CENG 213 DataStructures Bubble Sort Algorithm template <class Item> void bubleSort(Item a[], int n) { bool sorted = false; int last = n-1; for (int i = 0; (i < last) && !sorted; i++){ sorted = true; for (int j=last; j > i; j--) if (a[j-1] > a[j]{ swap(a[j],a[j-1]); sorted = false; // signal exchange } } }
  • 49.
    CENG 213 DataStructures Bubble Sort – Analysis • Best-case:  O(n) – Array is already sorted in ascending order. – The number of moves: 0  O(1) – The number of key comparisons: (n-1)  O(n) • Worst-case:  O(n2) – Array is in reverse order: – Outer loop is executed n-1 times, – The number of moves: 3*(1+2+...+n-1) = 3 * n*(n-1)/2  O(n2) – The number of key comparisons: (1+2+...+n-1)= n*(n-1)/2  O(n2) • Average-case:  O(n2) – We have to look at all possible initial data organizations. • So, Bubble Sort is O(n2)
  • 50.
    CENG 213 DataStructures Mergesort • Mergesort algorithm is one of two important divide-and-conquer sorting algorithms (the other one is quicksort). • It is a recursive algorithm. – Divides the list into halves, – Sort each halve separately, and – Then merge the sorted halves into one sorted array.
  • 51.
    CENG 213 DataStructures Mergesort - Example
  • 52.
    CENG 213 DataStructures Merge const int MAX_SIZE = maximum-number-of-items-in-array; void merge(DataType theArray[], int first, int mid, int last) { DataType tempArray[MAX_SIZE]; // temporary array int first1 = first; // beginning of first subarray int last1 = mid; // end of first subarray int first2 = mid + 1; // beginning of second subarray int last2 = last; // end of second subarray int index = first1; // next available location in tempArray for ( ; (first1 <= last1) && (first2 <= last2); ++index) { if (theArray[first1] < theArray[first2]) { tempArray[index] = theArray[first1]; ++first1; } else { tempArray[index] = theArray[first2]; ++first2; } }
  • 53.
    CENG 213 DataStructures Merge (cont.) // finish off the first subarray, if necessary for (; first1 <= last1; ++first1, ++index) tempArray[index] = theArray[first1]; // finish off the second subarray, if necessary for (; first2 <= last2; ++first2, ++index) tempArray[index] = theArray[first2]; // copy the result back into the original array for (index = first; index <= last; ++index) theArray[index] = tempArray[index]; } // end merge
  • 54.
    CENG 213 DataStructures Mergesort void mergesort(DataType theArray[], int first, int last) { if (first < last) { int mid = (first + last)/2; // index of midpoint mergesort(theArray, first, mid); mergesort(theArray, mid+1, last); // merge the two halves merge(theArray, first, mid, last); } } // end mergesort
  • 55.
    CENG 213 DataStructures Mergesort - Example 6 3 9 1 5 4 7 2 5 4 7 26 3 9 1 6 3 9 1 7 2 5 4 6 3 19 5 4 27 3 6 1 9 2 7 4 5 2 4 5 71 3 6 9 1 2 3 4 5 7 8 9 divide dividedividedivide dividedivide divide merge merge merge merge merge merge merge
  • 56.
    CENG 213 DataStructures Mergesort – Example2
  • 57.
    CENG 213 DataStructures Mergesort – Analysis of Merge A worst-case instance of the merge step in mergesort
  • 58.
    CENG 213 DataStructures Mergesort – Analysis of Merge (cont.) Merging two sorted arrays of size k • Best-case: – All the elements in the first array are smaller (or larger) than all the elements in the second array. – The number of moves: 2k + 2k – The number of key comparisons: k • Worst-case: – The number of moves: 2k + 2k – The number of key comparisons: 2k-1 ...... ...... ...... 0 k-1 0 k-1 0 2k-1
  • 59.
    CENG 213 DataStructures Mergesort - Analysis Levels of recursive calls to mergesort, given an array of eight items
  • 60.
    CENG 213 DataStructures Mergesort - Analysis . . . . . . . . . . . . . . . . . . . . . . . 2m 2m-1 2m-1 2m-2 2m-2 2m-2 2m-2 20 20 level 0 : 1 merge (size 2m-1) level 1 : 2 merges (size 2m-2) level 2 : 4 merges (size 2m-3) level m level m-1 : 2m-1 merges (size 20)
  • 61.
    CENG 213 DataStructures Mergesort - Analysis • Worst-case – The number of key comparisons: = 20*(2*2m-1-1) + 21*(2*2m-2-1) + ... + 2m-1*(2*20-1) = (2m - 1) + (2m - 2) + ... + (2m – 2m-1) ( m terms ) = m*2m – = m*2m – 2m – 1 Using m = log n = n * log2n – n – 1  O (n * log2n )    1 0 2 m i i
  • 62.
    CENG 213 DataStructures Mergesort – Analysis • Mergesort is extremely efficient algorithm with respect to time. – Both worst case and average cases are O (n * log2n ) • But, mergesort requires an extra array whose size equals to the size of the original array. • If we use a linked list, we do not need an extra array – But, we need space for the links – And, it will be difficult to divide the list into half ( O(n) )
  • 63.
    CENG 213 DataStructures Quicksort • Like mergesort, Quicksort is also based on the divide-and-conquer paradigm. • But it uses this technique in a somewhat opposite manner, as all the hard work is done before the recursive calls. • It works as follows: 1. First, it partitions an array into two parts, 2. Then, it sorts the parts independently, 3. Finally, it combines the sorted subsequences by a simple concatenation.
  • 64.
    CENG 213 DataStructures Quicksort (cont.) The quick-sort algorithm consists of the following three steps: 1. Divide: Partition the list. – To partition the list, we first choose some element from the list for which we hope about half the elements will come before and half after. Call this element the pivot. – Then we partition the elements so that all those with values less than the pivot come in one sublist and all those with greater values come in another. 2. Recursion: Recursively sort the sublists separately. 3. Conquer: Put the sorted sublists together.
  • 65.
    CENG 213 DataStructures Partition • Partitioning places the pivot in its correct place position within the array. • Arranging the array elements around the pivot p generates two smaller sorting problems. – sort the left section of the array, and sort the right section of the array. – when these two smaller sorting problems are solved recursively, our bigger sorting problem is solved.
  • 66.
    CENG 213 DataStructures Partition – Choosing the pivot • First, we have to select a pivot element among the elements of the given array, and we put this pivot into the first location of the array before partitioning. • Which array item should be selected as pivot? – Somehow we have to select a pivot, and we hope that we will get a good partitioning. – If the items in the array arranged randomly, we choose a pivot randomly. – We can choose the first or last element as a pivot (it may not give a good partitioning). – We can use different techniques to select the pivot.
  • 67.
    CENG 213 DataStructures Partition Function template <class DataType> void partition(DataType theArray[], int first, int last, int &pivotIndex) { // Partitions an array for quicksort. // Precondition: first <= last. // Postcondition: Partitions theArray[first..last] such that: // S1 = theArray[first..pivotIndex-1] < pivot // theArray[pivotIndex] == pivot // S2 = theArray[pivotIndex+1..last] >= pivot // Calls: choosePivot and swap. // place pivot in theArray[first] choosePivot(theArray, first, last); DataType pivot = theArray[first]; // copy pivot
  • 68.
    CENG 213 DataStructures Partition Function (cont.) // initially, everything but pivot is in unknown int lastS1 = first; // index of last item in S1 int firstUnknown = first + 1; //index of 1st item in unknown // move one item at a time until unknown region is empty for (; firstUnknown <= last; ++firstUnknown) { // Invariant: theArray[first+1..lastS1] < pivot // theArray[lastS1+1..firstUnknown-1] >= pivot // move item from unknown to proper region if (theArray[firstUnknown] < pivot) { // belongs to S1 ++lastS1; swap(theArray[firstUnknown], theArray[lastS1]); } // else belongs to S2 } // place pivot in proper position and mark its location swap(theArray[first], theArray[lastS1]); pivotIndex = lastS1; } // end partition
  • 69.
    CENG 213 DataStructures Partition Function (cont.) Invariant for the partition algorithm
  • 70.
    CENG 213 DataStructures Partition Function (cont.) Initial state of the array
  • 71.
    CENG 213 DataStructures Partition Function (cont.) Moving theArray[firstUnknown] into S1 by swapping it with theArray[lastS1+1] and by incrementing both lastS1 and firstUnknown.
  • 72.
    CENG 213 DataStructures Partition Function (cont.) Moving theArray[firstUnknown] into S2 by incrementing firstUnknown.
  • 73.
    CENG 213 DataStructures Partition Function (cont.) Developing the first partition of an array when the pivot is the first item
  • 74.
    CENG 213 DataStructures Quicksort Function void quicksort(DataType theArray[], int first, int last) { // Sorts the items in an array into ascending order. // Precondition: theArray[first..last] is an array. // Postcondition: theArray[first..last] is sorted. // Calls: partition. int pivotIndex; if (first < last) { // create the partition: S1, pivot, S2 partition(theArray, first, last, pivotIndex); // sort regions S1 and S2 quicksort(theArray, first, pivotIndex-1); quicksort(theArray, pivotIndex+1, last); } }
  • 75.
    CENG 213 DataStructures Quicksort – Analysis Worst Case: (assume that we are selecting the first element as pivot) – The pivot divides the list of size n into two sublists of sizes 0 and n-1. – The number of key comparisons = n-1 + n-2 + ... + 1 = n2/2 – n/2  O(n2) – The number of swaps = = n-1 + n-1 + n-2 + ... + 1 swaps outside of the for loop swaps inside of the for loop = n2/2 + n/2 - 1  O(n2) • So, Quicksort is O(n2) in worst case
  • 76.
    CENG 213 DataStructures Quicksort – Analysis • Quicksort is O(n*log2n) in the best case and average case. • Quicksort is slow when the array is sorted and we choose the first element as the pivot. • Although the worst case behavior is not so good, its average case behavior is much better than its worst case. – So, Quicksort is one of best sorting algorithms using key comparisons.
  • 77.
    CENG 213 DataStructures Quicksort – Analysis A worst-case partitioning with quicksort
  • 78.
    CENG 213 DataStructures Quicksort – Analysis An average-case partitioning with quicksort
  • 79.
    CENG 213 DataStructures Radix Sort • Radix sort algorithm different than other sorting algorithms that we talked. – It does not use key comparisons to sort an array. • The radix sort : – Treats each data item as a character string. – First it groups data items according to their rightmost character, and put these groups into order w.r.t. this rightmost character. – Then, combine these groups. – We, repeat these grouping and combining operations for all other character positions in the data items from the rightmost to the leftmost character position. – At the end, the sort operation will be completed.
  • 80.
    CENG 213 DataStructures Radix Sort – Example mom, dad, god, fat, bad, cat, mad, pat, bar, him original list (dad,god,bad,mad) (mom,him) (bar) (fat,cat,pat) group strings by rightmost letter dad,god,bad,mad,mom,him,bar,fat,cat,pat combine groups (dad,bad,mad,bar,fat,cat,pat) (him) (god,mom) group strings by middle letter dad,bad,mad,bar,fat,cat,pat,him,god,mom combine groups (bad,bar) (cat) (dad) (fat) (god) (him) (mad,mom) (pat) group strings by first letter bad,bar,cat,dad,fat,god,him,mad,mom,par combine groups (SORTED)
  • 81.
    CENG 213 DataStructures Radix Sort – Example
  • 82.
    CENG 213 DataStructures Radix Sort - Algorithm radixSort(inout theArray:ItemArray, in n:integer, in d:integer) // sort n d-digit integers in the array theArray for (j=d down to 1) { Initialize 10 groups to empty Initialize a counter for each group to 0 for (i=0 through n-1) { k = jth digit of theArray[i] Place theArrar[i] at the end of group k Increase kth counter by 1 } Replace the items in theArray with all the items in group 0, followed by all the items in group 1, and so on. }
  • 83.
    CENG 213 DataStructures Radix Sort -- Analysis • The radix sort algorithm requires 2*n*d moves to sort n strings of d characters each.  So, Radix Sort is O(n) • Although the radix sort is O(n), it is not appropriate as a general- purpose sorting algorithm. – Its memory requirement is d * original size of data (because each group should be big enough to hold the original data collection.) – For example, we need 27 groups to sort string of uppercase letters. – The radix sort is more appropriate for a linked list than an array. (we will not need the huge memory in this case)
  • 84.
    CENG 213 DataStructures Comparison of Sorting Algorithms
  • 85.
  • 86.
    2 Potential Speedup O(nlogn) optimalsequential sorting algorithm Best we can expect based upon a sequential sorting algorithm using n processors is: )(log )log( complexitytimeparalleloptimal nO n nnO 
  • 87.
    3 Form the basisof several, if not most, classical sequential sorting algorithms. Two numbers, say A and B, are compared between P0 and P1. Compare-and-Exchange Sorting Algorithms A MIN B MAX P0 P1
  • 88.
  • 89.
    5 Odd-Even Transposition Sort- example Parallel time complexity: Tpar = O(n) (for P=n)
  • 90.
    6 Odd-Even Transposition Sort– Example (N >> P) P0 P1 P2 P3 13 7 12 8 5 4 6 1 3 9 2 10 Local sort 7 12 13 4 5 8 1 3 6 2 9 10 O-E 4 5 7 8 12 13 1 2 3 6 9 10 E-O 4 5 7 1 2 3 8 12 13 6 9 10 O-E 1 2 3 4 5 7 6 8 9 10 12 13 E-O SORTED: 1 2 3 4 5 6 7 8 9 10 12 13 Each PE gets n/p numbers. First, PEs sort n/p locally, then they run odd-even trans. algorithm each time doing a merge-split for 2n/p numbers. Time complexity: Tpar = (Local Sort) + (p merge-splits) +(p exchanges) Tpar = (n/p)log(n/p) + p*(n/p) + p*(n/p) = (n/p)log(n/p) + 2n
  • 91.
  • 92.
    8 Mergesort - Timecomplexity )log( **** log log :Sequential nnOT nnn nT seq n n seq   2 2 2 2 2 21 2 2    )( log :Parallel nOT n nnnn T par n kpar 4 22222 12 2222 2 210 210            
  • 93.
    9 Bitonic Sequence A bitonicsequence is defined as a list with no more than one LOCAL MAXIMUM and no more than one LOCAL MINIMUM. (Endpoints must be considered - wraparound ) Bitonic Mergesort
  • 94.
    10 A bitonic sequenceis a list with no more than one LOCAL MAXIMUM and no more than one LOCAL MINIMUM. (Endpoints must be considered - wraparound ) This is ok! 1 Local MAX; 1 Local MIN The list is bitonic! This is NOT bitonic! Why? 1 Local MAX; 2 Local MINs
  • 95.
    11 1. Divide thebitonic list into two equal halves. 2. Compare-Exchange each item on the first half with the corresponding item in the second half. Binary Split Result: Two bitonic sequences where the numbers in one sequence are all less than the numbers in the other sequence.
  • 96.
    Repeated application ofbinary split Bitonic list: 24 20 15 9 4 2 5 8 | 10 11 12 13 22 30 32 45 Result after Binary-split: 10 11 12 9 4 2 5 8 | 24 20 15 13 22 30 32 45 If you keep applying the BINARY-SPLIT to each half repeatedly, you will get a SORTED LIST ! 10 11 12 9 . 4 2 5 8 | 24 20 15 13 . 22 30 32 45 4 2 . 5 8 10 11 . 12 9 | 22 20 . 15 13 24 30 . 32 45 4 . 2 5 . 8 10 . 9 12 .11 15 . 13 22 . 20 24 . 30 32 . 45 2 4 5 8 9 10 11 12 13 15 20 22 24 30 32 45 Q: How many parallel steps does it take to sort ? A: log n
  • 97.
    13 Compare-and-exchange moves smallernumbers of each pair to left and larger numbers of pair to right. Given a bitonic sequence, recursively performing ‘binary split’ will sort the list. Sorting a bitonic sequence
  • 98.
    14 To sort anunordered sequence, sequences are merged into larger bitonic sequences, starting with pairs of adjacent numbers. By a compare-and-exchange operation, pairs of adjacent numbers formed into increasing sequences and decreasing sequences. Pairs form a bitonic sequence of twice the size of each original sequences. By repeating this process, bitonic sequences of larger and larger lengths obtained. In the final step, a single bitonic sequence sorted into a single increasing sequence. Sorting an arbitrary sequence
  • 99.
    15 Bitonic Sort Figure 2:Six phases of Bitonic Sort on a hypercube of dimension 3 Step No. 1 2 3 4 5 6 Processor No. 000 001 010 011 100 101 110 111 L H H L L H H L L L H H H H L L L H L H H L H L L L L L H H H H L L H H L L H H L H L H L H L H
  • 100.
    16 Bitonic sort (forN = P) P0 P1 P2 P3 P4 P5 P6 P7 000 001 010 011 100 101 110 111 K G J M C A N F Lo Hi Hi Lo Lo Hi High Low G K M J A C N F L L H H H H L L G J M K N F A C L H L H H L H L G J K M N F C A L L L L H H H H G F C A N J K M L L H H L L H H C A G F K J N M A C F G J K M N
  • 101.
    17 In general, withn = 2k, there are k phases, each of 1, 2, 3, …, k steps. Hence the total number of steps is: Number of steps (P=n) )(log )(logloglog nO nn iT ni i bitonic par 2 1 2 1      
  • 102.
    18 x x xx x x x x x x x x x x x x x x x x x x x x x x x x x x x x Bitonic sort (for N >> P)
  • 103.
    Bitonic sort (forN >> P) P0 P1 P2 P3 P4 P5 P6 P7 000 001 010 011 100 101 110 111 2 7 4 13 6 9 4 18 5 12 1 7 6 3 14 11 6 8 4 10 5 2 15 17 Local Sort (ascending): 2 4 7 6 9 13 4 5 18 1 7 12 3 6 14 6 8 11 4 5 10 2 15 17 L H H L L H High Low 2 4 6 7 9 13 7 12 18 1 4 5 3 6 6 8 11 14 10 15 17 2 4 5 L L H H H H L L 2 4 6 1 4 5 7 12 18 7 9 13 10 15 17 8 11 14 3 6 6 2 4 5 L H L H H L H L 1 2 4 4 5 6 7 7 9 12 13 18 14 15 17 8 10 11 5 6 6 2 3 4 L L L L H H H H 1 2 4 4 5 6 5 6 6 2 3 4 14 15 17 8 10 11 7 7 9 12 13 18 L L H H L L H H 1 2 4 2 3 4 5 6 6 4 5 6 7 7 9 8 10 11 14 15 17 12 13 18 L H L H L H L H 1 2 2 3 4 4 4 5 5 6 6 6 7 7 8 9 10 11 12 13 14 15 17 18
  • 104.
    20 Number of steps(for N >> P) )log...321( P N 2 P N log P N MergeBitonicParallelSortLocal P Tbitonic par   )} 2 )log1(log (2 P N {log P N PP   )logloglogN(log P N 2 PPP  )logN(log P N 2 PT bitonic par 
  • 105.
    21 Computational time complexityusing P=n processors • Odd-even transposition sort - O(n) • Parallel mergesort - O(n) unbalanced processor load and Communication • Bitonic Mergesort - O(log2n) (** BEST! **) • ??? Parallel Shearsort - O(n logn) (* covered later *) • Parallel Rank sort - O(n) (for P=n) (* covered later *) Parallel sorting - summary
  • 106.
    22 • Two networkstructures have received special attention: mesh and hypercube Parallel computers have been built with these networks. • However, it is of less interest nowadays because networks got faster and clusters became a viable option. • Besides, network architecture is often hidden from the user. • MPI provides libraries for mapping algorithms onto meshes, and one can always use a mesh or hypercube algorithm even if the underlying architecture is not one of them. Sorting on Specific Networks
  • 107.
    23 The layout ofa sorted sequence on a mesh could be row by row or snakelike: Two-Dimensional Sorting on a Mesh
  • 108.
    24 Alternate row andcolumn sorting until list is fully sorted. Alternate row directions to get snake-like sorting: Shearsort
  • 109.
    On a nx n Mesh, it takes 2log n phases to sort n2 numbers. Therefore: Since sorting n2 numbers sequentially takes Tseq = O(n2 log n); 25 Shearsort – Time complexity meshnxnaon)log( nnOT shearsort par  n efficiency nO T T Speedup par seq shearsort 1   However, )nP(for)( 2
  • 110.
    26 Number of elementsthat are smaller than each selected element is counted. This count provides the position of the selected number, its “rank” in the sorted list. • First a[0] is read and compared with each of the other numbers, a[1] … a[n-1], recording the number of elements less than a[0]. Suppose this number is x. This is the index of a[0] in the final sorted list. • The number a[0] is copied into the final sorted list b[0] … b[n-1], at location b[x]. Actions repeated with the other numbers. Overall sequential time complexity of rank sort: Tseq = O(n2) (not a good sequential sorting algorithm!) Rank Sort
  • 111.
    27 for (i =0; i < n; i++) { /* for each number */ x = 0; for (j = 0; j < n; j++) /* count number less than it */ if (a[i] > a[j]) x++; b[x] = a[i]; /* copy number into correct place */ } *This code needs to be fixed if duplicates exist in the sequence. Sequential code sequential time complexity of rank sort: Tseq = O(n2)
  • 112.
    28 One number isassigned to each processor. Pi finds the final index of a[i] in O(n) steps. forall (i = 0; i < n; i++) { /* for each no. in parallel*/ x = 0; for (j = 0; j < n; j++) /* count number less than it */ if (a[i] > a[j]) x++; b[x] = a[i]; /* copy no. into correct place */ } Parallel time complexity, O(n), as good as any sorting algorithm so far. Can do even better if we have more processors. Parallel Rank Sort (P=n) Parallel time complexity: Tpar = O(n) (for P=n)
  • 113.
    29 Use n processorsto find the rank of one element. The final count, i.e. rank of a[i] can be obtained using a binary addition operation (global sum  MPI_Reduce()) Parallel Rank Sort with P = n2 Time complexity (for P=n2): Tpar = O(log n) Can we do it in O(1) ?