0% found this document useful (0 votes)
7 views

Sorting-Bubble, Insertion and Selection

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Sorting-Bubble, Insertion and Selection

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 126

Sorting-Bubble Sort ,

Insertion Sort
and
Selection Sort

Dr. Kumkum Saxena


Sorting
 Important process in computing, especially in
data processing
 Telephone directories
 Sports league tables
 Lottery numbers
 Etc.

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 2


Sorting
Efficient sorting is important to optimizing the
use of other algorithms (such as search and
merge algorithms) that require sorted lists to
work correctly; it is also often useful for
canonicalizing data and for producing human-
readable output.

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 3


Sorting
Since the dawn of computing, the sorting
problem has attracted a great deal of research,
perhaps due to the complexity of solving it
efficiently despite its simple, familiar statement.

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 4


Sorting Arrays

Sorting, like searching, is also a common task in


computer programming. It would be used, for
instance, if you wanted to display the grades ,
“Assigning Grades,” in alphabetical order. Many
different algorithms have been developed for
sorting. This section introduces three simple,
intuitive sorting algorithms: selection sort ,bubble
sort and insertion sort.

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 5


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 6


Sorting
 External Sorts
 External storage devices used
 Large amounts of data
 Internal Sorts
 Fairly small lists
 Uses internal memory (RAM)

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 7


Bubble Sort
"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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 9


"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 Swap4277
77 35 12 101 5

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 10


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 11


"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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 12


"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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 13


"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 1015 Swap 101


5

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 14


"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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 15


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 2 3 4 5 6

42 35 12 77 5 101

Largest value correctly placed

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 16


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.

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 17


“Bubbling” All the Elements
1 2 3 4 5 6
42 35 12 77 5 101

1 2 3 4 5 6
35 12 42 5 77 101

1 2 3 4 5 6
N-1

12 35 5 42 77 101

1 2 3 4 5 6
12 5 35 42 77 101

1 2 3 4 5 6
5 12 35 42 77 101

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 18


Reducing the Number of
Comparisons
1 2 3 4 5 6
77 42 35 12 101 5
1 2 3 4 5 6
42 35 12 77 5 101

1 2 3 4 5 6
35 12 42 5 77 101

1 2 3 4 5 6
12 35 5 42 77 101

1 2 3 4 5 6
12 5 35 42 77 101

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 19


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 2 3 4 5 6
12 35 5 42 77 101

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 20


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


and1 “stop
2 early”!
3 4 5 6
5 12 35 42 77 101

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 21


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.”

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 22


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 23


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 24


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 25


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 26


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 27


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 28


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 29


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 30


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 31


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 32


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 33


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 34


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 35


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 36


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 37


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 38


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 39


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 40


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 41


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 42


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 43


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 44


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 45


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 46


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 47


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 48


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 49


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 50


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 51


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 52


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 53


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 54


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 55


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 56


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 57


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 58


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 59


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 60


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 61


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 62


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 63


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 64


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 65


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 66


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 67


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 68


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 69


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 70


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 71


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 72


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 73


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 74


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 75


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 76


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 77


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 78


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 79


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 80


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 81


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 82


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 83


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 84


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 85


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 86


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 87


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 88


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 89


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 90


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 91


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 92


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 93


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 94


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

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 95


Bubble Sort: Example

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 96


Bubble Sort: Example
One Pass

Array after
Completion
of Each Pass

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 97


Bubble Sort: Algorithm

 for pass = 1 .. n-1


 exchange = false
 for position = 1 .. n-pass
 if element at position < element at
position +1
 exchange elements
 exchange = true
 end if
 next position
 if exchange = false BREAK
 next pass

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 98


