0% found this document useful (0 votes)
27 views85 pages

7 Sorting

Sorting is a crucial topic in computer science due to its historical significance and efficiency in solving various problems. It involves organizing data and is essential for optimizing algorithms, with many sorting algorithms available, including Selection Sort and Insertion Sort. The document discusses the importance, applications, and analysis of sorting algorithms, highlighting their complexities and use cases.

Uploaded by

Serdar Bozdağ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views85 pages

7 Sorting

Sorting is a crucial topic in computer science due to its historical significance and efficiency in solving various problems. It involves organizing data and is essential for optimizing algorithms, with many sorting algorithms available, including Selection Sort and Insertion Sort. The document discusses the importance, applications, and analysis of sorting algorithms, highlighting their complexities and use cases.

Uploaded by

Serdar Bozdağ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 85

Sorting

CS 201
Importance of sorting
Why don’t CS profs ever stop talking about sorting?

1. Computers spend more time sorting than anything else, historically 25% on
mainframes.
2. Sorting is the best studied problem in computer science, with a variety of
different algorithms known.
3. Most of the interesting ideas we will encounter in the course can be taught in
the context of sorting, such as divide-and-conquer, randomized algorithms,
and lower bounds.

(slide by Steven Skiena)

2
Sorting
● Organize data into ascending / descending order
○ Useful in many applications
○ Any examples can you think of?

● Internal sort vs. external sort


○ We will analyze only internal sorting algorithms

● Sorting also has other uses. It can make an algorithm faster.


○ e.g., find the intersection of two sets

3
Efficiency of sorting
● Sorting is important because once a set of items is sorted, many other
problems become easy.
● Furthermore, using O(n log n) sorting algorithms leads naturally to
sub-quadratic algorithms for these problems.
● Large-scale data processing would be impossible if sorting took O(n2) time.

(slide by Steven Skiena)

4
Applications of sorting
● Closest pair: Given n numbers, find the pair which are closest to each other.
○ Once the numbers are sorted, the closest pair will be next to each other in sorted order, so an
O(n) linear scan completes the job.

● Element uniqueness: Given a set of n items, are they all unique or are there
any duplicates?
○ Sort them and do a linear scan to check all adjacent pairs.
○ This is a special case of closest pair above.
○ Complexity?

● Mode: Given a set of n items, which element occurs the largest number of
times? More generally, compute the frequency distribution.
○ How would you solve it?

5
Sorting algorithms
● There are many sorting algorithms, such as:
○ Selection Sort
○ Insertion Sort
○ Bubble Sort
○ Merge Sort
○ Quick Sort

● First three sorting algorithms are not so efficient, but the last two are efficient
sorting algorithms.

6
Selection sort
● List divided into two sublists, sorted and unsorted.

● Find the biggest element from the unsorted sublist. Swap it with the element
at the end of the unsorted data.

● After each selection and swapping, imaginary wall between the two sublists
move one element back.

● Sort pass: 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 sort data.


7
Selection sort

8
Selection sort
typedef type-of-array-item DataType;

void selectionSort(DataType theArray[], int n) {


for (int last = n-1; last >= 1; --last) {
int largest = indexOfLargest(theArray, last+1);
swap(theArray[largest], theArray[last]);
}
}

9
Selection sort
int indexOfLargest(const DataType theArray[], int size) {
int indexSoFar = 0;
for (int currentIndex=1; currentIndex<size;++currentIndex){
if (theArray[currentIndex] > theArray[indexSoFar])
indexSoFar = currentIndex;
}
return indexSoFar;
}
--------------------------------------------------------
void swap(DataType &x, DataType &y) {
DataType temp = x;
x = y;
y = temp;
}
10
Selection sort - analysis
● To analyze sorting, count simple operations.
● For sorting, important simple operations: key comparisons and number of
moves.
● In selectionSort() function, the for loop executes n-1 times.
● In selectionSort() function, we invoke swap() once at each iteration.
→ Total Swaps: n-1
→ Total Moves: 3*(n-1) (Each swap has three moves)

11
Selection sort - analysis
● In indexOfLargest() function, the for loop executes (for each size from n to 2),
and in each iteration we make one key comparison.
→ # of key comparisons = n-1 + n-2 + … + 2 + 1 = (n-1)*n/2
→ So, Selection sort is O(n2)
● The best case, worst case, and average case are the same → all O(n2)
○ Meaning: behavior of selection sort does not depend on initial organization of data.
○ Since O(n2) grows so rapidly, the selection sort algorithm is appropriate only for small n.

