0% found this document useful (0 votes)
13 views135 pages

bubble sort merged sort

Uploaded by

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

bubble sort merged sort

Uploaded by

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

Bubble Sort

Merge Sort
Bubble Sort
Sorting
• Sorting takes an unordered collection and
makes it an ordered one.
1 2 3 4 5 6

77 42 35 12 101 5

1 2 3 4 5 6
5 12 35 42 77 101
"Bubbling Up" the Largest Element

• Traverse a collection of elements


– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping

1 2 3 4 5 6

77 42 35 12 101 5
"Bubbling Up" the Largest Element

• Traverse a collection of elements


– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping

1 2 3 4 5 6
42Swap77 12 101
77 42 35 5
"Bubbling Up" the Largest Element

• Traverse a collection of elements


– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping

1 2 3 4 5 6

42 35Swap35
77 77 12 101 5
"Bubbling Up" the Largest Element

• Traverse a collection of elements


– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping

1 2 3 4 5 6

42 35 12Swap12
77 77 101 5
"Bubbling Up" the Largest Element

• Traverse a collection of elements


– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping

1 2 3 4 5 6

42 35 12 77 101 5

No need to swap
"Bubbling Up" the Largest Element

• Traverse a collection of elements


– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping

1 2 3 4 5 6

42 35 12 77 5 Swap101
101 5
"Bubbling Up" the Largest Element

• Traverse a collection of elements


– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping

1 2 3 4 5 6

42 35 12 77 5 101

Largest value correctly placed


The “Bubble Up” Algorithm

index <- 1
last_compare_at <- n – 1

loop
exitif(index > last_compare_at)
if(A[index] > A[index + 1]) then
Swap(A[index], A[index + 1])
endif
index <- index + 1
endloop
An Animated Example

N 8 did_swap true
to_do 7
index

98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap false
to_do 7
index 1

98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap false
to_do 7
index 1

Swap

98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 1

Swap

23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 2

23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 2

Swap

23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 2

Swap

23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 3

23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 3

Swap

23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 3

Swap

23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 4

23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 4

Swap

23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 4

Swap

23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 5

23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 5

Swap

23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 5

Swap

23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 6

23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 6

Swap

23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 6

Swap

23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 7

23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 7

Swap

23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 7

Swap

23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
After First Pass of Outer Loop

N 8 did_swap true
to_do 7
index 8 Finished first “Bubble Up”

23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap false
to_do 6
index 1

23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap false
to_do 6
index 1

No Swap

23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap false
to_do 6
index 2

23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap false
to_do 6
index 2

Swap

23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 2

Swap

23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 3

23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 3

Swap

23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 3

Swap

23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 4

23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 4

No Swap

23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 5

23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 5

Swap

23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 5

Swap

23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 6

23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 6

Swap

23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 6

Swap

23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
After Second Pass of Outer Loop

N 8 did_swap true
to_do 6
index 7 Finished second “Bubble Up”

23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap false
to_do 5
index 1

23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap false
to_do 5
index 1

Swap

23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 1

Swap

14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 2

14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 2

Swap

14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 2

Swap

14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 3

14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 3

No Swap

14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 4

14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 4

Swap

14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 4

Swap

14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 5

14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 5

Swap

14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 5

Swap

14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
After Third Pass of Outer Loop

N 8 did_swap true
to_do 5
index 6 Finished third “Bubble Up”

14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”

N 8 did_swap false
to_do 4
index 1

14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”

N 8 did_swap false
to_do 4
index 1

Swap

14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”

N 8 did_swap true
to_do 4
index 1

Swap

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”

N 8 did_swap true
to_do 4
index 2

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”

N 8 did_swap true
to_do 4
index 2

No Swap

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”

N 8 did_swap true
to_do 4
index 3

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”

N 8 did_swap true
to_do 4
index 3

No Swap

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”

N 8 did_swap true
to_do 4
index 4

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”

N 8 did_swap true
to_do 4
index 4

No Swap

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
After Fourth Pass of Outer Loop

N 8 did_swap true
to_do 4
index 5 Finished fourth “Bubble Up”

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”

N 8 did_swap false
to_do 3
index 1

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”

N 8 did_swap false
to_do 3
index 1

No Swap

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”

N 8 did_swap false
to_do 3
index 2

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”

N 8 did_swap false
to_do 3
index 2

No Swap

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”

N 8 did_swap false
to_do 3
index 3

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”

N 8 did_swap false
to_do 3
index 3

No Swap

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
After Fifth Pass of Outer Loop

N 8 did_swap false
to_do 3
index 4 Finished fifth “Bubble Up”

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
Finished “Early”

N 8 did_swap false
to_do 3
We didn’t do any swapping,
index 4 so all of the other elements
must be correctly placed.

We can “skip” the last two


passes of the outer loop.

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
Summary
• “Bubble Up” algorithm will move largest
value to its correct location (to the right)
• Repeat “Bubble Up” until all elements are
correctly placed:
– Maximum of N-1 times
– Can finish early if no swapping occurs
• We reduce the number of elements we
compare each time one is correctly placed
LB

Truth in CS Act

• NOBODY EVER USES BUBBLE SORT

• NOBODY

• NOT EVER

• BECAUSE IT IS EXTREMELY INEFFICIENT


Mergesort
Sorting
• Sorting takes an unordered collection and
makes it an ordered one.
1 2 3 4 5 6

77 42 35 12 101 5

1 2 3 4 5 6
5 12 35 42 77 101
Divide and Conquer

• Divide and Conquer cuts the problem in


half each time, but uses the result of both
halves:
– cut the problem in half until the problem
is trivial
– solve for both halves
– combine the solutions
Mergesort
• A divide-and-conquer algorithm:
• Divide the unsorted array into 2 halves until the
sub-arrays only contain one element
• Merge the sub-problem solutions together:
– Compare the sub-array’s first elements
– Remove the smallest element and put it into
the result array
– Continue the process until all elements have
been put into the result array

37 23 6 89 15 12 2 19
Algorithm
Mergesort(Passed an array)
if array size > 1
Divide array in half
Call Mergesort on first half.
Call Mergesort on second half.
Merge two halves.

Merge(Passed two arrays)


Compare leading element in each array
Select lower and place in new array.
(If one input array is empty then place
remainder of other array in output array)
LB

More TRUTH in CS

• We don’t really pass in two arrays!

• We pass in one array with indicator variables which


tell us where one set of data starts and finishes and
where the other set of data starts and finishes.

• Honest.

s1 f1 s2 f2
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

23
Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

23 98
Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14 23

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14 23 45

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14 23 45 98

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

23 98 14 45

14 23 45 98
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45

14 23 45 98
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45

14 23 45 98 Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45 6

14 23 45 98 Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45 6 67

14 23 45 98 Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67

14 23 45 98
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67

14 23 45 98 Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33

14 23 45 98 Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67 98

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67 98
98 23 45 14 6 67 33 42

6 14 23 33 42 45 67 98
Summary

• Divide the unsorted collection into two

• Until the sub-arrays only contain one


element

• Then merge the sub-problem solutions


together

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy