You are on page 1of 44

Simple Sorting and searching

Algorithm
Sorting Algorithms:
Sorting is one of the most important operations
performed by computers.
Sorting is a process of reordering a list of items
in either increasing or decreasing order.
A fundamental application for computers
Done to make finding data (searching) faster
 Many different algorithms for sorting
 One of the difficulties with sorting is working
with a fixed size storage container (array)
if resize, that is expensive (slow)
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
Contd.

The following are simple sorting algorithms used


to sort small-sized lists.
 Bubble Sort
Insertion Sort
Selection Sort
Bubble sort
One of the simplest sorting algorithms proceeds by
walking down the list, comparing adjacent elements,
and swapping them if they are in the wrong order.
The process is continued until the list is sorted.
In this method multiple swapping take place when we
pass through the list of elements sequentially.
If we compare pairs of adjacent elements and none
are out of order, the list is sorted.
If any are out of order, we must have to swap them to
get an ordered list
Bubble sort
1. Initialize the size of the list to be sorted to be the actual size of
the list.

2. Loop through the list until no element needs to be exchanged


with another
to reach its correct position.

2.1 Loop (i) from 0 to size of the list to be sorted - 2.


2.1.1 Compare the ith and (i + 1)st elements in the
unsorted list.
2.1.2 Swap the ith and (i + 1)st elements if not in order
( ascending or descending as desired).
2.2 Decrease the size of the list to be sorted by 1.
The Bubble Sort Algorithm

• The Bubble
Sort 70

algorithm 60

