0% found this document useful (0 votes)
26 views

Lecture 5-BubbleSort - SelectionSort

The document discusses the bubble sort algorithm. It begins by explaining the sorting problem and then describes the bubble sort technique in detail through a series of examples where it compares adjacent elements in an array and swaps them if they are out of order. It shows the array becoming progressively more sorted through multiple passes of the algorithm. Pseudocode for the bubble sort method is provided at the end.

Uploaded by

Dilmi Ranaweera
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Lecture 5-BubbleSort - SelectionSort

The document discusses the bubble sort algorithm. It begins by explaining the sorting problem and then describes the bubble sort technique in detail through a series of examples where it compares adjacent elements in an array and swaps them if they are out of order. It shows the array becoming progressively more sorted through multiple passes of the algorithm. Pseudocode for the bubble sort method is provided at the end.

Uploaded by

Dilmi Ranaweera
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Sorting

Lecture 5
Content
 Sorting
 Bubble Sort
 Selection Sort

2
Sorting
 The problem: put things in order
 Usually smallest to largest: “ascending”
 Could also be largest to smallest:“descending”

 Lots of applications!
 ordering hits in web search engine
 preparing lists of output
 merging data from multiple sources
to help solve other problems
 faster search (allows binary search)

3
Sorting: More Formally
Given an array b[0], b[1], ... b[n-1],
reorder entries so that
b[0] <= b[1] <= ... <= b[n-1]

Shorthand for these slides:

the notation array[i..k] means all of the elements


array[i], array[i+1]. .. array[k]

Using this notation, the entire array would be:


b[0..n-1]
4
Sorting Algorithms

 Sorting has been intensively studied for


decades

 Many different ways to do it!

 We’ll look at two algorithms today,


“bubble sort” and “Selection Sort”

 Other algorithms include, Insertion Sort,


QuickSort, and MergeSort.

5
Sorting Problem
 What we want: Data sorted in order
0 n
b sorted: b[0]<=b[1]<=…<=b[n-1]

Initial conditions
0 n
b unsorted

6
Bubble Sort
Data
 Bubble Sort is one of
many sorting techniques 0 56
available. 1 42
2 5
3 87
4 34
5 23

7
Bubble Sort
Data
 Compare adjacent values
and interchange. 0 56
1 42
2 5
3 87
4 34
5 23

8
Bubble Sort
Data
 Compare adjacent values
and interchange. 0 42
1 56
2 5
3 87
4 34
5 23

9
Bubble Sort
Data
 Compare adjacent values
and interchange. 0 42
1 5
2 56
3 87
4 34
5 23

10
Bubble Sort
Data
 Compare adjacent values
and interchange. 0 42
1 5
2 56
3 87
4 34
5 23

11
Bubble Sort
Data
 Compare adjacent values
and interchange. 0 42
1 5
2 56
3 34
4 87
5 23

12
Bubble Sort
Data
 Compare adjacent values
and interchange. 0 42
1 5
2 56
3 34
4 23
5 87

13
Bubble Sort
Data
 Compare adjacent values
and interchange. 0 42
1 5
2 56
3 34
4 23
5 87

14
Bubble Sort
Data
 Compare adjacent values
and interchange. 0 5
1 42
2 56
3 34
4 23
5 87

15
Bubble Sort
Data
 Compare adjacent values
and interchange. 0 5
1 42
2 56
3 34
4 23
5 87

16
Bubble Sort
Data
 Compare adjacent values
and interchange. 0 5
1 42
2 34
3 56
4 23
5 87

17
Bubble Sort
Data
 Compare adjacent values
and interchange. 0 5
1 42
2 34
3 23
4 56
5 87

18
Bubble Sort
Data
 Compare adjacent values
and interchange. 0 5
1 42
2 34
3 23
4 56
5 87

19
Bubble Sort
Data
 Compare adjacent values
and interchange. 0 5
1 42
2 34
3 23
4 56
5 87

20
Bubble Sort
Data
 Compare adjacent values
and interchange. 0 5
1 34
2 42
3 23
4 56
5 87

21
Bubble Sort
Data
 Compare adjacent values
and interchange. 0 5
1 34
2 23
3 42
4 56
5 87

22
Bubble Sort
Data
 Compare adjacent values
and interchange. 0 5
1 34
2 23
3 42
4 56
5 87

23
Bubble Sort
Data
 Compare adjacent values
and interchange. 0 5
1 34
2 23
3 42
4 56
5 87

24
Bubble Sort
Data
 Compare adjacent values
and interchange. 0 5
1 23
2 34
3 42
4 56
5 87

25
Bubble Sort
Data
 Compare adjacent values
and interchange. 0 5
1 23
2 34
 Sorted Data 3 42
