You are on page 1of 22

Sorting Techniques

 Bubble Sort
 Insertion Sort
 Selection Sort
 Quick Sort
 Merge Sort
Bubble Sort
Here DATA is an array with N elements. This algorithm
sorts the elements in DATA.
BUBBLE(DATA, N)
1. Repeat step 2 and 3 for K=1 to N-1.
2. Set PTR:=1. [Initialize pass pointer PTR.]
3. Repeat while PTR ≤ N-K: Execute Pass.]
a) IF DATA[PTR] > DATA[PTR + 1], then:
Interchange DATA[PTR] and DATA[PTR + 1].
[End of If structure]
b) Set PTR:=PTR +1.
[End of inner loop]
[End of Step 1 outer loop.]
4. Exit.
Example of Bubble Sort
7 2 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8

2 7 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8

2 7 8 5 4 2 5 7 4 8 2 4 5 7 8 (done)
2 7 5 8 4 2 5 4 7 8

2 7 5 4 8

Pass 1 Pass 2 Pass 3 Pass 4


Bubble Sort Complexity
 Complexity analysis of Bubble sort method:
Traditionally, time for sorting an array is measured in
terms of number of comparisons. The number f(n) of
comparisons in bubble sort is easily computed.
Specifically, there are n-1 comparisons during first pass,
n-2 comparisons in second pass and so on.
Thus, f(n) = (n-1) +(n-2) +-------+2+1
= n(n-1)/2 = n2 / 2 – n/2
= O(n2)
Thus time required to sort an array using bubble sort
method is proportional to n2 where n is number of
input items. Thus complexity of bubble sort is O(n2)
Insertion Sort
INSERTION_SORT (A, N)
1. Set A[0] = -∞.
2. Repeat Step 3 to 5 for K = 2, 3, …, N:
3. Set TEMP = A[K] and PTR = K – 1.
4. Repeat while TEMP < A[PTR]:
(a) Set A[PTR+1] = A[PTR]
(b) Set PTR = PTR – 1.
[End of Loop.]
5. Set A[PTR+1] = TEMP.
[End of Loop 2.]
6. Return.
Insertion Sort Example
 Sort: 34 8 64 51 32 21
 34 8 64 51 32 21
 The algorithm sees that 8 is smaller than 34 so it swaps.
 8 34 64 51 32 21
 51 is smaller than 64, so they swap.
 8 34 51 64 32 21
 8 34 51 64 32 21 (from previous slide)
 The algorithm sees 32 as another smaller number and moves it
to its appropriate location between 8 and 34.
 8 32 34 51 64 21
 The algorithm sees 21 as another smaller number and moves
into between 8 and 32.
 Final sorted numbers:
 8 21 32 34 51 64
Insertion Sort Complexity
 This Sorting algorithm is frequently used when n is very
small.

 Worst case occurs when array is in reverse order. The inner


loop must use K – 1 comparisons.

f(n) = 1 + 2 + 3 + ….+ (n – 1)
= n(n – 1)/2
= O(n2)

 In average case, there will be approximately (K – 1)/2


comparisons in the inner loop.
f(n) = (1 + 2 + 3 + ….+ (n – 1))/2
Selection Sort
This algorithm sorts an array A with N elements.
SELECTION(A, N)
1. Repeat steps 2 and 3 for k=1 to N-1:
2. Call MIN(A, K, N, LOC).
3. [Interchange A[k] and A[LOC]]
Set Temp:= A[k], A[k]:= A[LOC] and A[LOC]:=Temp.
[End of step 1 Loop.]
4. Exit.
MIN(A, K, N, LOC).
1. Set MIN := A[K] and LOC:= K.
2. Repeat for j=k+1 to N:
If Min>A[j], then: Set Min:= A[j] and LOC:=J.
[End of if structure]
3. Return.
Selection Sort Example
8 4 6 9 2 3 1 1 2 3 4 9 6 8

1 4 6 9 2 3 8 1 2 3 4 6 9 8

1 2 6 9 4 3 8 1 2 3 4 6 8 9

