0% found this document useful (0 votes)
3 views83 pages

9 Sorting

The document discusses sorting algorithms, focusing on selection sort and insertion sort, and their implementations in C++. It highlights the importance of efficiency in sorting techniques, considering factors like coding time, execution time, and memory requirements. Additionally, it provides a detailed analysis of the selection sort algorithm, including its time complexity and step-by-step process.

Uploaded by

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

9 Sorting

The document discusses sorting algorithms, focusing on selection sort and insertion sort, and their implementations in C++. It highlights the importance of efficiency in sorting techniques, considering factors like coding time, execution time, and memory requirements. Additionally, it provides a detailed analysis of the selection sort algorithm, including its time complexity and step-by-step process.

Uploaded by

abuyousefyazan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Sorting I

Introduction
• Common problem: sort a list of values, starting
from lowest to highest.
– List of exam scores
– Words of dictionary in alphabetical order
– Students names listed alphabetically
– Student records sorted by ID#
• Generally, we are given a list of records that have
keys. These keys are used to define an ordering of
the items in the list.
C++ Implementation of Sorting
• Use C++ templates to implement a generic sorting
function.
• This would allow use of the same function to sort items
of any class.
• However, class to be sorted must provide the following
overloaded operators:
– Assignment: =
– Ordering: >, <, ==
• Example class: C++ STL string class
• In this lecture, we’ll talk about sorting integers;
however, the algorithms are general and can be applied
to any class as described above.
Efficiency of a sorting technique:
• How to select a sorting technique for a given set of elements?
• There are number of sorting techniques available to sort a
given array of data items. Each sorting technique has its own
advantages and disadvantages. Different techniques are useful
in different applications.
• There are 3 most important factors are counted while selecting
a sorting technique, which are.
1. Coding time: The amount of time invested in writing full length
sorting program.
2. Execution time (Time complicity): The amounts of time
required execute the sorting program. This normally frequency
of execution of statements in a program i.e. number of times
statements are executed.
3. Memory requirement (Space complicity): The amount of
memory required to store the entire sorting program in main
memory while execution.
Analysis of a sorting technique:
• Analysis of a sorting technique depends of three factors, which
are code time, time complicity and space complicity. Among
these 3 factors while analyzing a sorting technique we mainly
concentrate more on the time complicity.

• The time complicity is amount of time required to


execute the sorting program. Which is analyzed in
terms of 3 cases
1. Best case
2. Worst case
3. Average case
Quadratic Sorting Algorithms
• We are given n records to sort.
• There are a number of simple sorting
algorithms whose worst and average case
performance is quadratic O(n2):
– Selection sort
– Insertion sort
– Bubble sort
The Selection Sort Algorithm
Sorting an Array of Integers
• Example: we
are given an 70
array of six 60
integers that 50
we want to 40
sort from 30
smallest to 20
largest
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Selection Sort Algorithm
• Start by
finding the 70
smallest 60
entry. 50
40

30
20
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Selection Sort Algorithm
• Swap the
smallest 70
entry with 60
the first 50
entry. 40

30
20
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Selection Sort Algorithm
• Swap the
smallest 70
entry with 60
the first 50
entry. 40

30
20
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Selection Sort Algorithm
Sorted side Unsorted side
70
• Part of the 60
array is now 50
sorted. 40

30
20
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Selection Sort Algorithm
Sorted side Unsorted side
70
• Find the 60
smallest 50
element in 40
the unsorted 30
side.
20
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Selection Sort Algorithm
Sorted side Unsorted side
70
• Swap with 60
the front of 50
the unsorted 40
side. 30
20
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Selection Sort Algorithm
Sorted side Unsorted side
70
• We have 60
increased the 50
size of the 40
sorted side 30
by one
20
element.
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Selection Sort Algorithm
Sorted side Unsorted side
70
• The process 60
continues... Smallest
50 from
40 unsorted
30
20
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Selection Sort Algorithm
Sorted side Unsorted side
70
• The process 60
continues... 50
40

30
20
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Selection Sort Algorithm
Sorted side
is bigger Sorted side Unsorted side
70
• The process 60
continues... 50
40

