You are on page 1of 12

Sorting algorithms: Insertion sort

(ascending order)
Partition the file into two strings : The unsorted string followed by the sorted string
Algorithm:
For each record in the unsorted string
Repeat

Scan the sorted string until the first record is found with a key less than that of the
record from the unsorted string
Move the record from the unsorted string to the position in front of the record located
in the sorted string, moving the records in between forward by one position
Insertion sort
The following are the steps taken to carry out an insertion sort on a file with four records
Initial situation: 35 11 47 18
Insert 35 11 47 18 35
Insert 11 47 18 11 35
Insert 47 18 11 35 47
Insert 18 11 18 35 47

Carry out the steps of an insertion sort in ascending order for the following
a) 79 43 12 82 23 47
b) 45 34 56 23 18 14 12
c) 23 34 43 18 56 67 78
selection sort (ascending order)

 Partition the file into two strings : The unsorted string followed by the sorted
string
 Algorithm:
 Repeat

 Select the record with the highest key the unsorted string.
 Move this record from the front of the sorted string, moving the
intervening records forward by one place
 This moves the boundary of the sorted string forward by one record
 Until all records have been placed in the sorted string.
selection sort
(ascending order)
The following are the steps taken to carry out an Selection sort on a file with four records
Initial Pass: 35 11 47 18
Pass 35 11 18 47
Insert 11 18 11 35 47
Insert 47 11 18 35 47
Insert 18 11 18 35 47

Carry out the steps of a Selection sort in ascending order for the following
a) 79 43 12 82 23 47
b) 45 34 56 23 18 14 12
c) 23 34 43 18 56 67 78
Algorithm for quicksort :Fastest sorting
method
If the set contains more than one record then
Select the first record
Partition the remaining records into two subsets
A left subset; with keys less than that of the first
record
A right subset; with keys greater than that of the first
record
Place the first record between the two subsets
Quicksort the left subset
Quicksort the right subset
Else
The set is sorted
Algorithm for quicksort
Nb The algorithm is recursive , it calls itself repeatedly, for smaller and
smaller subsets of the set of data. For example quicksort is applied to
the keys of 8 records in a file. The square brackets show which elements
have not yet been sorted.
Original order of keys
[ 11 9 23 7 31 5 2 17]
Select the first key (11) and partition the set.
[9 7 5 2 ] 11 [23 31 17]
Quicksort the left subset
Select the first record (9) and partition the set .
[7 5 2 ] 9 11 [23 31 17]
Quicksort the right subset
Algorithm for quicksort
Select the first record (23) and partition the set
[7 5 2 ] 9 11 [17] 23 [31]
Quicksort the first remaining subset
Select the first record key (7) and partition the set
[5 2] 79 11 [17] 23 [31]
Quicksort the first (5) and partition the set
[2] 5 7 9 11 [17] 23 [31]
All remaining subsets are of length one element, and are therefore already sorted
2 5 7 9 11 23 31
Carry out the steps of a Selection sort in ascending order for the following
a) 79 43 12 82 23 47
b) 45 34 56 23 18 14 12
c) 23 34 43 18 56 67 78
Implementing Bubble Sort Algorithm
 Got its name from the fact that the sorted item floats to the top of the list
like a bubble in water.
 Following are the steps involved in bubble sort (for sorting a given array in
ascending order):
 Starting with the first element(index = 0), compare the current element with
the next element of the array.
 If the current element is greater than the next element of the array, swap
them.
 If the current element is less than the next element, move to the next
element. Repeat Step 1.
  
 Let's consider an array with values {5, 1, 6, 2, 4, 3}
 Below, we have a pictorial representation of how bubble sort will sort the
given array.
cont’
Cont’
 So as we can see in the representation above, after the first iteration, 6 is
placed at the last index, which is the correct position for it.
 Similarly after the second iteration, 5 will be at the second last index, and so
on.
Optimized Bubble Sort Algorithm
To optimize our bubble sort algorithm, we can introduce a flag to monitor whether elements are getting swapped inside
the inner for loop.
Hence, in the inner for loop, we check whether swapping of elements is taking place or not, everytime.
If for a particular iteration, no swapping took place, it means the array has been sorted and we can jump out of
the for loop, instead of executing all the iterations.
Let's consider an array with values {11, 17, 18, 26, 23}
Below, we have a pictorial representation of how the optimized bubble sort will sort the given array.
Cont’

You might also like