Lecture 5-BubbleSort - SelectionSort
Lecture 5-BubbleSort - SelectionSort
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]
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?
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?