#include<stdio.h>
void main ()
{ int i, j,temp;
int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
for(i = 0; i<10; i++)
{
for(j = i+1; j<10; j++)
{
if(a[j] > a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
} } }
printf("Printing Sorted Element List ...\n");
for(i = 0; i<10; i++)
{ printf("%d\n",a[i]); } }
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 99
public void bubbleSort(int[] arr)
{
boolean swapped = true;
int j = 0;
int tmp;
while (swapped)
{
swapped = false;
j++;
for (int i = 0; i < arr.length - j; i++)
{
if (arr[i] > arr[i + 1])
{
tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
swapped = true;
} } }}

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 100
Bubble Sort: Analysis
 Number of comparisons (worst case):

 (n-1) + (n-2) + ... + 3 + 2 + 1  O(n²)


 Number of comparisons (best case):

 n – 1  O(n)
 Number of exchanges (worst case):
 (n-1) + (n-2) + ... + 3 + 2 + 1  O(n²)
 Number of exchanges (best case):

 0  O(1)
 Overall worst case: O(n²) + O(n²) = O(n²)

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 101
Insertion Sort
 Insertion sort is the simple sorting algorithm
which is commonly used in the daily lives
while ordering a deck of cards.
 In this algorithm, we insert each element onto
its proper place in the sorted array.
 This is less efficient than the other sort
algorithms like quick sort, merge sort, etc.

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 103
Technique
 Consider an array A whose elements are to be sorted. Initially,
A[0] is the only element on the sorted set. In pass 1, A[1] is
placed at its proper index in the array.
 In pass 2, A[2] is placed at its proper index in the array.
Likewise, in pass n-1, A[n-1] is placed at its proper index into the
array.
 To insert an element A[k] to its proper index, we must compare it
with all other elements i.e. A[k-1], A[k-2], and so on until we find
an element A[j] such that, A[j]<=A[k].
 All the elements from A[k-1] to A[j] need to be shifted and A[k]
will be moved to A[j+1].

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 104
Insertion Sort
int[] myList = {2, 9, 5, 4, 8, 1, 6}; //
The insertion sort Unsorted
Step 1: Initially, the sorted sublist contains the 2 9 5 4 8 1 6
algorithm sorts a list first element in the list. Insert 9 to the sublist.

of values by
Step2: The sorted sublist is {2, 9}. Insert 5 to the 2 9 5 4 8 1 6
repeatedly inserting sublist.
an unsorted element
into a sorted sublist Step 3: The sorted sublist is {2, 5, 9}. Insert 4 to 2 5 9 4 8 1 6
the sublist.
until the whole list
is sorted. Step 4: The sorted sublist is {2, 4, 5, 9}. Insert 8 2 4 5 9 8 1 6
to the sublist.

Step 5: The sorted sublist is {2, 4, 5, 8, 9}. Insert 2 4 5 8 9 1 6


1 to the sublist.

Step 6: The sorted sublist is {1, 2, 4, 5, 8, 9}. 1 2 4 5 8 9 6


Insert 6 to the sublist.

Step 7: The entire list is now sorted 1 2 4 5 6 8 9

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 105
animation

Insertion Sort
int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted

2 9 5 4 8 1 6
2 9 5 4 8 1 6
2 5 9 4 8 1 6
2 4 5 9 8 1 6
2 4 5 8 9 1 6
1 2 4 5 8 9 6
1 2 4 5 6 8 9

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 106
How to Insert?

The insertion sort [0] [1] [2] [3] [4] [5] [6]
algorithm sorts a list list 2 5 9 4 Step 1: Save 4 to a temporary variable currentElement

of values by [0] [1] [2] [3] [4] [5] [6]