30
20
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Selection Sort Algorithm
• The process Sorted side Unsorted side
keeps adding 70
one more 60
number to the 50
sorted side. 40
• The sorted side 30
has the smallest 20
numbers, 10
arranged from 0
small to large. [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Selection Sort Algorithm
• We can stop Sorted side Unsorted sid
when the 70
unsorted side 60
has just one 50
number, since 40
that number 30
must be the 20
largest number.
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Selection Sort Algorithm
• The array is
now sorted. 70

• We repeatedly 60
selected the 50
smallest 40
element, and 30
moved this 20
element to the 10
front of the 0
unsorted side. [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
template <class Item>
void selection_sort(Item data[ ], size_t n)
{
size_t i, j, smallest;
Item temp;

if(n < 2) return; // nothing to sort!!

for(i = 0; i < n-1 ; ++i)


{
// find smallest in unsorted part of array
smallest = i;
for(j = i+1; j < n; ++j)
if(data[smallest] > data[j]) smallest = j;

// put it at front of unsorted part of array (swap)


temp = data[i];
data[i] = data[smallest];
data[smallest] = temp;
}
}
Selection Time Sort Analysis
• In O-notation, what is:
– Worst case running time for n items?
– Average case running time for n items?
• Steps of algorithm:
for i = 1 to n-1
find smallest key in unsorted part of array
swap smallest item to front of unsorted array
decrease size of unsorted array by 1
Selection Time Sort Analysis
• In O-notation, what is:
– Worst case running time for n items?
– Average case running time for n items?
• Steps of algorithm:
for i = 1 to n-1 O(n)
find smallest key in unsorted part of array O(n)
swap smallest item to front of unsorted array
decrease size of unsorted array by 1
• Selection sort analysis: O(n2)
template <class Item>
void selection_sort(Item data[ ], size_t n)
{
size_t i, j, smallest;
Item temp;

if(n < 2) return; // nothing to sort!!

for(i = 0; i < n-1 ; ++i) Outer loop:


{ O(n)
// find smallest in unsorted part of array
smallest = i;
for(j = i+1; j < n; ++j)
if(data[smallest] > data[j]) smallest = j;

// put it at front of unsorted part of array (swap)


temp = data[i];
data[i] = data[smallest];
data[smallest] = temp;
}
}
template <class Item>
void selection_sort(Item data[ ], size_t n)
{
size_t i, j, smallest;
Item temp;

if(n < 2) return; // nothing to sort!!

for(i = 0; i < n-1 ; ++i) Outer loop:


{
// find smallest in unsorted part of array O(n)
smallest = i;
for(j = i+1; j < n; ++j) Inner loop:
if(data[smallest] > data[j]) smallest = j; O(n)
// put it at front of unsorted part of array (swap)
temp = data[i];
data[i] = data[smallest];
data[smallest] = temp;
}
}
The Insertion Sort Algorithm
• The Insertion
Sort algorithm 70
also views the 60
array as having 50
a sorted side 40
and an 30
unsorted side. 20
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Insertion Sort Algorithm
Sorted side Unsorted side
• The sorted 70

side starts 60

with just the 50


first 40
element, 30
which is not 20
necessarily 10
the smallest 0
element. [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Insertion Sort Algorithm
Sorted side Unsorted side

• The sorted 70

side grows 60

by taking the 50

front 40

element 30
from the 20
unsorted 10
side... 0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Insertion Sort Algorithm
Sorted side Unsorted side

• ...and 70

inserting it 60

in the place 50

that keeps 40

the sorted 30
side 20
arranged 10
from small 0
[1] [2] [3] [4] [5] [6]
to large. [0] [1] [2] [3] [4] [5]
The Insertion Sort Algorithm
Sorted side Unsorted side
70

60
50
40

30
20
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Insertion Sort Algorithm
Sorted side Unsorted side

• Sometimes 70

we are lucky 60

and the new 50

inserted item 40

doesn't need 30
to move at 20
all. 10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Insertionsort Algorithm
Sorted side Unsorted side

• Sometimes 70

we are lucky 60

twice in a 50

row. 40

30
20
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
How to Insert One Element
Copy the Sorted side Unsorted side
new element 70
to a separate
60
location.
50
40

30
20
10
0
[3] [4] [5] [6] [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
How to Insert One Element
Shift
elements in 70
the sorted
60
side,
50
creating an
40
open space
30
for the new
20
element.
10
0
[3] [4] [5] [6] [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
How to Insert One Element
Shift
elements in 70
the sorted
60
side,
50
creating an
40
open space
30
for the new
20
element.
10
0
[3] [4] [5] [6] [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
How to Insert One Element
Continue
shifting 70
elements...
60
50
40

30
20
10
0
[3] [4] [5] [6] [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
How to Insert One Element
Continue
shifting 70
elements...
60
50
40

30
20
10
0
[3] [4] [5] [6] [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
How to Insert One Element
...until you
reach the 70
location for
60
the new
50
element.
40

30
20
10
0
[3] [4] [5] [6] [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
How to Insert One Element
Copy the Sorted side Unsorted sid
new element 70
back into the
60
array, at the
50
correct
40
location.
30
20
10
0
[3] [4] [5] [6] [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
How to Insert One Element
• The last Sorted side Unsorted sid
element 70
must also be 60
inserted. 50
Start by 40
copying it... 30
20
10

0
[2] [3] [4] [5] [6] [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
Sorted Result

70

60
50
40

30
20
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
template <class Item>
void insertion_sort(Item data[ ], size_t n)
{
size_t i, j;
Item temp;

if(n < 2) return; // nothing to sort!!

for(i = 1; i < n; ++i)


{
// take next item at front of unsorted part of array
// and insert it in appropriate location in sorted part of array
temp = data[i];
for(j = i; data[j-1] > temp and j > 0; --j)
data[j] = data[j-1]; // shift element forward

data[j] = temp;
}
}
Insertion Sort Time Analysis
• In O-notation, what is:
– Worst case running time for n items?
– Average case running time for n items?
• Steps of algorithm:
for i = 1 to n-1
take next key from unsorted part of array
insert in appropriate location in sorted part of array:
for j = i down to 0,
shift sorted elements to the right if key > key[i]
increase size of sorted array by 1
Insertion Sort Time Analysis
• In O-notation, what is:
– Worst case running time for n items?
– Average case running time for n items?
• Steps of algorithm:
for i = 1 to n-1 Outer loop:
take next key from unsorted part of array O(n)
insert in appropriate location in sorted part of array:
for j = i down to 0,
shift sorted elements to the right if key > key[i]
increase size of sorted array by 1
Insertion Sort Time Analysis
• In O-notation, what is:
– Worst case running time for n items?
– Average case running time for n items?
• Steps of algorithm:
for i = 1 to n-1 Outer loop:
take next key from unsorted part of array O(n)
insert in appropriate location in sorted part of array:
for j = i down to 0, Inner loop:
shift sorted elements to the right if key > key[i] O(n)
increase size of sorted array by 1
template <class Item>
void insertion_sort(Item data[ ], size_t n)
{
size_t i, j;
Item temp;

if(n < 2) return; // nothing to sort!!

for(i = 1; i < n; ++i)


O(n)
{
// take next item at front of unsorted part of array
// and insert it in appropriate location in sorted part of array
temp = data[i];
for(j = i; data[j-1] > temp and j > 0; --j)
O(n)
data[j] = data[j-1]; // shift element forward

data[j] = temp;
}
}
BUBBLE SORT
• It is most popular sorting technique among all other techniques
because is very simple to understand and implement. It is also
called exchange or sinking sort
• Working of Bubble Sort
• The algorithm begins by comparing the element at the bottom of
the array with next element. If the first element is grater the
second element, then are swapped or exchanged.
• This process in then repeated for next two elements i.e. for
second and third element. After n-1 comparisons the largest of
all data items bubbles up to the top of the array.
• The first n-1 comparisons constitute first pass. During second
pass number of comparison is one les than previous pass i.e.
there are n-2 comparisons in the second pass. During second
pass second largest element bubbles up to the last but one
position.
The Bubble Sort Algorithm
Example 1
• The Bubble
Sort algorithm 70
looks at pairs 60
of entries in 50
the array, and 40
swaps their 30
order if 20
needed.
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm
• The Bubble
Sort algorithm 70
looks at pairs 60
of entries in 50
the array, and 40
swaps their 30
order if 20
needed.
10

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

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

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

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

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

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

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

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

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

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] [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] [6]
[5]
The Bubble Sort Algorithm
• Loop over
array n-1 70
times, 60
swapping pairs 50
of entries as 40
needed. 30
20
10

Swap? No. 0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm
• Loop over
array n-1 70
times, 60
swapping pairs 50
of entries as 40
needed. 30
20
10

Swap? Yes. 0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm
• Loop over
array n-1 70
times, 60
swapping pairs 50
of entries as 40
needed. 30
20
10

Swap? Yes. 0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm
• Loop over
array n-1 70
times, 60
swapping pairs 50
of entries as 40
needed. 30
20
10

Swap? Yes. 0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm
• Loop over
array n-1 70
times, 60
swapping pairs 50
of entries as 40
needed. 30
20
10

Swap? Yes. 0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm
• Loop over
array n-1 70
times, 60
swapping pairs 50
of entries as 40
needed. 30
20
10

Swap? No. 0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm
• Loop over
array n-1 70
times, 60
swapping pairs 50
of entries as 40
needed. 30
20
10

Swap? No. 0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm
• Continue
looping, until 70
done. 60
50
40
30
20
10

Swap? Yes. 0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
template <class Item>
void bubble_sort(Item data[ ], size_t n)
{
size_t i, j;
Item temp;

if(n < 2) return; // nothing to sort!!

for(i = 0; i < n-1; ++i)


{
for(j = 0; j < n-1;++j)
if(data[j] > data[j+1]) // if out of order, swap!
{
temp = data[j];
data[j] = data[j+1];
data[j+1] = temp;
}
}
}
template <class Item>
void bubble_sort(Item data[ ], size_t n)
{
size_t i, j;
Item temp;
bool swapped = true;

if(n < 2) return; // nothing to sort!!


for(i = 0; swapped and i < n-1; ++i)
{ // if no elements swapped in an iteration,
// then elements are in order: done!
for(swapped = false, j = 0; j < n-1;++j)
if(data[j] > data[j+1]) // if out of order, swap!
{
temp = data[j];
data[j] = data[j+1];
data[j+1] = temp;
swapped = true;
}
}
}
Bubble Sort Time Analysis
• In O-notation, what is:
– Worst case running time for n items?
– Average case running time for n items?
• Steps of algorithm:
for i = 0 to n-1
for j =0 to n-2
if key[j] > key[j+1] then swap
if no elements swapped in this pass through array, done.
otherwise, continue
Bubble Sort Time Analysis
• In O-notation, what is:
– Worst case running time for n items?
– Average case running time for n items?
• Steps of algorithm:
for i = 0 to n-1 O(n)
for j =0 to n-2 O(n)
if key[j] > key[j+1] then swap
if no elements swapped in this pass through array, done.
otherwise, continue
Timing and Other Issues
• Selection Sort, Insertion Sort, and Bubble Sort all
have a worst-case time of O(n2), making them
impractical for large arrays.
• But they are easy to program, easy to debug.
• Insertion Sort also has good performance when the
array is nearly sorted to begin with.
• But more sophisticated sorting algorithms are
needed when good performance is needed in all
cases for large arrays.
Bubble Sort Time Analysis
Example 2
 Consider following array A of elements.
A 30 10 5 20 15

A[0] A[1] A[2] A[3] A[4]

Begin the sort by comparing first two elements


30 10 5 20 15
Compare A[0] and A[1]. Since 30>10, interchange

10 30 5 20 15
Compare A[1] and A[2]. Since 30>5, interchange Pass 1

10 5 30 20 15
Compare A[2] and A[3]. Since 30>20, interchange

10 5 20 30 15
Compare A[3] and A[4]. Since 30>15, interchange

10 5 20 15 30
Largest element 30 has bubble up to last position
10 5 20 15 30 Compare A[0] and A[1]. Since 10>5, interchange

5 10 20 15 30 Compare A[1] and A[2]. Since 10<20, no interchange Pass 2

5 10 20 15 30 Compare A[2] and A[3]. Since 20>15, interchange

5 10 15 20 30 Second largest element bubbles up to the position last but one.

Compare A[0] and A[1]. Since 5<10, no interchange


5 10 15 20 30
Pass 3
Compare A[1] and A[2]. Since 10<15, no interchange
5 10 15 20 30

Third largest element is in its right position.


5 10 15 20 30

Compare A[0] and A[2]. Since 5>10, no interchange Pass 4


5 10 15 20 30

Final sorted array after n-1 passes.


5 10 15 20 30
Algorithm:
Algorithm: BUBBLE_SORT(A, n) This algorithm sort a given array A[n] using bubble sort technique. Variables I and J are used to index the array
and temp is a temporary variable.
Step1: start
Step2: Input the array A[n]
Step3: [Compute the sorting]
Repeat For I0 to n-1
Step4: [Compare the adjacent elements]
Repeat For J0 to n-1-I
Step5: If (A[J]>A[J+1])
[Interchange A[J] and A[J+1]]
TempA[J]
A[J]A[J+1]
A[J+1]temp
[End If]
[End step3 for loop]
[End step4 for loop]
Step6: [Display output]
Repeat For I0 to n-1
Output A[I]
[End for]
Step9: stop
Analysis of bubble sort:

 Best case: If the given array of elements is in the ascending order, the
outer for loop will be executed n-1 times. The inner for loop and if
statement will be executed n-1 times for the first iteration of the outer
for loop, n-2 times for the second iteration of the outer for loop and so
on . Only one time during the n-1th iteration of the outer for loop. The
interchange part will not be executed even once.

 Worst case: : If the given array of elements is in reverse order, the


outer for loop will be executed n-1 times. The inner for loop, if
statement and interchange part will be executed n-1 times for the first
iteration of the outer for loop, n-2 times for the second iteration of the
outer for loop and so on. Only one time during the n-1th iteration of the
outer for loop. Hence maximum number of comparisons and
interchange operations.
 Advantages:
1. Simple to understand and implement.
2. Very straight forward.
3. Better than selection sort.

 Disadvantages:
1. It runs slowly and hence it is not efficient, because more efficient
sorting techniques are available.
2. Even if array is sorted, n-1 comparisons are required.

You might also like