1 2 3 9 4 6 8 1 2 3 4 6 8 9
Selection Sort Complexity
The number f(n) of comparisons in selection sort
algorithm is independent of original order of
elements. There are n-1 comparisons during
pass 1 to find the smallest element, n-2
comparisons during pass 2 to find the second
smallest element, and so on.
Accordingly,
f (n) = (n-1)+(n-2)+-----+2+1
= n(n-1)/2
= O(n2)
The f (n) holds the same value O(n2) both for
Quick sort algorithm
PARTITION(A, BEG, END, LOC)
1. 1- Set LEFT = BEG, RIGHT=END, LOC=BEG
2. 2- [Scan from right to left]
(a) Repeat while A[LOC] <=A[RIGHT] and LOC != RIGHT
RIGHT = RIGHT-1
[End of Loop]
(b) If LOC == RIGHT then return
(c) If A[LOC] > A[RIGHT] then
(i) Interchange A[LOC] and A[RIGHT]
(ii) Set LOC = RIGHT
[End of if structure]
3. 3- [Scan from left to right]
(a) Repeat while A[LEFT] <=A[LOC] and LEFT != LOC
LEFT = LEFT+1
[End of Loop]
(b) If LOC == LEFT then return
(c) If A[LEFT] > A[LOC] then
(i) Interchange A[LEFT] and A[LOC]
(ii) Set LOC = LEFT
(iii) Go to step 2.
[End of if structure]
Quick sort algorithm
QUICKSORT()
1. 1- TOP = -1
2. 2- If N >1 then TOP = TOP + 1, LOWER[TOP] = 0, UPPER[TOP] = N-1
3. 3- Repeat steps 4 to 7 while TOP != -1
4. 4- [Pop sublist from stacks]
Set BEG = LOWER[TOP] , END = UPPER[TOP]
TOP = TOP – 1
5- Call PARTITION (A, BEG, END, LOC)
6- [ Push left sublist onto stacks when it has 2 or more elements]
If BEG < LOC-1 then
a. TOP = TOP + 1
b. LOWER[TOP] = BEG
c. UPPER[TOP] = LOC-1
[End of if Structure]
7- [ Push right sublist onto stacks when it has 2 or more elements]
If LOC+1 < END then
a. TOP = TOP+1
b. LOWER[TOP] = LOC+1
c. UPPER[TOP] = END
[End of if Structure]
[End of step 3 loop]
8- Exit
Complexity of Quick Sort
 Worst case
 O(n2)

 Average case
 O(n log n)
Merge Sort
• Divide and Conquer
• Recursive in structure
– Divide the problem into sub-problems that are
similar to the original but smaller in size
– Conquer the sub-problems by solving them
recursively. If they are small enough, just solve
them in a straightforward manner.
– Combine the solutions to create a solution to the
original problem
An Example: Merge Sort
Sorting Problem: Sort a sequence of n elements into
non-decreasing order.

 Divide: Divide the n-element sequence to be sorted


into two subsequences of n/2 elements each
 Conquer: Sort the two subsequences recursively using
merge sort.
 Combine: Merge the two sorted subsequences to
produce the sorted answer.
Merge Sort – Example
Original Sequence Sorted Sequence

18 26 32 6 43 15 9 1 1 6 9 15 18 26 32 43

18 26 32 6 43 15 9 1 6 18 26 32 1 9 15 43
43

18 26 32 6 43 15 9 1 18 26 6 32 15 43 1 9

18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1

18 26 32 6 43 15 9 1
Merge-Sort (A, p, r)
INPUT: a sequence of n numbers stored in array A
OUTPUT: an ordered sequence of n numbers

MergeSort (A, p, r) // sort A[p..r] by divide & conquer


1 if p < r
2 then q  (p+r)/2
3 MergeSort (A, p, q)
4 MergeSort (A, q+1, r)
5 Merge (A, p, q, r) // merges A[p..q] with A[q+1..r]

Initial Call: MergeSort(A, 1, n)


Procedure Merge
Merge(A, p, q, r)
1 n1  q – p + 1
2 n2  r – q Input: Array containing sorted subarrays
3 for i  1 to n1 A[p..q] and A[q+1..r].
4 do L[i]  A[p + i – 1]
5 for j  1 to n2
Output: Merged sorted subarray in A[p..r].
6 do R[j]  A[q + j]
7 L[n1+1]  
8 R[n2+1]  
9 i1
10 j  1
11 for k p to r
12 do if L[i]  R[j]
13 then A[k]  L[i]
14 ii+1
15 else A[k]  R[j]
16 jj+1
Merge – Example

A … 61 8
6 26
8 32
9 1
26 9
32 42 43 …

k k k k k k k k k

L 6 8 26 32  
R 1 9 42 43

i i i i i j j j j j
Analysis of Merge Sort
 Running time T(n) of Merge Sort:
 Divide: computing the middle takes (1)
 Conquer: solving 2 sub-problems takes 2T(n/2)
 Combine: merging n elements takes (n)
 Total:
T(n) = (1) if n = 1
T(n) = 2T(n/2) + (n) if n > 1
T(n) = 2 T(n/2) + n
= 2 ((n/2)log(n/2) + (n/2)) + n
= n (log(n/2)) + 2n
= n log n – n + 2n
= n log n + n
= O(n log n )
Comparing the Algorithms

Best Average Worst


Case Case Case
Bubble Sort O(n) O(n2) O(n2)
Insertion Sort O(n) O(n2) O(n2)
Selection Sort O(n2) O(n2) O(n2)
Merge Sort O(n log n) O(n log n) O(n log n)
Quick Sort O(n log n) O(n log n) O(n2)
Heap Sort O(n log n) O(n log n) O(n log n)
Thank You

You might also like