repeatedly inserting list 2 5 9 Step 2: Move list[2] to list[3]
an unsorted element [0] [1] [2] [3] [4] [5] [6]
into a sorted sublist list 2 5 9 Step 3: Move list[1] to list[2]
until the whole list
[0] [1] [2] [3] [4] [5] [6]
is sorted. list 2 4 5 9 Step 4: Assign currentElement to list[1]

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 107
Insertion Sort: Example
 The Card Player‘s Method: Insertion Sort

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 108
Insertion Sort: Example
 Insertion Sort:

 4 passes

 Pass 3

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 109
Insertion Sort: Analysis
 Number of comparisons (worst case):

 (n-1) + (n-2) + ... + 3 + 2 + 1  O(n²)


 Number of comparisons (best case):

 n –1  O(n)
 Number of exchanges (worst case):

 (n-1) + (n-2) + ... + 3 + 2 + 1  O(n²)


 Number of exchanges (best case):

 0  O(1)
 Overall worst case: O(n²) + O(n²) = O(n²)

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 110
void insertionSort(int[] arr)
{
int i, j, newValue;
for (i = 1; i < arr.length; i++)
{
newValue = arr[i];
j = i;
while (j > 0 && arr[j - 1] > newValue)
{
arr[j] = arr[j - 1];
j--;
}
arr[j] = newValue;
}
}

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 111
#include<stdio.h>
void main ()
{
int i,j, k,temp;
int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
printf("\nprinting sorted elements...\n");
for(k=1; k<10; k++)
{
temp = a[k];
j= k-1;
while(j>=0 && temp <= a[j])
{
a[j+1] = a[j];
j = j-1;
}
a[j+1] = temp;
}
}

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 112
Selection Sort
SELECTION SORT

• Selection sor t is a sor ting


algorithm, specifically an in-place
comparison sor t.

 I t has O(n2) time complexity, making it inefficient


on large lists, and generally performs worse than the
similar insertion sort.

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 114
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 115
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 116
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 117
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 118
Example:
Selection
Sort

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 119
 PSEUDO CODE FOR SELECTION SORT

for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
if(a[j]<a[min])
Min=j;
Swap(a[i},a[min}
}
}
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 120
// Selection Sort
#include <stdlib.h>
#define N 6
int main()
{
int a[N]= { 23, 78, 45, 8, 32, 56};
int i,j;
// Sort the array using Selection Sort
int minIndex;
for(i=0; i < N-1; i++)
{
// find the minimum element in the unsorted part of the
array
minIndex=i;
for(j=i+1; j < N; j++)
if(a[j] < a[minIndex])
minIndex = j;

// Swap a[i] with the smallest element


int temp = a[i];
a[i] = a[minIndex];
a[minIndex] = temp;
}}
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 121
Time Complexity
• Analysis of the worst case:
– Outer loop executes N-1 times (no exch rqd. for final element).
– Inner loop executes N-i-1 comparisons.
(N-1) + (N-2) + … + 2 + 1 = N*(N-1)/2 = O(N2)
The worst case occurs if the array is already sorted in descending order.
Best Case = O (n^2)
 Average Case = O(n^2)

Selection sort is an inplace sorting algorithm. So its takes


constant O(1) space.

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 122
Advantages
– Easy to write
– Can be done “in place”
– Can be done on linked lists too (keep a tail pointer)

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 123
Disadvantages
It is about N2 even in the best case for
comparisons.
So the running time (over all inputs)
is approximately O(N2).
The primary disadvantage of selection sort is
its poor efficiency when dealing with a huge
list of items.

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 124
Comparison of Quadratic Sorts

Comparisons Exchanges

Best Worst Best Worst

Selection O(n²) O(n²) O(1) O(n)


Sort
Bubble O(n) O(n²) O(1) O(n²)
Sort
Insertion O(n) O(n²) O(1) O(n²)
Sort

Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 125
Result Quadratic Algorithms
Pro Contra

Selection If array is in If array is


‘total disorder’ presorted
Sort
Bubble Sort If array is If array is in
presorted ‘total disorder’

Insertion If array is If array is in


presorted ‘total disorder’
Sort

 Overall: O(n²) is not acceptable


since there are nLog(n) algorithms
!
Dr. Kumkum Saxena Sorting- Bubble, Insertion and Selection page 126

You might also like