● Although selection sort requires O(n2) key comparisons, it only requires O(n)
moves.
○ Selection sort is good choice if data moves are costly but key comparisons are not costly
(short keys, long records).

12
Insertion sort
● Insertion sort is a simple sorting algorithm appropriate for small inputs.
○ Most common sorting technique used by card players.

● List 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 in place.

● List of n elements will take at most n-1 passes to sort data.

13
Insertion sort
● Assume input array: A[1..n]
● Iterate j from 2 to n
already sorted
j
iter j

insert into sorted array

j
after
iter j
sorted subarray
14
Insertion sort
Insertion-Sort (A)
1. for j ← 2 to n do
2. key ← A[j];
3. i ← j - 1;
4. while i > 0 and A[i] > key
do
5. A[i+1] ← A[i];
6. i ← i - 1;
endwhile
7. A[i+1] ← key;
endfor
15
Insertion sort
Insertion-Sort (A)
1. for j ← 2 to n do
Iterate over array elements j
2. key ← A[j];
3. i ← j - 1;
4. while i > 0 and A[i] > key Loop invariant:
The subarray A[1..j-1]
do is always sorted
5. A[i+1] ← A[i];
6. i ← i - 1; already sorted
j
endwhile
7. A[i+1] ← key;
endfor
key
16
Insertion sort
Insertion-Sort (A)
1. for j ← 2 to n do
2. key ← A[j];
3. i ← j - 1;
Shift right the entries
4. while i > 0 and A[i] > key in A[1..j-1] that are > key
do
5. A[i+1] ← A[i]; already sorted
j
6. i ← i - 1; < key > key
endwhile
j
7. A[i+1] ← key; < key > key
endfor
17
Insertion sort
Insertion-Sort (A)
1. for j ← 2 to n do
key
2. key ← A[j];
3. i ← j - 1;
4. while i > 0 and A[i] > key j
do < key > key

5. A[i+1] ← A[i]; now sorted


6. i ← i - 1;
endwhile
7. A[i+1] ← key; Insert key to the correct location
End of iter j: A[1..j] is sorted
endfor
18
Insertion sort - example
Insertion-Sort (A)
1. for j ← 2 to n do
2. key ← A[j];
3. i ← j - 1; 5 2 4 6 1 3
4. while i > 0 and A[i] > key
do
5. A[i+1] ← A[i];
6. i ← i - 1;
endwhile
7. A[i+1] ← key;
endfor
19
Insertion sort - example: iteration j=2
Insertion-Sort (A)
1. for j ← 2 to n do key=2
j
2. key ← A[j];
3. i ← j - 1; 5 2 4 6 1 3 initial
4. while i > 0 and A[i] > key sorted
do
>2 j
5. A[i+1] ← A[i];
5 2 4 6 1 3 shift
6. i ← i - 1;
endwhile
7. A[i+1] ← key;
sorted
insert
endfor 2 5 4 6 1 3
key
20
Insertion sort - example: iteration j=3
Insertion-Sort (A)
1. for j ← 2 to n do key=4
j
2. key ← A[j];
3. i ← j - 1; 2 5 4 6 1 3 initial
4. while i > 0 and A[i] > key sorted
do
5. A[i+1] ← A[i];
6. i ← i - 1;
endwhile
What are the entries at the
end of iteration j=3?
7. A[i+1] ← key;
endfor ? ? ? ? ? ?
21
Insertion sort - example: iteration j=3
Insertion-Sort (A)
1. for j ← 2 to n do key=4
j
2. key ← A[j];
3. i ← j - 1; 2 5 4 6 1 3 initial
4. while i > 0 and A[i] > key sorted
do j
<4 >4
5. A[i+1] ← A[i];
2 5 4 6 1 3 shift
6. i ← i - 1;
endwhile
7. A[i+1] ← key;
sorted
insert
endfor 2 4 5 6 1 3
key
22
Insertion sort - example: iteration j=4
Insertion-Sort (A)
1. for j ← 2 to n do key=6
j
2. key ← A[j];
3. i ← j - 1; 2 4 5 6 1 3 initial
4. while i > 0 and A[i] > key sorted
do j
<6
5. A[i+1] ← A[i];
2 4 5 6 1 3 shift
6. i ← i - 1;
endwhile
7. A[i+1] ← key;
sorted
insert
endfor 2 4 5 6 1 3
key
23
Insertion sort - example: iteration j=5
Insertion-Sort (A)
1. for j ← 2 to n do key=1
j
2. key ← A[j];
3. i ← j - 1; 2 4 5 6 1 3 initial
4. while i > 0 and A[i] > key sorted
do
5. A[i+1] ← A[i];
6. i ← i - 1;
endwhile
What are the entries at the
end of iteration j=5?
7. A[i+1] ← key;
endfor ? ? ? ? ? ?
24
Insertion sort - example: iteration j=5
Insertion-Sort (A)
1. for j ← 2 to n do key=1
j
2. key ← A[j];
3. i ← j - 1; 2 4 5 6 1 3 initial
4. while i > 0 and A[i] > key sorted
do j
>1 >1 >1 >1
5. A[i+1] ← A[i];
2 4 5 6 1 3 shift
6. i ← i - 1;
endwhile
7. A[i+1] ← key;
sorted
insert
endfor 1 2 4 5 6 3
key
25
Insertion sort - example: iteration j=6
Insertion-Sort (A)
1. for j ← 2 to n do
j key=3
2. key ← A[j];
3. i ← j - 1; 1 2 4 5 6 3 initial
4. while i > 0 and A[i] > key sorted
do j
<3 >3 >3 >3
5. A[i+1] ← A[i];
1 2 4 5 6 3 shift
6. i ← i - 1;
endwhile
7. A[i+1] ← key;
sorted
insert
endfor 1 2 3 4 5 6
key
26
Insertion sort - notes
● Items sorted in-place
○ Elements rearranged within array
○ At most constant number of items stored outside the array at any time (e.g., the variable key)
○ Input array A contains sorted output sequence when the algorithm ends

