0% found this document useful (0 votes)
8 views53 pages

3 Sorting

Uploaded by

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

3 Sorting

Uploaded by

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

Algorithms

Lecture 3
Sorting with Divide and Conquer
Prantik Paul [PNP]
Lecturer
Department of Computer Science and
Engineering
1
BRAC University
Bubble Sort

def bubble_sort(arr): Worst Case


Complexity?
n = len(arr)
Best Case
for i in range(n-1): Complexity?
for j in range(n-i-1): Space Complexity?
Optimization?
if arr[j] > arr[j+1]:
temp = arr[j]
arr[j] = arr[j+1]
arr[j+1] = temp
2
Selection Sort

def selection_sort(arr): Worst Case


n = len(arr) Complexity?
Best Case
for i in range(n):
Complexity?
min_idx = i Space Complexity?
for j in range(i+1, n): Optimization?
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx],
arr[i]
3
Insertion Sort

def insertion_sort(arr): Worst Case


n = len(arr) Complexity?
Best Case
for i in range(1, n):
Complexity?
key = arr[i] Space Complexity?
j=i-1 Optimization?
while j >= 0 and key < arr[j]:
arr [j+1] = arr[j]
j = j -1
arr[j+1] = key
4
Divide and Conquer Strategy
Divide the problem (instance) into
subproblems.
the subproblems by solving these
Conque
recursively (or iteratively).
r

Combin the solutions to the subproblems.


e

5
Merge Sort

6
Merge Sort (Simulation)

7
Merge Sort (Simulation)

8
Merge Sort (Simulation)

9
Merge Sort (Simulation)

10
Merge Sort (Simulation)

11
Merge Sort (Simulation)

12
Merge Sort (Simulation)

13
Merge (Simulation)

14
Merge (Simulation)

15
Merge (Simulation)

16
Merge (Simulation)

17
Merge (Simulation)

18
Merge (Simulation)

19
Merge (Simulation)

20
Merge (Simulation)

21
Merge (Simulation)

22
Merge (Simulation)

23
Merge (Simulation)

24
Merge (Algorithm)

25
Merge Sort (Algorithm)
Out-of-Place
Algorithm

26
Quick Sort

27
Quick Sort (Simulation)

28
Quick Sort (Simulation)

29
Quick Sort (Simulation)

30
Quick Sort (Simulation)

31
Quick Sort (Simulation)

32
Quick Sort (Simulation)

33
Quick Sort (Simulation)

34
Quick Sort (Simulation)

35
Quick Sort (Simulation)

36
Quick Sort (Simulation)

37
Partitioning (Algorithm)

38
Partitioning (Simulation)

39
Partitioning (Simulation)

40
Partitioning (Simulation)

41
Partitioning (Simulation)

42
Partitioning (Simulation)

43
Partitioning (Simulation)

44
Partitioning (Simulation)

45
Partitioning (Simulation)

46
Partitioning (Simulation)

47
Partitioning (Simulation)

48
Partitioning (Simulation)

49
Partitioning (Simulation)

50
Quick Sort (Algorithm)

51
Quick Sort (Soft Analysis - Worst Case)

52
Quick Sort (Soft Analysis - Best Case)

53

You might also like