1. 1
Introduction to Sorting
Sorting is the fundamental algorithmic problem in
mathematics and computer science.
It puts elements in a certain order. The most
commonly used orders are numerical and
lexicographical(alphabetical) order.
Efficient sorting is important to optimize the use
of other algorithms, as it is the first step in most
of them.
There are many sorting algorithms, but knowing
which one to use depends on the specific
problem at hand.
2. 2
Classification of Sorting algorithms
Sorting algorithms are often classified using different metrics:
Computational complexity: classification is based on worst, average and best
behavior of sorting a list of size (n).
For typical sorting algorithms acceptable/good behavior is O(n log n) and
unacceptable/bad behavior is Ω(n^2).
Ideal behavior for a sort is O(n).
Memory usage (and use of other computer resources):
Some sorting algorithms are “in place", such that only O(1) or O(log n) memory is
needed beyond the items being sorted.
Others need to create auxiliary data structures for data to be temporarily stored.
We’ve seen in class that mergesort needs more memory resources as it is not an
“in place” algorithm, while quicksort and heapsort are “in place”. Radix and
bucket sorts are not “in place”.
Recursion: some algorithms are either recursive or non-recursive.(e.g., mergesort is
recursive).
Stability: stable sorting algorithms maintain the relative order of elements/records
with equal keys/values. Radix and bucket sorts are stable.
General method: classification is based on how sort functions internally.
Methods used internally include insertion, exchange, selection, merging,
distribution etc. Bubble sort and quicksort are exchange sorts. Heapsort is a
selection sort.
3. 3
Classification of Sorting algorithms…contd
Comparison sorts: A comparison sort examines elements with a comparison
operator, which usually is the less than or equal to operator(≤). Comparison sorts
include:
Bubble sort
Insertion sort
Selection sort
Shell sort
Heapsort
Mergesort
Quicksort.
Non-Comparison sorts: these use other techniques to sort data, rather than using
comparison operations. These include:
Radix sort (examines individual bits of keys)
Bucket sort (examines bits of keys)
Counting sort (indexes using key values)
5. 5
Divide and Conquer
1.Base Case, solve the problem
directly if it is small enough
2.Divide the problem into two or
more similar and smaller
subproblems
3.Recursively solve the subproblems
4.Combine solutions to the
subproblems
6. Quick sort
A key step in the Quicksort algorithm is
partitioning the array
We choose some (any) number p in the
array to use as a pivot
We partition the array into three parts:
p
numbers
less than
p
numbers greater
than or equal to
p
p
7. Quick Sort(Partitioning )
Choose an array value (say, the first) to
use as the pivot
Starting from the left end, find the first
element that is greater than or equal to
the pivot
Searching backward from the right end,
find the first element that is less than
the pivot
Interchange (swap) these two elements
Repeat, searching from where we left
off, until done
9. Quick sort
To sort a[left...right]:
1. if left < right:
1.1. Partition a[left...right] such that:
all a[left...p-1] are less than a[p], and
all a[p+1...right] are >= a[p]
1.2. Quicksort a[left...p-1]
1.3. Quicksort a[p+1...right]
2. Terminate
10. Partitioning
To partition a[left...right]:
1. Set p = a[left], l = left + 1, r = right;
2. while l < r, do
2.1. while l < right && a[l] < p { l = l + 1 }
2.2. while r > left && a[r] >= p { r = r – 1}
2.3. if l < r { swap a[l] and a[r] }
3. a[left] = a[r]; a[r] = p;
4. Terminate
47. 47
Merge(A, left, middle, right)
1. n1 ← middle – left + 1
2. n2 ← right – middle
3. create array L[n1], R[n2]
4. for i ← 0 to n1-1 do L[i] ← A[left +i]
5. for j ← 0 to n2-1 do R[j] ← A[middle+j]
6. k ← i ← j ← 0
7. while i < n1 & j < n2
8. if L[i] < R[j]
9. A[k++] ← L[i++]
10. else
11. A[k++] ← R[j++]
12. while i < n1
13. A[k++] ← L[i++]
14. while j < n2
15. A[k++] ← R[j++]
n = n1+n2
Space: n
Time : cn for some constant c
Editor's Notes
Divide: if the initial array A has at least two elements (nothing needs to be done if A has zero or one elements), divide A into two subarrays each containing about half of the elements of A.
Conquer: sort the two subarrays using Merge Sort.
Combine: merging the two sorted subarray into one sorted array