● Incremental approach
○ Having sorted A[1..j-1], place A[j] correctly so that A[1..j] is sorted

27
Insertion sort
void insertionSort(DataType theArray[], int n) {

for (int unsorted = 1; unsorted < n; ++unsorted) {

DataType nextItem = theArray[unsorted];


int loc = unsorted;

for ( ;(loc > 0) && (theArray[loc-1] > nextItem); --loc)


theArray[loc] = theArray[loc-1];

theArray[loc] = nextItem;
}
}

28
Insertion sort

Sorted Unsorted

29
Insertion sort - analysis
● What is the complexity of insertion sort? → Depends on array contents
● 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 p-1 times, for p = 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) 30
Insertion sort - analysis
● Which running time will be used to characterize this algorithm?
○ Best, worst or average?

→ Worst case:
○ 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 problems:


○ Difficult to figure out average case, i.e., what is the average input?
○ Are we going to assume all possible inputs are equally likely?
○ In fact, for most algorithms average case is the same as the worst case.

31
Bubble sort
● List divided into two sublists: sorted and unsorted.
● The largest element is bubbled from the unsorted list and moved to the sorted
sublist.
● After that, the wall moves one element back, increasing the number of sorted
elements and decreasing the number of unsorted ones.
● One sort pass: each time an element moves from the unsorted part to the
sorted part.
● Given a list of n elements, bubble sort requires up to n-1 passes (maximum
passes) to sort data.

32
Bubble sort

33
Bubble sort
void bubbleSort(DataType theArray[], int n) {
bool sorted = false;

for (int pass = 1; (pass < n) && !sorted; ++pass) {


sorted = true;
for (int index = 0; index < n-pass; ++index) {
int nextIndex = index + 1;
if (theArray[index] > theArray[nextIndex]) {
swap(theArray[index], theArray[nextIndex]);
sorted = false; // signal exchange
}
}
}
}
34
Bubble sort - analysis
● Worst-case: → O(n2)
○ Array is in reverse order.
○ Inner loop is executed n-1 times,
○ The number of moves: 3*(n-1+n-2+...+2+1) = 3 * n*(n-1)/2 → O(n2)
○ The number of key comparisons: (n-1+n-2+...+2+1)= n*(n-1)/2 → O(n2)
● 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)
● Average-case: → O(n2)
○ We have to look at all possible initial data organizations.
● So, Bubble Sort is O(n2) 35
Merge sort
● One of two important divide-and-conquer sorting algorithms
○ Other one is Quick sort

● It is a recursive algorithm.
○ Divide the list into halves,
○ Sort each half separately, and
○ Then merge the sorted halves into one sorted array.

36
Merge sort - basic idea

Input array A
Divide
sort this half sort this half
Conquer

merge two sorted halves


Combine

37
Merge sort
Merge-Sort (A, p, r)

if p = r then return;
else
q ← ⎣ (p+r)/2 ⎦; (Divide)
Merge-Sort (A, p, q); (Conquer)
Merge-Sort (A, q+1, r); (Conquer)
Merge (A, p, q, r); (Combine)
endif

● Call Merge-Sort(A,1,n) to sort A[1..n]


● Recursion bottoms out when subsequences have length 1

38
Merge sort - example
Merge-Sort (A, p, r) p q r

if p = r then 5 2 4 6 1 3
return p q r
else
q ← ⎣ (p+r)/2 ⎦ 2 4 5 1 3 6

Merge-Sort (A, p, q)
Merge-Sort (A, q+1, r) 1 2 3 4 5 6

Merge(A, p, q, r)
endif
39
How to merge 2 sorted subarrays?

A[p..q] 2 4 5

1 2 3 4 5 6
A[q+1..r] 1 3 6

● What is the complexity of this step? Θ(n)

40
Merge sort - correctness
Merge-Sort (A, p, r) Base case: p = r
→Trivially correct
if p = r then
return Inductive hypothesis: Merge-Sort
else is correct for any subarray that is a
q ← ⎣ (p+r)/2 ⎦ strict (smaller) subset of A[p, r].

Merge-Sort (A, p, q) General case: Merge-Sort is


Merge-Sort (A, q+1, r) correct for A[p, r].
→From inductive hypothesis and
Merge(A, p, q, r) correctness of Merge.
endif
41
Merge sort - another example

42
Merge sort
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

43
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;
}
} …
44
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

45
Merge sort - another example
6 3 9 1 5 4 7 2
divide
6 3 9 1 5 4 7 2
divide divide
6 3 9 1 5 4 7 2

divide divide divide divide


6 3 9 1 5 4 7 2
merge merge merge merge
3 6 1 9 4 5 2 7
merge merge
1 3 6 9 2 4 5 7
merge
1 2 3 4 5 6 7 9
46
Merge sort - another example

47
Merge sort - analysis of merge
A worst-case instance of the merge step in mergesort

48
Merge sort - analysis of merge
0 k-1 0 k-1
Merging two sorted arrays of size k .......... ..........

0 2k-1
..........
● 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

49
Merge sort - complexity
Merge-Sort (A, p, r) T(n)

if p = r then
return Θ(1)
else
q ← ⎣ (p+r)/2 ⎦ Θ(1)

Merge-Sort (A, p, q) T(n/2)


Merge-Sort (A, q+1, r) T(n/2)

Merge(A, p, q, r) Θ(n)
endif
50
Merge sort - recurrence
● Describe a function recursively in terms of itself
● To analyze the performance of recursive algorithms

● For merge sort:

Θ(1) if n=1
T(n) =
2T(n/2) + Θ(n) otherwise

51
How to solve for T(n)?
● Generally, we will assume T(n) = Θ(1) for sufficiently small n

● The recurrence can be rewritten as:

T(n) = 2 T(n/2) + Θ(n)

● How to solve this recurrence?

52
Solve recurrence: T(n) = 2T(n/2) + Θ(n)

Θ(n)

T(n/2) T(n/2)

53
Solve recurrence: T(n) = 2T(n/2) + Θ(n)

Θ(n)

Θ(n/2) Θ(n/2)
T(n/2)

each size
subprobs

halved
2x

T(n/4) T(n/4) T(n/4) T(n/4)

54
Solve recurrence: T(n) = 2T(n/2) + Θ(n)

Θ(n) Θ(n)

Θ(n/2) Θ(n/2) Θ(n)


Θ(lgn)

Θ(n/4) Θ(n/4) Θ(n/4) Θ(n/4)

Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(n)
Θ(n) Total: Θ(nlgn)
55
Merge sort - analysis

Levels of recursive calls to mergesort, given an array of eight items

56
Merge sort - analysis

2m level 0 : 1 merge (size 2m-1)


2m-1 2m-1 level 1 : 2 merges (size 2m-2)
level 2 : 4 merges (size 2m-3)
2m-2 2m-2 2m-2 2m-2
. .
. .
. . level m-1 : 2m-1 merges (size 20)
20 20 level m
.................

57
Merge sort - 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) note that n=2m

= n * log2n – n + 1

→ O (n * log2n)
58
Merge sort - analysis
● There are possibilities when merging two sorted lists of size k.

● k=2 → = = 6 different cases

# of key comparisons = ((2*2)+(4*3)) / 6 = 16/6 = 2 + 2/3

Average # of key comparisons in merge sort is


n * log2n – 1.25*n – O(1)

→ O (n * log2n )

59
Merge sort - analysis
● Merge sort is an extremely efficient algorithm with respect to time.
○ Both worst-case and average-case are O (n * log2n)

● But, merge sort 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) )

60
Quick sort
● Like Merge sort, Quick sort is based on divide-and-conquer paradigm.