looks at pairs 50
40
of entries in
30
the array,
20
and swaps 10
their order if 0
needed. [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm

• The Bubble
Sort 70

algorithm 60

looks at pairs 50
40
of entries in
30
the array,
20
and swaps 10
their order
Swap?
if 0
needed. [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm

• The Bubble
Sort 70

algorithm 60

looks at pairs 50

40
of entries in
30
the array,
20
and swaps 10
their order
Yes!
if 0
needed. [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm

• The Bubble
Sort 70

algorithm 60

looks at pairs 50

40
of entries in
30
the array,
20
and swaps 10
their order
Swap?
if 0
needed. [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm

• The Bubble
Sort 70

algorithm 60

looks at pairs 50

40
of entries in
30
the array,
20
and swaps 10
their order
No.
if 0
needed. [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm

• The Bubble
Sort 70

algorithm 60

looks at pairs 50

40
of entries in
30
the array,
20
and swaps 10
their order
Swap?
if 0
needed. [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm

• The Bubble
Sort 70

algorithm 60

looks at pairs 50

40
of entries in
30
the array,
20
and swaps 10
their order
No.
if 0
needed. [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm

• The Bubble
Sort 70

algorithm 60

looks at pairs 50

40
of entries in
30
the array,
20
and swaps 10
their order
Swap?
if 0
needed. [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm

• The Bubble
Sort 70

algorithm 60

looks at pairs 50

40
of entries in
30
the array,
20
and swaps 10
their order
Yes!
if 0
needed. [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm

• The Bubble
Sort 70

algorithm 60

looks at pairs 50

40
of entries in
30
the array,
20
and swaps 10
their order
Swap?
if 0
needed. [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm

• The Bubble
Sort 70

algorithm 60

looks at pairs 50

40
of entries in
30
the array,
20
and swaps 10
their order
Yes!
if 0
needed. [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm

• Repeat.
70
60

50

40
30

20
10

Swap? No. 0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm

• Repeat.
70
60

50

40
30

20
10

Swap? No. 0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm

• Repeat.
70
60

50

40
30

20
10

Swap? Yes. 0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm

• Repeat.
70
60

50

40
30

20
10

Swap? Yes. 0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm

• Repeat.
70
60

50

40
30

20
10

Swap? Yes. 0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm

• Repeat.
70
60

50

40
30

20
10

Swap? Yes. 0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm

• Repeat.
70
60

50

40
30

20
10

Swap? No. 0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [5][6]
The Bubble Sort Algorithm

• Loop over
array n-1 70

times, 60

swapping 50

40
pairs of
30
entries as
20
needed. 10

Swap? No. 0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [5][6]
The Bubble Sort Algorithm

• Loop over
array n-1 70

times, 60

swapping 50

40
pairs of
30
entries as
20
needed. 10

Swap? Yes. 0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [5][6]
The Bubble Sort Algorithm

• Loop over
array n-1 70

times, 60

swapping 50

40
pairs of
30
entries as
20
needed. 10

Swap? Yes. 0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [5][6]
The Bubble Sort Algorithm

• Loop over
array n-1 70

times, 60

swapping 50

40
pairs of
30
entries as
20
needed. 10

Swap? Yes. 0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [5][6]
The Bubble Sort Algorithm

• Loop over
array n-1 70

times, 60

swapping 50

40
pairs of
30
entries as
20
needed. 10

Swap? Yes. 0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [5][6]
"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

29
"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 Swap 42
77 77 35 12 101 5

30
"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 7735 Swap 35
77 12 101 5

31
"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 Swap 12
77 77 101 5

32
"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

33
"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 Swap 101
101 5

34
"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

35
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 we’ll correctly


place all N elements.

36
Bubble Sort Code
void bubbleSort (int a[ ] , int size)
{
int i, j, temp;
for ( i = 0; i < size; i++ ) /* controls passes through the list */
{
for ( j = 0; j < size - 1; j++ ) /* performs adjacent comparisons */
{
if ( a[ j ] > a[ j+1 ] ) /* determines if a swap should occur */
{
temp = a[ j ]; /* swap is performed */
a[ j ] = a[ j + 1 ];
a[ j+1 ] = temp;
}
}
}
}
37
Bubble Sort Example
9, 6, 2, 12, 11, 9, 3, 7
6, 9, 2, 12, 11, 9, 3, 7
Bubblesort compares the numbers in pairs from left to right
exchanging when necessary. Here the first number is compared to
the second and as it is larger they are exchanged.

6, 2, 9, 12, 11, 9, 3, 7
Now the next pair of numbers are compared. Again the 9 is the
larger and so this pair is also exchanged.

6, 2, 9, 12, 11, 9, 3, 7
In the third comparison, the 9 is not larger than the 12 so no
exchange is made. We move on to compare the next pair without
any change to the list.

6, 2, 9, 11, 12, 9, 3, 7
The 12 is larger than the 11 so they are exchanged.

6, 2, 9, 11, 9, 12, 3, 7
The twelve is greater than the 9 so they are exchanged
The end of the list has been reached so this is the end of the first pass. The

6, 2, 9, 11, 9, 3, 12, 7
twelve at the end of the list must be largest number in the list and so is now in
The 12
the correct is greater
position. Wethan
now the 3 so
start theypass
a new are exchanged.
from left to right.

6, 2, 9, 11, 9, 3, 7, 12
The 12 is greater than the 7 so they are exchanged. 38
Bubble Sort Example
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass

6, 6,
2, 2, 9, 9,
11,3,
11,
9,7,
11,
3,11,
7, 12
Notice that this time we do not have to compare the last two
numbers as we know the 12 is in position. This pass therefore only
requires 6 comparisons.

39
Bubble Sort Example
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass

Third Pass
2, 6, 9, 9, 3, 7, 11, 12
2, 6, 9, 3,
9, 9,
7, 9,
3, 7, 11, 12
This time the 11 and 12 are in position. This pass therefore only
requires 5 comparisons.

40
Bubble Sort Example
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass

Third Pass
2, 6, 9, 9, 3, 7, 11, 12
Fourth Pass
2, 6, 9, 3, 7, 9, 11, 12
2, 6, 3,
9, 9,
7, 9,
3, 7, 9, 11, 12
Each pass requires fewer comparisons. This time only 4 are needed.

41
Bubble Sort Example
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass

Third Pass
2, 6, 9, 9, 3, 7, 11, 12
Fourth Pass
2, 6, 9, 3, 7, 9, 11, 12
Fifth Pass
2, 6, 3, 7, 9, 9, 11, 12
2, 6,
3, 3,
6, 7, 9, 9, 11, 12
The list is now sorted but the algorithm does not know this until it
completes a pass with no exchanges.

42
Bubble Sort Example
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass

Third Pass
2, 6, 9, 9, 3, 7, 11, 12
Fourth Pass
2, 6, 9, 3, 7, 9, 11, 12
2, 6, 3, 7, 9, 9, 11, 12
Fifth Pass This pass no exchanges are made so the algorithm knows the list is
sorted. It can therefore save time by not doing the final pass. With

Sixth Pass
2, 3, 6, 7, 9, 9, 11,
other lists this check could save much more work.
12
2, 3, 6, 7, 9, 9, 11, 12
43
Efficiency of Bubble sort(complexity)
In general , where N is the number of items in the
array, there are N-1 comparisons on the first pass,
N-2 on the second, and so on. The formula for the
sum of such a series is:

(N-1) + (N-2) + (N-3) +…+ 1 = N*(N-1)/2

Therefore the bubble sort runs is O(N2)time.

You might also like