Sie sind auf Seite 1von 300

SEARCHING AND

SORTING
ALGORITHMS
Udaibhan Singh
1401104
Vaibhav Maheshwari
1401105
Vaibhav Dwivedi
1401106
Vemparella Gopal Krishna
1401107

Searching Algorithms

Searching Algorithms

Searching is the process of determining


whether or not a given value exists in a
data structure or a storage media
-23

97

18

21

-86

64

Suppose you have a big array full of


data, and you want to find a particular
value.
How will you find that value?

-37

Searching Algorithms

Necessary components to search a list of fdata


Array containing the list
Length of the list
Item for which you are searching

After search completed


If item found, report success, return location in array
If item not found, report not found or failure

Linear Search
Suppose that you want to determine whether 27 is in the list
First compare 27 with list[0]; that is, compare 27 with
35
Because list[0] 27, you then compare 27 with
list[1]
Because list[1] 27, you compare 27 with the next
element in the list
Because list[2] = 27, the search stops
This search is successful!

Figure 1: Array list with seven (07) elements

procedure Search (cur isoftype in ptr toa Node,


target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop
if(cur = NIL) then
print(Target data not found)
else
print(Target data found)
endif
endprocedure // Search
target = 4

head

cur

22

34

procedure Search ( cur isoftype in ptr toa Node,


target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop
if(cur = NIL) then
print(Target data not found)
else
print(Target data found)
endif
endprocedure // Search
target = 4

head

cur

22

34

procedure Search ( cur isoftype in ptr toa Node,


target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop
if(cur = NIL) then
print(Target data not found)
else
print(Target data found)
endif
endprocedure // Search
target = 4

head

cur

22

34

procedure Search ( cur isoftype in ptr toa Node,


target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop
if(cur = NIL) then
print(Target data not found)
else
print(Target data found)
endif
endprocedure // Search
cur

target = 4

head

22

34

procedure Search ( cur isoftype in ptr toa Node,


target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop
if(cur = NIL) then
print(Target data not found)
else
print(Target data found)
endif
endprocedure // Search
cur

target = 4

head

22

34

procedure Search ( cur isoftype in ptr toa Node,


target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop
if(cur = NIL) then
print(Target data not found)
else
print(Target data found)
endif
endprocedure // Search
cur

target = 4

head

22

34

procedure Search ( cur isoftype in ptr toa Node,


target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop
if(cur = NIL) then
print(Target data not found)
else
print(Target data found)
endif
endprocedure // Search
cur

target = 4

head

22

34

procedure Search ( cur isoftype in ptr toa Node,


target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop
if(cur = NIL) then
print(Target data not found)
else
print(Target data found)
endif
endprocedure // Search
cur

target = 4

head

22

34

procedure Search ( cur isoftype in ptr toa Node,


target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop
if(cur = NIL) then
print(Target data not found)
else
print(Target data found)
endif
endprocedure // Search
cur

target = 4

head

22

34

procedure Search ( cur isoftype in ptr toa Node,


target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop
if(cur = NIL) then
print(Target data not found)
else
print(Target data found)
endif
endprocedure // Search
cur

target = 4

head

22

34

procedure Search ( cur isoftype in ptr toa Node,


target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop
if(cur = NIL) then
print(Target data not found)
else
print(Target data found)
endif
endprocedure // Search
cur

target = 4

head

22

34

procedure Search ( cur isoftype in ptr toa Node,


target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop
if(cur = NIL) then
print(Target data not found)
else
print(Target data found)
endif
endprocedure // Search
cur

target = 4

head

22

34

procedure Search ( cur isoftype in ptr toa Node,


target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop
if(cur = NIL) then
print(Target data not found)
else
print(Target data found)
endif
endprocedure // Search
cur

target = 4

head

22

34

procedure Search ( cur isoftype in ptr toa Node,


target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop
if(cur = NIL) then
print(Target data not found)
else
print(Target data found)
endif
endprocedure // Search
cur

target = 4

head

22

34

procedure Search ( cur isoftype in ptr toa Node,


target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop
if(cur = NIL) then
print(Target data not found)
else
print(Target data found)
endif
endprocedure // Search
cur

target = 4

head

22

34

procedure Search ( cur isoftype in ptr toa Node,


target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop
if(cur = NIL) then
print(Target data not found)
else
print(Target data found)
endif
endprocedure // Search
cur

target = 4

head

22

34

procedure Search ( cur isoftype in ptr toa Node,


target isoftype in Num )
loop
Target
data found
exitif((cur
= NIL)
OR (cur^.data = target))
cur <- cur^.next
endloop
if(cur = NIL) then
print(Target data not found)
else
print(Target data found)
endif
endprocedure // Search
cur

target = 4

head

22

34

procedure Search ( cur isoftype in ptr toa Node,


target isoftype in Num )
loop
exitif((cur = NIL) OR (cur^.data = target))
cur <- cur^.next
endloop
if(cur = NIL) then
print(Target data not found)
else
print(Target data found)
endif
endprocedure // Search
cur

target = 4

head

22

34

Performance of the Linear Search


Suppose that the first element in the array list contains the
variable key, then we have performed one comparison to find
the key.
Suppose that the second element in the array list contains the
variable key, then we have performed two comparisons to find
the key.
Carry on the same analysis till the key is contained in the last
element of the array list. In this case, we have performed N
comparisons (N is the size of the array list) to find the key.
Finally if the key is NOT in the array list, then we would have
performed N comparisons and the key is NOT found and we
would return -1.

Performance of the Linear Search (Contd)


Therefore, the best case is: 1
And, the worst case is: N
The average case is:
Worst case and key found at the end of
the array list!
Best case

Average Number of
=
Comparisons

1 + 2 + 3 + ..+ N + N
N+1
Worst case and key is NOT found!

Number of possible cases

Binary Search

The Scenario

We have a sorted array


We want to determine if a
particular element is in the array
Once found, print or return
(index, boolean, etc.)
If not found, indicate the element
is not in the collection
7

12 42 59 71 86 104 212

Binary Search

Requires a sorted array or a


binary search tree.
Cuts the search space in half each
time.
Keeps cutting the search space in half
until the target is found or has
exhausted the all possible locations.
Inappropriate for linked lists because
linked lists lack random access.

The Scenario

We have a sorted array

We want to determine if a particular element is in the array


Once found, print or return (index, boolean, etc.)
If not found, indicate the element is not in the collection

12 42 59 71 86 104 212

A Better Search Algorithm

Of course we could use our simpler search


and traverse the array

But we can use the fact that the array is


sorted to our advantage

This will allow us to reduce the number of


comparisons

Binary Search Algorithm


look at middle element
if no match then
look left (if need smaller) or
right (if need larger)
1

9 12 33 42

59 76 81 84 91 92 93 99

Look for 42

The Algorithm
look at middle element
if no match then
look left or right

9 12 33 42

59 76 81 84 91 92 93 99

Look for 42

The Algorithm
look at middle element
if no match then
look left or right

9 12 33 42

59 76 81 84 91 92 93 99

Look for 42

The Algorithm
look at middle element
if no match then
look left or right

9 12 33 42

59 76 81 84 91 92 93 99

Look for 42

The Binary Search


Algorithm

Return found or not found (true or false), so it should


be a function.

When move left or right, change the array boundaries


Well need a first and last

The Binary Search


Algorithm
calculate middle position
if (first and last have crossed) then
Item not found
elseif (element at middle = to_find) then
Item Found
elseif to_find < element at middle then
Look to the left
else
Look to the right

Looking Left

Use indices first and last to keep


track of where we are looking
Move left by setting last = middle 1

12 42 59 71 86 104 212

Looking Right

Use indices first and last to keep


track of where we are looking
Move right by setting first = middle +
1
7

12 42 59 71 86 104 212

Binary Search Example Found

12 42 59 71 86 104 212

M
Looking for 42

Binary Search Example Found

12 42 59 71 86 104 212

L
Looking for 42

Binary Search Example Found

12 42 59 71 86 104 212

F
M
L
42 found in 3 comparisons

Binary Search Example Not


Found

12 42 59 71 86 104 212

M
Looking for 89

Binary Search Example Not


Found

12 42 59 71 86 104 212

F
Looking for 89

Binary Search Example Not


Found

12 42 59 71 86 104 212

F
M
Looking for 89

Binary Search Example Not


Found

12 42 59 71 86 104 212

89 not found 3 comparisons

Binary Search Function


Function Find return boolean (A Array, first, last, to_find)
middle <- (first + last) div 2
if (first > last) then
return false
elseif (A[middle] = to_find) then
return true
elseif (to_find < A[middle]) then
return Find(A, first, middle1, to_find)
else
return Find(A, middle+1, last, to_find)
endfunction

Binary Search Analysis: Best Case


Funct i on Fi nd r et ur n bool ean ( A Ar r ay, fi r st , l ast , t o_fi nd)
mi ddl e <- ( fi r st + l ast ) di v 2
i f ( fi r st > l ast ) t hen
r et ur n f al se
el sei f ( A[ mi ddl e] = t o_ fi nd) t hen
r et ur n t r ue
el sei f ( t o_fi nd < A[ mi ddl e] ) t hen
r et ur n Fi nd( A, fi r st , mi ddl e1, t o_fi nd)
Best Case:
el se
1 comparison
r et ur n Fi nd( A, mi ddl e+1, l ast , t o_fi nd)
endf unct i on

Best Case: match from the firs comparison


1

Target: 59

9 12 33 42

59 76 81 84 91 92 93 99

Binary Search Analysis: Worst Case


Funct i on Fi nd r et ur n bool ean ( A Ar r ay, fi r st , l ast , t o_ fi nd)
mi ddl e <- ( fi r st + l ast ) di v 2
i f ( fi r st > l ast ) t hen
r et ur n f al se
el sei f ( A[ mi ddl e] = t o_ fi nd) t hen
How many
r et ur n t r ue
comparisons??
el sei f ( t o_fi nd < A[ mi ddl e] ) t hen
r et ur n Fi nd( A, fi r st , mi ddl e1, t o_fi nd)
el se
r et ur n Fi nd( A, mi ddl e+1, l ast , t o_fi nd)
endf unct i on

Worst Case: divide until reach one item, or no match.


1

9 12 33 42

59 76 81 84 91 92 93 99

Binary Search Analysis: Worst Case

With each comparison we throw away of the list


N

1 comparison

N/2

1 comparison

N/4

1 comparison

N/8

1 comparison

.
.
.
1

1 comparison

Number of steps is at
most Log2N

Summary

Binary search reduces the work by half at each


comparison

If array is not sorted Linear Search


Best Case O(1)
Worst Case O(N)

If array is sorted Binary search


Best Case O(1)
Worst Case O(Log2N)

BUBBLE SORT

Sorting

Sorting takes an unordered collection and


makes it an ordered one.
1

2
42

77

1
5

35

2
12

3
35

5
101

12

4
42

6
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
77

2
42

3
35

4
12

6
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
42 Swap42
77
77

3
35

4
12

6
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
42

7735 Swap 35
77

4
12

6
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
42

2
35

77
12 Swap 12
77

6
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
42

2
35

3
12

4
77

6
101

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
42

2
35

3
12

4
77

6
101
5 Swap 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
42

2
35

3
12

4
77

6
5

Largest value correctly placed

101

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

No, Swap isnt built in.


Procedure Swap(a, b isoftype
in/out Num)
t isoftype Num
t <- a
a <- b
b <- t
endprocedure // Swap

LB

Items of Interest

Notice that only the largest value is


correctly placed
All other values are still out of order
So we need to repeat this process
1
42

2
35

3
12

4
77

6
5

Largest value correctly placed

101

Repeat Bubble Up How Many Times?

If we have N elements

And if each time we bubble an element,


we place it in its correct location

Then we repeat the bubble up


process N 1 times.

This guarantees well correctly


place all N elements.

Bubbling All the


Elements
1
42

2
35

N-1

35

5
1

77
4

3
35

42

101
6

77
5

101
6

77
5

42
4

6
5

42

35
2

12

3
5

42

35

12

12

12

12

101
6

77
5

101
6

77

101

Reducing the Number of


Comparisons
1
77

2
42

1
42

12

12

12

35

12
4

77
4

3
35

4
42

6
101

5
6

5
5

101
6

77
5

42

5
2

42
2

1
12

35

35

35

101
6

77
5

101
6

77

101

Reducing the Number of Comparisons

On the Nth bubble up, we only need to


do MAX-N comparisons.
For example:
This is the 4th bubble up
MAX is 6
Thus we have 2 comparisons to do
1
12

2
35

3
5

4
42

6
77

101

Putting It All Together

N is // Size of Array
Arr_Type definesa Array[1..N] of Num
Procedure Swap(n1, n2 isoftype in/out Num)
temp isoftype Num
temp <- n1
n1
<- n2
n2
<- temp
endprocedure // Swap

Outer loop

loop
exitif(to_do = 0)
index <- 1
loop
exitif(index > to_do)
if(A[index] > A[index + 1]) then
Swap(A[index], A[index + 1])
endif
index <- index + 1
endloop
to_do <- to_do - 1
endloop
endprocedure // Bubblesort

Inner loop

procedure Bubblesort(A isoftype in/out Arr_Type)


to_do, index isoftype Num
to_do <- N 1

Already Sorted
Collections?

What if the collection was already sorted?


What if only a few elements were out of place
and after a couple of bubble ups, the collection
was sorted?
We want to be able to detect this
and stop early!
1
5

2
12

3
35

4
42

6
77

101

Using a Boolean Flag

We can use a boolean variable to


determine if any swapping occurred
during the bubble up.

If no swapping occurred, then we know


that the collection is already sorted!

This boolean flag needs to be reset


after each bubble up.

did_swap isoftype Boolean


did_swap <- true
loop
exitif ((to_do = 0) OR NOT(did_swap))
index <- 1
did_swap <- false
loop
exitif(index > to_do)
if(A[index] > A[index + 1]) then
Swap(A[index], A[index + 1])
did_swap <- true
endif
index <- index + 1
endloop
to_do <- to_do - 1
endloop

An Animated Example
N

to_do

did_swap

true

index

98 23 45 14
1

6 67 33 42
5

An Animated Example
N

to_do

index

did_swap false

98 23 45 14
1

6 67 33 42
5

An Animated Example
N

to_do

index

did_swap false

Swap
98 23 45 14
1

6 67 33 42
5

An Animated Example
N

to_do

index

did_swap

true

Swap
23 98 45 14
1

6 67 33 42
5

An Animated Example
N

to_do

index

did_swap

23 98 45 14
1

true

6 67 33 42
5

An Animated Example
N

to_do

index

did_swap

true

Swap
23 98 45 14
1

6 67 33 42
5

An Animated Example
N

to_do

index

did_swap

true

Swap
23 45 98 14
1

6 67 33 42
5

An Animated Example
N

to_do

index

did_swap

23 45 98 14
1

true

6 67 33 42
5

An Animated Example
N

to_do

index

did_swap

true

Swap
23 45 98 14
1

6 67 33 42
5

An Animated Example
N

to_do

index

did_swap

true

Swap
23 45 14 98
1

6 67 33 42
5

An Animated Example
N

to_do

index

did_swap

23 45 14 98
1

true

6 67 33 42
5

An Animated Example
N

to_do

index

did_swap

true

Swap
23 45 14 98
1

6 67 33 42
5

An Animated Example
N

to_do

index

did_swap

true

Swap
23 45 14 6
1

98 67 33 42
5

An Animated Example
N

to_do

index

did_swap

23 45 14 6
1

true

98 67 33 42
5

An Animated Example
N

to_do

index

did_swap

true

Swap
23 45 14 6
1

98 67 33 42
5

An Animated Example
N

to_do

index

did_swap

true

Swap
23 45 14 6
1

67 98 33 42
5

An Animated Example
N

to_do

index

did_swap

23 45 14 6
1

true

67 98 33 42
5

An Animated Example
N

to_do

index

did_swap

true

Swap
23 45 14 6
1

67 98 33 42
5

An Animated Example
N

to_do

index

did_swap

true

Swap
23 45 14 6
1

67 33 98 42
5

An Animated Example
N

to_do

index

did_swap

23 45 14 6
1

true

67 33 98 42
5

An Animated Example
N

to_do

index

did_swap

true

Swap
23 45 14 6
1

67 33 98 42
5

An Animated Example
N

to_do

index

did_swap

true

Swap
23 45 14 6
1

67 33 42 98
5

After First Pass of Outer


Loop
N

to_do

index

did_swap

Finished first Bubble Up

23 45 14 6
1

true

67 33 42 98
5

The Second Bubble Up


N

to_do

index

did_swap false

23 45 14 6
1

67 33 42 98
5

The Second Bubble Up


N

to_do

index

did_swap false

No Swap
23 45 14 6
1

67 33 42 98
5

The Second Bubble Up


N

to_do

index

did_swap false

23 45 14 6
1

67 33 42 98
5

The Second Bubble Up


N

to_do

index

did_swap false

Swap
23 45 14 6
1

67 33 42 98
5

The Second Bubble Up


N

to_do

index

did_swap

true

Swap
23 14 45 6
1

67 33 42 98
5

The Second Bubble Up


N

to_do

index

did_swap

23 14 45 6
1

true

67 33 42 98
5

The Second Bubble Up


N

to_do

index

did_swap

true

Swap
23 14 45 6
1

67 33 42 98
5

The Second Bubble Up


N

to_do

index

did_swap

true

Swap
23 14 6
1

45 67 33 42 98
4

The Second Bubble Up


N

to_do

index

did_swap

23 14 6
1

true

45 67 33 42 98
4

The Second Bubble Up


N

to_do

index

did_swap

true

No Swap
23 14 6
1

45 67 33 42 98
4

The Second Bubble Up


N

to_do

index

did_swap

23 14 6
1

true

45 67 33 42 98
4

The Second Bubble Up


N

to_do

index

did_swap

true

Swap
23 14 6
1

45 67 33 42 98
4

The Second Bubble Up


N

to_do

index

did_swap

true

Swap
23 14 6
1

45 33 67 42 98
4

The Second Bubble Up


N

to_do

index

did_swap

23 14 6
1

true

45 33 67 42 98
4

The Second Bubble Up


N

to_do

index

did_swap

true

Swap
23 14 6
1

45 33 67 42 98
4

The Second Bubble Up


N

to_do

index

did_swap

true

Swap
23 14 6
1

45 33 42 67 98
4

After Second Pass of Outer Loop


N

to_do

index

did_swap

Finished second Bubble Up

23 14 6
1

true

45 33 42 67 98
4

The Third Bubble Up


N

to_do

index

did_swap false

23 14 6
1

45 33 42 67 98
4

The Third Bubble Up


N

to_do

index

did_swap false

Swap
23 14 6
1

45 33 42 67 98
4

The Third Bubble Up


N

to_do

index

did_swap

true

Swap
14 23 6
1

45 33 42 67 98
4

The Third Bubble Up


N

to_do

index

did_swap

14 23 6
1

true

45 33 42 67 98
4

The Third Bubble Up


N

to_do

index

did_swap

true

Swap
14 23 6
1

45 33 42 67 98
4

The Third Bubble Up


N

to_do

index

did_swap

true

Swap
14 6
1

23 45 33 42 67 98
3

The Third Bubble Up


N

to_do

index

did_swap

14 6
1

true

23 45 33 42 67 98
3

The Third Bubble Up


N

to_do

index

did_swap

true

No Swap
14 6
1

23 45 33 42 67 98
3

The Third Bubble Up


N

to_do

index

did_swap

14 6
1

true

23 45 33 42 67 98
3

The Third Bubble Up


N

to_do

index

did_swap

true

Swap
14 6
1

23 45 33 42 67 98
3

The Third Bubble Up


N

to_do

index

did_swap

true

Swap
14 6
1

23 33 45 42 67 98
3

The Third Bubble Up


N

to_do

index

did_swap

14 6
1

true

23 33 45 42 67 98
3

The Third Bubble Up


N

to_do

index

did_swap

true

Swap
14 6
1

23 33 45 42 67 98
3

The Third Bubble Up


N

to_do

index

did_swap

true

Swap
14 6
1

23 33 42 45 67 98
3

After Third Pass of Outer


Loop
N

to_do

index

did_swap

Finished third Bubble Up

14 6
1

true

23 33 42 45 67 98
3

The Fourth Bubble Up


N

to_do

index

did_swap false

14 6
1

23 33 42 45 67 98
3

The Fourth Bubble Up


N

to_do

index

did_swap false

Swap
14 6
1

23 33 42 45 67 98
3

The Fourth Bubble Up


N

to_do

index

did_swap

true

Swap
6
1

14 23 33 42 45 67 98
2

The Fourth Bubble Up


N

to_do

index

6
1

did_swap

true

14 23 33 42 45 67 98
2

The Fourth Bubble Up


N

to_do

index

did_swap

true

No Swap
6
1

14 23 33 42 45 67 98
2

The Fourth Bubble Up


N

to_do

index

6
1

did_swap

true

14 23 33 42 45 67 98
2

The Fourth Bubble Up


N

to_do

index

did_swap

true

No Swap
6
1

14 23 33 42 45 67 98
2

The Fourth Bubble Up


N

to_do

index

6
1

did_swap

true

14 23 33 42 45 67 98
2

The Fourth Bubble Up


N

to_do

index

did_swap

true

No Swap
6
1

14 23 33 42 45 67 98
2

After Fourth Pass of Outer Loop


N

to_do

index

6
1

did_swap

true

Finished fourth Bubble Up

14 23 33 42 45 67 98
2

The Fifth Bubble Up


N

to_do

index

6
1

did_swap false

14 23 33 42 45 67 98
2

The Fifth Bubble Up


N

to_do

index

did_swap false

No Swap
6
1

14 23 33 42 45 67 98
2

The Fifth Bubble Up


N

to_do

index

6
1

did_swap false

14 23 33 42 45 67 98
2

The Fifth Bubble Up


N

to_do

index

did_swap false

No Swap
6
1

14 23 33 42 45 67 98
2

The Fifth Bubble Up


N

to_do

index

6
1

did_swap false

14 23 33 42 45 67 98
2

The Fifth Bubble Up


N

to_do

index

did_swap false

No Swap
6
1

14 23 33 42 45 67 98
2

After Fifth Pass of Outer


Loop
N

to_do

index

6
1

did_swap false
Finished fifth Bubble Up

14 23 33 42 45 67 98
2

Finished Early
N

to_do

index

did_swap false
We didnt do any swapping,
so all of the other elements
must be correctly placed.
We can skip the last two
passes of the outer loop.

6
1

14 23 33 42 45 67 98
2

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

Truth in CS Act

NOBODY EVER USES BUBBLE SORT


BECAUSE IT IS EXTREMELY
INEFFICIENT

LB

MERGE SORT

Sorting

Sorting takes an unordered collection and


makes it an ordered one.
1

2
42

77

1
5

35

2
12

3
35

5
101

12

4
42

6
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-arrays 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

89

15

12

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 dont 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

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

98 23 45 14

6 67 33 42

98 23 45 14
98 23 45 14

6 67 33 42
6 67 33 42

98 23 45 14
98 23 45 14
98 23

45 14

6 67 33 42
6 67 33 42

98 23 45 14
98 23 45 14
98 23
98

23

45 14

6 67 33 42
6 67 33 42

98 23 45 14
98 23 45 14
98 23
98

23

Merge

45 14

6 67 33 42
6 67 33 42

98 23 45 14
98 23 45 14
98 23
98

23

23
Merge

45 14

6 67 33 42
6 67 33 42

98 23 45 14
98 23 45 14
98 23
98

23

23 98
Merge

45 14

6 67 33 42
6 67 33 42

98 23 45 14
98 23 45 14
98 23
98

23

23 98

45 14
45

14

6 67 33 42
6 67 33 42

98 23 45 14
98 23 45 14
98 23
98

23

45 14
45

14

23 98
Merge

6 67 33 42
6 67 33 42

98 23 45 14
98 23 45 14
98 23
98

23

23 98

45 14
45

14

14
Merge

6 67 33 42
6 67 33 42

98 23 45 14
98 23 45 14
98 23
98

23

23 98

45 14
45

14

14 45
Merge

6 67 33 42
6 67 33 42

98 23 45 14
98 23 45 14
98 23
98

23

23 98

45 14
45

14

14 45

Merge

6 67 33 42
6 67 33 42

98 23 45 14
98 23 45 14
98 23
98

23

23 98

45 14
45

14

14 45

14
Merge

6 67 33 42
6 67 33 42

98 23 45 14
98 23 45 14
98 23
98

23

23 98

45 14
45

14

14 45

14 23
Merge

6 67 33 42
6 67 33 42

98 23 45 14
98 23 45 14
98 23
98

23

23 98

45 14
45

14

14 45

14 23 45
Merge

6 67 33 42
6 67 33 42

98 23 45 14
98 23 45 14
98 23
98

23

23 98

45 14
45

14

14 45

14 23 45 98
Merge

6 67 33 42
6 67 33 42

98 23 45 14
98 23 45 14
98 23
98

23

23 98

45 14
45

14

14 45

14 23 45 98

6 67 33 42
6 67 33 42
6 67

33 42

98 23 45 14
98 23 45 14
98 23
98

23

23 98

45 14
45

14

14 45

14 23 45 98

6 67 33 42
6 67 33 42
6 67
6

67

33 42

98 23 45 14
98 23 45 14
98 23
98

23

23 98

45 14
45

14

6 67 33 42
6 67 33 42
6 67
6

67

14 45

14 23 45 98

Merge

33 42

98 23 45 14
98 23 45 14
98 23
98

23

23 98

45 14
45

14

14 45

14 23 45 98

6 67 33 42
6 67 33 42
6 67
6

67

6
Merge

33 42

98 23 45 14
98 23 45 14
98 23
98

23

23 98

45 14
45

14

14 45

14 23 45 98

6 67 33 42
6 67 33 42
6 67
6

67

6 67
Merge

33 42

98 23 45 14
98 23 45 14
98 23
98

23

23 98

45 14
45

14

14 45

14 23 45 98

6 67 33 42
6 67 33 42
6 67

33 42

33

67

6 67

42

98 23 45 14
98 23 45 14
98 23
98

23

23 98

45 14
45

14

14 45

14 23 45 98

6 67 33 42
6 67 33 42
6 67

33 42

33

67

42

6 67
Merge

98 23 45 14
98 23 45 14
98 23
98

23

23 98

45 14
45

14

14 45

14 23 45 98

6 67 33 42
6 67 33 42
6 67

33 42

33

67

6 67

42

33
Merge

98 23 45 14
98 23 45 14
98 23
98

23

23 98

45 14
45

14

14 45

14 23 45 98

6 67 33 42
6 67 33 42
6 67

33 42

33

67

6 67

42

33 42
Merge

98 23 45 14
98 23 45 14
98 23
98

23

23 98

45 14
45

14

14 45

6 67 33 42
6 67 33 42
6 67

33 42

33

67

6 67

42

33 42

14 23 45 98
Merge

98 23 45 14
98 23 45 14
98 23
98

23

23 98

45 14
45

14

14 45

14 23 45 98

6 67 33 42
6 67 33 42
6 67

33 42

33

67

6 67

42

33 42

6
Merge

98 23 45 14
98 23 45 14
98 23
98

23

23 98

45 14
45

14

14 45

14 23 45 98

6 67 33 42
6 67 33 42
6 67

33 42

33

67

6 67

42

33 42

6 33
Merge

98 23 45 14
98 23 45 14
98 23
98

23

23 98

45 14
45

14

14 45

14 23 45 98

6 67 33 42
6 67 33 42
6 67

33 42

33

67

6 67

42

33 42

6 33 42
Merge

98 23 45 14
98 23 45 14
98 23
98

23

23 98

45 14
45

14

14 45

14 23 45 98

6 67 33 42
6 67 33 42
6 67

33 42

33

67

6 67

42

33 42

6 33 42 67
Merge

98 23 45 14

6 67 33 42

98 23 45 14
98 23
98

23

23 98

6 67 33 42

45 14
45

14

6 67

33 42

33

14 45

67

6 67

33 42

6 33 42 67

14 23 45 98

Merge

42

98 23 45 14

6 67 33 42

98 23 45 14
98 23
98

23

23 98

6 67 33 42

45 14
45

14

6 67

33 42

33

14 45

67

6 67

33 42

6 33 42 67

14 23 45 98
6
Merge

42

98 23 45 14

6 67 33 42

98 23 45 14
98 23
98

23

6 67 33 42

45 14
45

23 98

14

6 67

33 42

33

14 45

67

6 67

33 42

6 33 42 67

14 23 45 98
6 14
Merge

42

98 23 45 14

6 67 33 42

98 23 45 14
98 23
98

23

23 98

6 67 33 42

45 14
45

14

6 67

33 42

33

14 45

67

6 67

33 42

6 33 42 67

14 23 45 98
6 14 23
Merge

42

98 23 45 14

6 67 33 42

98 23 45 14
98 23
98

23

23 98

6 67 33 42

45 14
45

14

6 67

33 42

33

14 45

67

6 67

33 42

6 33 42 67

14 23 45 98
6 14 23 33
Merge

42

98 23 45 14

6 67 33 42

98 23 45 14
98 23
98

23

23 98

6 67 33 42

45 14
45

14

6 67

33 42

33

14 45

67

6 67

33 42

6 33 42 67

14 23 45 98
6 14 23 33 42
Merge

42

98 23 45 14

6 67 33 42

98 23 45 14
98 23
98

23

23 98

6 67 33 42

45 14
45

14

6 67

33 42

33

14 45

67

6 67

33 42

6 33 42 67

14 23 45 98

6 14 23 33 42 45
Merge

42

98 23 45 14

6 67 33 42

98 23 45 14
98 23
98

23

23 98

6 67 33 42

45 14
45

14

6 67

33 42

33

14 45

67

6 67

33 42

6 33 42 67

14 23 45 98

6 14 23 33 42 45 67
Merge

42

98 23 45 14

6 67 33 42

98 23 45 14
98 23
98

23

23 98

6 67 33 42

45 14
45

14

6 67

33 42

33

14 45

67

6 67

33 42

6 33 42 67

14 23 45 98

6 14 23 33 42 45 67 98
Merge

42

98 23 45 14
98 23 45 14
98 23
98

23

23 98

45 14
45

14

14 45

14 23 45 98

6 67 33 42
6 67 33 42
6 67

33 42

33

67

6 67

42

33 42

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

RADIX SORT

Bucket Sort

Given that, we have N integers in the


range 0 to M-1
Maintain an array count of size M, which
is initialized to zero. Thus, count has M
cells (buckets).
Read Ai
Increment count[Ai] by 1
After all the input array is read, scan the
count array, printing out a representation
of sorted list.

Radix Sort

Radix sort is generalization of bucket


sort.
It uses several passes of bucket sort
Perform the bucket sorts by least
significant digits
First sort by digit in units place
Second sort by digit in tens place
Third sort by digit in hundreds place
.

Lets sort the following array using radix sort:


64

8 216 512 27 729 0

1 343 125

0
1
2
3
4
5
6
7
8
9

Pass 1

64
0
1
2
3
4
5
6
7
8
9

8 216 512 27 729 0

1 343 125

64
0
1
2
3
4
5
6
7
8
9

8 216 512 27 729 0

1 343 125

64
0
1
2
3
4
5
6
7
8
9

64

8 216 512 27 729 0

1 343 125

64
0
1
2
3
4
5
6
7
8
9

64

8 216 512 27 729 0

1 343 125

64
0
1
2
3
4

64

5
6
7
8
9

8 216 512 27 729 0

1 343 125

64
0
1
2
3
4

64

5
6
7
8
9

8 216 512 27 729 0

1 343 125

64
0
1
2
3
4

64

5
6

216

7
8
9

8 216 512 27 729 0

1 343 125

64
0
1
2
3
4

64

5
6

216

7
8
9

8 216 512 27 729 0

1 343 125

64
0
1
2

512

3
4

64

5
6

216

7
8
9

8 216 512 27 729 0

1 343 125

64
0
1
2

512

3
4

64

5
6

216

7
8
9

8 216 512 27 729 0

1 343 125

64
0
1
2

512

3
4

64

5
6

216

27

8 216 512 27 729 0

1 343 125

64
0
1
2

512

3
4

64

5
6

216

27

8 216 512 27 729 0

1 343 125

64
0
1
2

512

3
4

64

5
6

216

27

729

8 216 512 27 729 0

1 343 125

64
0
1
2

512

3
4

64

5
6

216

27

729

8 216 512 27 729 0

1 343 125

64
0

1
2

512

3
4

64

5
6

216

27

729

8 216 512 27 729 0

1 343 125

64
0

1
2

512

3
4

64

5
6

216

27

729

8 216 512 27 729 0

1 343 125

64
0

512

3
4

64

5
6

216

27

729

8 216 512 27 729 0

1 343 125

64
0

512

3
4

64

5
6

216

27

729

8 216 512 27 729 0

1 343 125

64
0

512

343

64

5
6

216

27

729

8 216 512 27 729 0

1 343 125

64
0

512

343

64

5
6

216

27

729

8 216 512 27 729 0

1 343 125

64
0

512

343

64

125

216

27

729

8 216 512 27 729 0

1 343 125

64
0

512

343

64

125

216

27

729

8 216 512 27 729 0

1 343 125

512

343

64

125

216

27

729

512

343

64

125

216

27

729

0
0

512

343

64

125

216

27

729

0
0
1

512

343

64

125

216

27

729

0
0
1

512

343

64

125

216

27

729

0
0
1

512

343

64

125

216

27

729

0
0
1
2

512

343

64

125

216

27

729

0
0
1
2

512

343

64

125

216

27

729

1 512

0
0
1
2
3

343

64

125

216

27

729

1 512

1 512 343 64 125 216 27

0
1
2
3
4
5
6
7
8
9

Pass 2

8 729

00 1 512 343 64 125 216 27


0
1
2
3
4
5
6
7
8
9

8 729

0
0
1
2
3
4
5
6
7
8
9

1 512 343 64 125 216 27

8 729

0
0
1
2
3
4
5
6
7
8
9

01 512 343 64 125 216 27

8 729

0
0
1
2
3
4
5
6
7
8
9

1 512 343 64 125 216 27

8 729

0
0
1
2
3
4
5
6
7
8
9

1 512 343 64 125 216 27

8 729

0
0

512

2
3
4
5
6
7
8
9

1 512 343 64 125 216 27

8 729

0
0

512

2
3
4
5
6
7
8
9

1 512 343 64 125 216 27

8 729

0
0

512

2
3
4
5
6
7
8
9

343

1 512 343 64 125 216 27

8 729

0
0

512

2
3
4
5
6
7
8
9

343

1 512 343 64 125 216 27

8 729

0
0

512

2
3
4

343

5
6
7
8
9

64

1 512 343 64 125 216 27

8 729

0
0

512

2
3
4

343

5
6
7
8
9

64

1 512 343 64 125 216 27

8 729

0
0

512

125

3
4

343

5
6
7
8
9

64

1 512 343 64 125 216 27

8 729

0
0

512

125

3
4

343

5
6
7
8
9

64

1 512 343 64 125 216 27

8 729

0
0

512

216

125

3
4

343

5
6
7
8
9

64

1 512 343 64 125 216 27

8 729

0
0

512

216

125

3
4

343

5
6
7
8
9

64

1 512 343 64 125 216 27

8 729

0
0

512

216

125

27

3
4

343

5
6
7
8
9

64

1 512 343 64 125 216 27

8 729

0
0

512

216

125

27

3
4

343

5
6
7
8
9

64

1 512 343 64 125 216 27 08 729

0
0

512

216

125

27

3
4

343

5
6
7
8
9

64

1 512 343 64 125 216 27

8 729

0
0

512

216

125

27

3
4

343

5
6
7
8
9

64

1 512 343 64 125 216 27

8 729

0
0

512

216

125

27

3
4

343

5
6
7
8
9

64

729

1 512 343 64 125 216 27

8 729

0
0

512

216

125

27

3
4

343

5
6
7
8
9

64

729

1 512 343 64 125 216 27

8 729

512

216

125

27

3
4

343

5
6
7
8
9

64

729

0
0

512

216

125

27

3
4

343

5
6
7
8
9

64

729

0
0
1

512

216

125

27

3
4

343

5
6
7
8
9

64

729

0
0
1

512

216

125

27

3
4

343

5
6
7
8
9

64

729

8 512 216

0
0
1
2

125

3
4

343

5
6
7
8
9

64

27

729

8 512 216

0
0
1
2

125

3
4

343

5
6
7
8
9

64

27

729

8 512 216 125 27 729

0
0
1
2
3
4

343

5
6
7
8
9

64

8 512 216 125 27 729

0
0
1
2
3
4

343

5
6
7
8
9

64

8 512 216 125 27 729 343

0
0
1
2
3
4
5
6
7
8
9

64

8 512 216 125 27 729 343

0
0
1
2
3
4
5
6
7
8
9

64

8 512 216 125 27 729 343 64

0
1
2
3
4
5
6
7
8
9

Pass 3

8 512 216 125 27 729 343 64

000 1
0
1
2
3
4
5
6
7
8
9

8 512 216 125 27 729 343 64

0
0
1
2
3
4
5
6
7
8
9

8 512 216 125 27 729 343 64

0 001 8 512 216 125 27 729 343 64


0
1
2
3
4
5
6
7
8
9

0
0
1
2
3
4
5
6
7
8
9

8 512 216 125 27 729 343 64

0
0
1
2
3
4
5
6
7
8
9

1 008 512 216 125 27 729 343 64

0
0
1
2
3
4
5
6
7
8
9

8 512 216 125 27 729 343 64

0
0
1
2
3
4
5
6
7
8
9

8 512 216 125 27 729 343 64

0
0

1
2
3
4
5
6
7
8
9

512

8 512 216 125 27 729 343 64

0
0

1
2
3
4
5
6
7
8
9

512

8 512 216 125 27 729 343 64

0
0

1
2

216

3
4
5
6
7
8
9

512

8 512 216 125 27 729 343 64

0
0

1
2

216

3
4
5
6
7
8
9

512

8 512 216 125 27 729 343 64

0
0

125

216

3
4
5
6
7
8
9

512

8 512 216 125 27 729 343 64

0
0

125

216

3
4
5
6
7
8
9

512

8 512 216 125 027 729 343 64

0
0

125

216

3
4
5
6
7
8
9

512

1
27

8 512 216 125 27 729 343 64

0
0

125

216

3
4
5
6
7
8
9

512

1
27

8 512 216 125 27 729 343 64

0
0

125

216

3
4
5

512

6
7
8
9

729

1
27

8 512 216 125 27 729 343 64

0
0

125

216

3
4
5

512

6
7
8
9

729

1
27

8 512 216 125 27 729 343 64

0
0

125

216

343

4
5

512

6
7
8
9

729

1
27

8 512 216 125 27 729 343 64

0
0

125

216

343

4
5

512

6
7
8
9

729

1
27

8 512 216 125 27 729 343 064

0
0

125

216

343

4
5

512

6
7
8
9

729

1
27

8 512 216 125 27 729 343 64


64

0
0

125

216

343

4
5

512

6
7
8
9

729

1
27

8 512 216 125 27 729 343 64


64

125

216

343

4
5

512

6
7
8
9

729

27

64

0
0

125

216

343

4
5

512

6
7
8
9

729

1
27

27 64
64

0
0
1

125

216

343

4
5

512

6
7
8
9

729

27 64

0
0
1

125

216

343

4
5

512

6
7
8
9

729

27 64 125

0
0
1
2

216

343

4
5

512

6
7
8
9

729

27 64 125

0
0
1
2

216

343

4
5

512

6
7
8
9

729

27 64 125 216

0
0
1
2
3

343

4
5

512

6
7
8
9

729

27 64 125 216

0
0
1
2
3

343

4
5

512

6
7
8
9

729

27 64 125 216 343

0
0
1
2
3
4
5

512

6
7
8
9

729

27 64 125 216 343

0
0
1
2
3
4
5

512

6
7
8
9

729

27 64 125 216 343 512

0
0
1
2
3
4
5
6
7
8
9

729

27 64 125 216 343 512

0
0
1
2
3
4
5
6
7
8
9

729

27 64 125 216 343 512 729

0
0
1
2
3
4
5
6
7
8
9

27 64 125 216 343 512 729

The End

Das könnte Ihnen auch gefallen