4 56
5 87

26
Bubble Sort
Data
 Compare adjacent values
and interchange. 0 5
1 23
2 34
 Sorted Data 3 42
4 56
5 87

27
Bubble Sort
void bubbleSort(double array[], int size){
for ( pass No of times to repeat ?)
i=0
for ( i No of times to repeat ?)
if current > next
swap current, next
i=i+1
pass = pass + 1

28
Bubble Sort
void bubbleSort(double array[], int size ){
for ( pass No of times to repeat ?)
i=0
for ( i No of times to repeat ?)
if array[i]>array[i+1]
swap array[i], array[i+1]
i=i+1
pass = pass + 1

29
Bubble Sort
void bubbleSort(double array[], int size){
for ( pass < array length)
i=0
for ( i No of times to repeat ?)
if array[i]>array[i+1]
swap array[i], array[i+1]
i=i+1
pass = pass + 1

30
Bubble Sort
void bubbleSort(double array[], int size){
for ( pass < array length )
i=0
for ( i< array length – pass-1)
if array[i]>array[i+1]
swap array[i], array[i+1]
i=i+1
pass = pass + 1

31
Bubble Sort
void bubbleSort (double array[], int size)
{
double temp ;
for (int pass = 1; pass < size; pass++)
for (int i = 0; i < size – pass; i++)
if (array [i] > array [i+1])
{
temp = array[i] ;
array[i] = array [i+1] ;
array [i+1] = temp ;
}
}
32
Complexity
What is the time complexity of the bubble sort
algorithm?

For 6 elements no of swaps/comparisons in worst


case 5+4+3+2+1

For N elements = N*(N+1)/2 ≈ N2

Complexity : O(N2)

33
Selection Sort
Data
 Selection Sort is one of
many sorting techniques 0 56
available. 1 42
2 5
3 87
4 34
5 23

34
Selection Sort
Data
 Find Smallest number and
exchange with Data[0]. 0 56
1 42
2 5
3 87
4 34
5 23

35
Selection Sort
Data
 Find Smallest number and
exchange with Data[0]. 0 5
1 42
2 56
3 87
4 34
5 23

36
Selection Sort
Data
 Find Smallest number and
exchange with Data[1]. 0 5
1 42
2 56
3 87
4 34
5 23

37
Selection Sort
Data
 Find Smallest number and
exchange with Data[1]. 0 5
1 23
2 56
3 87
4 34
5 42

38
Selection Sort
Data
 Find Smallest number and
exchange with Data[2]. 0 5
1 23
2 56
3 87
4 34
5 42

39
Selection Sort
Data
 Find Smallest number and
exchange with Data[2]. 0 5
1 23
2 34
3 87
4 56
5 42

40
Selection Sort
Data
 Find Smallest number and
exchange with Data[3]. 0 5
1 23
2 34
3 87
4 56
5 42

41
Selection Sort
Data
 Find Smallest number and
exchange with Data[3]. 0 5
1 23
2 34
3 42
4 56
5 87

42
Selection Sort
Data
 Find Smallest number and
exchange with Data[4]. 0 5
1 23
2 34
3 42
4 56
5 87

43
Selection Sort
Data
 Sorted Data
0 5
1 23
2 34
3 42
4 56
5 87

44
Selection Sort
void selectionsort(double array[], int size) {

step = 0
while (step < array length-1)
min location = step;
determine min value between step and end of arra
Swap min value with step value
step = step + 1

45
Selection Sort
void selectionsort(double array[], int size) {
step = 0
while (step < array length-1)
min location = step;
loc = step
while (loc < array length)
if array[loc] < array[min location]
min location = loc
loc = loc + 1
Swap min value with step value
step = step + 1 46
Selection Sort
void selectionSort(double array[], int size) {
int min;
double temp;
for (int step = 0; step < size-1; step++) {
min = step;
for (int i = step+1; i < size; i++) {
if (array [i] < array[min]) {
min = i;
}
}
temp = array[step];
array [step] = array[min];
array [min] = temp;
} 47
Complexity
What is the time complexity of the Selection
Sort algorithm?

For 6 elements no of swaps = 5


No of comparisons = 5+4+3+2+1

For N elements = N*(N+1)/2 ≈ N2

Complexity : O(N2) – How ever faster than


bubble sort because no of swaps smaller.
48
Can We Do Better Than n2?
Sure we can!
Selection, insertion, bubble sorts are all
proportional to n2
Other sorts are proportional to n log n
Mergesort
Quicksort
etc.

log n is considerably smaller than n, especially as n


gets larger

As the size of our problem grows, the time to run


a n2 sort will grow much faster than a n log n one.
49

You might also like