Sorting and Searching Algorithms
Sorting and Searching Algorithms
Sorting Problem
Key?:
Let us consider a set of entities, each entity having a
characteristics whose values are can be ordered (there
exists a relation of total ordering on the set of these values
a1<a2<…<an). This characteristics is called sorting key.
4
Types of sorting
Internal sorting
The elements of the set are stored on a random access
memory.
Bubble sort
Insertion sort
Selection sort
External sorting
when the data to be sorted are stored in files on hard
disks.
Bubble sort
values [1] 36 U
N
[2] 24 S
O
[3]
10 R
[4]
6 T
E
[ 5]
12 D
Bubble Sort:
9
Pass One
values [1] 24 U
N
S
[2] 36 O
R
T
[3]
10 E
D
[4]
6
[ 5]
12
Bubble Sort:
10
Pass One
values [1] 24 U
N
S
[2] 10 O
R
T
[3]
36 E
D
[4]
6
[ 5]
12
Bubble Sort:
11
Pass One
values [1] 24 U
N
S
[2] 10 O
R
T
[3]
6 E
D
[4]
36
[ 5]
12
Bubble Sort: 12End Pass One
values [1] 24 N
S
O
[2] 10 R
T
E
[3]
6 D
[4]
12
[ 5] SORTED
36
Bubble Sort:
13
Pass Two
values [1] 24 N
S
O
[2] 10 R
T
E
[3]
6 D
[4]
12
[ 5] SORTED
36
Bubble Sort:
14
Pass Two
values [1] 10 N
S
O
[2] 24 R
T
E
[3]
6 D
[4]
12
[ 5] SORTED
36
Bubble Sort:
15
Pass Two
values [1] 10 N
S
O
[2] 6 R
T
E
[3]
24 D
[4]
12
[ 5] SORTED
36
Bubble Sort: End
16
Pass Two
U
values [1] 10 N
S
O
[2] 6 R
T
E
[3]
12 D
[4]
24
SORTED
[ 5]
36
Bubble Sort:17 Pass Three
U
values [1] 10 N
S
O
[2] 6 R
T
E
[3]
12 D
[4]
24
SORTED
[ 5]
36
Bubble Sort:18 Pass Three
U
values [1] 6 N
S
O
[2] 10 R
T
E
[3]
12 D
[4]
24 SORTED
[ 5]
36
Bubble Sort: 19End Pass Three
values [1] 6
UNSORTED
[2] 10
[3]
12 S
O
[4] R
24 T
E
[ 5] D
36
Bubble Sort:
20
Pass Four
values [1] 6
UNSORTED
[2] 10
[3]
12 S
O
[4] R
24 T
E
[ 5] D
36
Bubble Sort: 21 End Pass Four
6
S
10 O
R
12 T
24 E
D
36
Bubble Sort: Pseudo code
Begin
for i=1 to n-1
for j = 1 to n-i
if (A[j]> A[j+1]))
swap(A, j, j+1)
End 22
Insertion Sort
23
25
12
Insertion Sort
26
6 10 24 36
12
Insertion Sort
27
6 10 24 3
6
12
Insertion Sort
28
1 0 12 24
6 36
Insertion
29
Sort
values [0]
36 SORTED
[1]
U
24 N
[2] S
O
[3]
10 R
T
[4] 6 E
D
12
Insertion Sort:
31
Pass Two
values [0]
24 SORTED
[1]
36 U
[2]
N
[3]
10 S
O
R
[4] 6 T
E
D
12
Insertion Sort:
32
Pass Three
values [0] S
10 O
[1] R
24 T
E
[2] D
[3]
36
[4] 6
UNSORTED
12
Insertion Sort:
33
Pass Four
values [0]
6 S
[1] O
R
10 T
[2] E
D
[3]
24
[4] 36
12 UNSORTED
Insertion Sort:
34
Pass Five
values [0]
6
[1]
S
10 O
[2] R
T
[3]
12 E
D
[4] 24
36
Insertion Sorting - Pseudocode
ALGORITHM InsertionSort(A[0..n − 1])
//Sorts a given array by insertion sort
//Input: An array A[0..n − 1] of n orderable elements
//Output: Array A[0..n − 1] sorted in non-decreasing
order
for i ←1 to n − 1 do
key ←A[i]
j ←i − 1
while j ≥ 0 and A[j ]> key do
A[j + 1]←A[j ]
j ←j − 1
A[j + 1]←key
Insertion Sort - Summary
36
Advantages
Good running time for “almost sorted” arrays (n)
Disadvantages
(n2) running time in worst and average case
Selection Sort
37
Idea:
Find the smallest element in the array
Exchange it with the element in the first position
Find the second smallest element and exchange it
with the element in the second position
Continue until the array is sorted
Disadvantage:
Running time depends only slightly on the amount of
order in the file
Selection Sort
values [0]
36 U
[1]
N
24 S
[2]
O
[3]
10 R
[4] 6 T
E
12 D
Selection Sort:41 End Pass One
values [0]
6 SORTED
[1]
U
24 N
[2] S
O
[3]
10 R
T
[4] 36 E
D
12
Selection Sort:
42
Pass Two
values [0]
6 SORTED
[1]
U
24 N
[2] S
O
[3]
10 R
T
[4] 36 E
D
12
Selection Sort: End Pass Two
43
values [0]
6 SORTED
[1]
10
[2]
U
[3]
24 N
S
O
[4] 36 R
T
12 E
D
Selection Sort: Pass Three
44
values [0]
6 SORTED
[1]
10
[2]
U
[3]
24 N
S
O
[4] 36 R
T
12 E
D
Selection Sort: End Pass Three
45
values [0] S
6 O
[1] R
10 T
E
[2] D
[3]
12
[4] 36
UNSORTED
24
Selection Sort: Pass Four
46
values [0] S
6 O
[1] R
10 T
E
[2] D
[3]
12
[4] 36
UNSORTED
24
Selection Sort:47 End Pass Four
values [0]
6
[1] S
10 O
[2]
R
[3]
12 T
[4] 24 E
D
36
Selection Sort: Pseudo code
ALGORITHM SelectionSort(A[0..n − 1])
//Sorts a given array by selection sort
//Input: An array A[0..n − 1] of orderable
elements
//Output: Array A[0..n − 1] sorted in
nondecreasing order
for i ←1 to n do
min←i
for j ←i + 1 to n do
if A[j ]<A[min] min←j
swap A[i] and A[min]
Searching Problem
It is known that
left + right
mid =
2
Binary Search: Example
61
Binary Search Algorithm
Binary63Search
Binary64Search