● But somewhat opposite to Merge sort


○ Merge sort: Hard work done after recursive call
○ Quick sort: Hard work done before recursive call

● Algorithm
1. First, partition an array into two parts.
2. Then, sort each part independently.
3. Finally, combine sorted parts by a simple concatenation.

61
Quick sort
The Quick sort algorithm consists of the following three steps:

1. Divide: Partition the list.


1.1 Choose some element from list. Call this element the pivot.
- We hope about half the elements will come before and half after.
1.2 Then partition the elements so that all those with values less than the pivot come in one sublist
and all those with values greater than or equal to come in another.

2. Recursion: Recursively sort the sublists separately.

3. Conquer: Put the sorted sublists together.

62
Partition
● Partitioning places the pivot in its correct position within the array.

● Arranging elements around pivot p generates two smaller sorting problems.


○ Sort left section of the array, and sort right section of the array.
○ When these two smaller sorting problems are solved recursively, our bigger sorting problem is
solved.

63
Divide: partition the array around a pivot element
1. Choose a pivot element x
2. Rearrange the array such that:
Left subarray: All elements < x
Right subarray: All elements ≥ x

Input: 5 3 2 7 4 1 3 6 e.g. x = 5

After partitioning: 3 3 2 1 4 5 7 6
<5 ≥5

64
Conquer: recursively sort the subarrays
Note: Everything in the left subarray < everything in the right subarray

3 3 2 1 4 5 7 6

sort recursively sort recursively

After conquer: 1 2 3 3 4 5 6 7

Note: Combine is trivial after conquer. Array already sorted.

65
Partition - choosing the pivot
● First, select a pivot element among the elements of the given array, and put
the 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.

66
Partition function
Initial state of the array

67
Partition function
Invariant for the partition algorithm

68
Partition function
Moving theArray[firstUnknown] into S1 by swapping it with
theArray[lastS1+1] and by incrementing both lastS1 and firstUnknown.

69
Partition function
Moving theArray[firstUnknown] into S2 by incrementing firstUnknown.

70
Partition function
Developing the first partition
of an array when the pivot is
the first item

71
Quick sort function
void quicksort(DataType theArray[], int first, int last) {
// Precondition: theArray[first..last] is an array.
// Postcondition: theArray[first..last] is sorted.

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);
}
}
72
Partition function
void partition(DataType theArray[], int first, int last, int &pivotIndex) {
// Precondition: theArray[first..last] is an array; first <= last.
// Postcondition: Partitions theArray[first..last] such that:
// S1 = theArray[first..pivotIndex-1] < pivot
// theArray[pivotIndex] == pivot
// S2 = theArray[pivotIndex+1..last] >= pivot

// place pivot in theArray[first]


choosePivot(theArray, first, last);

DataType pivot = theArray[first]; // copy pivot

73
Partition function
// initially, everything but pivot is in unknown
int lastS1 = first; // index of last item in S1
int firstUnknown = first + 1; // index of first 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;
}
74
Quick sort - 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, Quick sort is O(n2) in worst case

75
Quick sort - analysis
● Quick sort is O(n*log2n) in the best case and average case.

● Quick sort is slow when the array is already 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, Quick sort is one of best sorting algorithms using key comparisons.

76
Quick sort - analysis
A worst-case partitioning with quicksort

77
Quick sort - analysis
An average-case partitioning with quicksort

78
Other sorting algorithms?
Many! For example:

● Radix sort
● Shell sort
● Comb sort
● Heapsort
● Counting sort
● Bucket sort
● Distribution sort
● Timsort

● e.g., Check http://en.wikipedia.org/wiki/Sorting_algorithm for a table that


compares sorting algorithms.
79
Radix sort
● Radix sort algorithm is different from other sorting algorithms that we saw.
○ It does not use key comparisons to sort an array.

● Radix sort :
○ Treats each data item as a character string.
○ First, group data items according to their rightmost character, and put these groups into order
w.r.t. this rightmost character.
○ Then, combine these groups.
○ Repeat these grouping and combining operations for all other character positions in the data
items from the rightmost to the leftmost character position.

80
Radix sort - example

81
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 middle letter

bad,bar,cat,dad,fat,god,him,mad,mom,par combine groups (SORTED)

82
Radix sort - algorithm
radixSort(int theArray[], 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 theArray[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
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 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, to sort strings of uppercase letters, we need 27 groups.
○ Radix sort is more appropriate for a linked list than an array (we will not need the
huge memory in this case).

84
Comparison of sorting algorithms

85

You might also like