Sorting
Sorting
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)
• Repeat step 2 and 3 for K=1 to N-1.
• Set PTR:=1. [Initialize pass pointer PTR.]
• Repeat while PTR ≤ N-K: Execute Pass.]
1. IF DATA[PTR] > DATA[PTR + 1], then:
Interchange DATA[PTR] and DATA[PTR + 1].
[End of If structure]
• Set PTR:=PTR +1.
[End of inner loop]
[End of Step 1 outer loop.]
1.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
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)
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 worst case
and average case.
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.
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
A … 61 8
6 26
8 32
9 1
26 9
32 42 43 …
k k k k k k kk k
L 6 8 26 32 1 9 42 43
R
i i i i i 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