Sorting PDF
Sorting PDF
DATA STRUCTURES
The Concept of
Sorting
Sorting
VI
• Sorting refers to arranging data in a particular format. Sorting algorithm
specifies the way to arrange data in a particular order.
• Sorting is also used to represent data in more readable formats.
• Examples Telephone Directory , Dictionary etc
Types of Sorting
Type of Sorting Description
In-place Sorting The program do not require any extra space and sorting
happens in-place, for example For e.g. Bubble sort.
Not In-place Sorting The program requires space which is more than or equal
to the elements being sorted. For example, Merge-sort
Stable Sorting A sorting algorithm, after sorting the contents, does not
change the sequence of similar content in which they
appear.
Unstable Sorting A sorting algorithm, after sorting the contents, changes
the sequence of similar content in which they appear.
VI
The Concept of
Bubble Sort
VI Bubble Sort Algorithm
STEP 1: Start with the first element (index = 0), compare the current element
STEP 2: If the current element is greater than the next element of the array,
swap them.
STEP 3: If the current element is less than the next element, move to the
next element.
VI 14 33 27 35 10
Iteration 1
Compare first two elements 14 and 33 . As 33 >14 no swapping.
14 33 27 35 10
14 33 27 35 10
VI 14 27 33 10 35
Iteration 2
Compare first two elements 14 and 27 .
14 27 33 10 35
As 14 < 27 no swapping.
Compare 27 and 33
14 27 33 10 35
As 27< 33 No swapping.
Compare 33 and 10
14 27 33 10 35
As, 33 > 10 swap
Compare 33 and 35 14 27 10 33 35
As 33< 35 No swapping
List after Iteration 2
VI 14 27 10 33 35
Iteration 3
Compare first two elements 14 and 27 .
14 27 10 33 35
As 14 < 27 no swapping.
Compare 27 and 10
14 27 10 33 35
As 10< 27 swap
Compare 27 and 33
14 10 27 33 35
As, 27 < 33 no swapping
Compare 33 and 35 14 10 27 33 35
As 33< 35 No swapping
List after Iteration 3
VI 14 10 27 33 35
Iteration 4
Compare first two elements 14 and 10
14 10 27 33 35
As 14> 10 swap.
Compare 14 and 27
10 14 27 33 35
As 24< 27 no swapping
Compare 27 and 33
10 14 27 33 35
As, 27 < 33 no swapping
Compare 33 and 35 10 14 27 33 35
As 33< 35 No swapping
List after Iteration 4
VI 10 14 27 33 35
Iteration 5
Compare first two elements 10 and 14
10 14 27 33 35
As 10<14 no swapping
Compare 14 and 27
10 14 27 33 35
As 24< 27 no swapping
Compare 27 and 33
10 14 27 33 35
As, 27 < 33 no swapping
Compare 33 and 35 10 14 27 33 35
As 33< 35 No swapping
Final sorted list
VI Time Complexity of Bubble Sort
The complexity of sorting algorithm is depends upon the number of comparisons
that are made. Two nested loops and ‘n’ possible swaps hence,
Total comparisons in Bubble sort is: n ( n – 1) / 2
1. Using Bubble Sort in the below given array, Display the list after 3
iterations.
23 5 14 24 16 27 12 19 6 21
2. Using Bubble Sort in the below given array, find the element at the
5th index after 10 number of comparisons.
23 5 14 24 16 27 12 19 6 21
3. Using Bubble Sort in the below given array, find the list after 20
number of comparisons.
23 5 14 24 16 27 12 19 6 21
VI
The Concept of
Insertion Sort
Step 4 − Shift all the elements in the sorted sub-list that is greater than
VI
0 1 2 3 4 5
15 20 10 55 30 50
Sorted Unsorted
15 20 10 55 30 50
VI Sorted Unsorted
10 15 20 55 30 50
1. Using Insertion Sort in the below given array, determine the position
of the bar after 12 number of comparisons.
23 5 14 24 16 27 12 19 6 21
2. Using Insertion Sort in the below given array, find the total number
of comparisons needed for sorting the array.
23 5 14 24 16 27 12 19 6 21
3. Using Insertion Sort in the below given array, find the number of
elements in the sorted list after 10 comparisons.
23 5 14 24 16 27 12 19 6 21
VI
The Concept of
Selection Sort
Selection Sort
VI
• In selection sort, the first element in the
Selection Sort Algorithm
list is selected and it is compared
repeatedly with remaining all the Step 1 : Set MIN to location 0
elements in the list. Step 2 : Search the minimum
• If any element is smaller than the element in the list
selected element (for Ascending order), Step 3 : Swap with value at
then both are swapped. location MIN
• Then we select the element at second
Step 4 : Increment MIN to point
position in the list and it is compared
to next element
with remaining all elements in the list
Step 5 − Repeat until list is
• If any element is smaller than the
sorted
selected element, then both are
swapped. .
Example Consider unsorted list
VI 15 20 10 30 50
Iteration :1
Take first element 15 and compare it with all other elements until
we get smaller value than 15 if we find smaller value then swap .
15 20 10 30 50
15 < 20 No swapping
15 20 10 30 50
10 20 15 30 50
10< 30 No swapping
10 20 15 30 50
10< 50 No swapping
List after
VI Iteration1
10 20 15 30 50
Iteration :2
Take second element 20 and compare it with all other elements until we get
smaller value than 20 if we find smaller value then swap .
10 20 15 30 50
10 15 20 30 50
15 < 30 No swapping
10 15 20 30 50
15 < 50 No swapping
List after 10 15 20 30 50
Iteration2
List after 10 15 20 30 50
Iteration2
VI
Iteration3 :
Take third element 20 and compare it with all other elements until we get smaller
value than 20 if we find smaller value then swap .
10 15 20 30 50
20< 30 No swapping
10 15 20 30 50
20< 50 No swapping
List after 10 15 20 30 50
Iteration 3
VI List after 10 15 20 30 50
Iteration 3
Iteration 4 :
Take fourth element 30 and compare it with all other elements until we get smaller
value than 30 if we find smaller value then swap .
10 15 20 30 50
30< 50 No swapping
10 15 20 30 50
VI Complexity of the Selection Sort Algorithm
• To sort a unsorted list with 'n' number of elements we need to
make ((n-1)+(n-2)+(n-3)+......+1) = (n (n-1))/2 number of
comparisons in the worst case.
• If the list already sorted, then it requires 'n' number of
comparisons.
1. Using Selection Sort in the below given array, determine the total
number of swapping in two iterations.
23 5 14 24 16 27 12 19 6 21
2. Using Selection Sort in the below given array, find the total number
of comparisons needed for sorting the array.
23 5 14 24 16 27 12 19 6 21
3. Using Selection Sort in the below given array, find the number of
comparisons and swapings for sorting the array.
23 5 14 24 16 27 12 19 6 21
VI
The Concept of
Quick Sort
Quick Sort
VI • Quick sort is based on partitioning of array of data into smaller arrays.
• A large array is partitioned into two arrays one of which holds values smaller than the
specified value, say pivot, based on which the partition is made and another array holds
values greater than the pivot value.
• Quick sort partitions an array and then calls itself recursively twice to sort the two resulting
sub arrays.
• This algorithm is quite efficient for large-sized data sets as its average and worst case
complexity are of Ο(n2), where n is the number of items.
• It is also called partition-exchange sort
• This algorithm divides the list into three main parts:
o Elements less than the Pivot element
o Pivot element(Central element)
o Elements greater than the pivot element
• Pivot element can be any element from the array, it can be the first element, the last element
or any random element.
Quick Sort Algorithm
VI Using pivot algorithm recursively, we end up with smaller possible partitions. Each partition is then
processed for quick sort.
We define recursive algorithm for quick sort as follows −
Step 1 : Introduce the right most element as ‘∞’
Step 2 : Consider the first element of the list to be ‘i’ and the last element (∞) as ‘j’
Step 3 :Consider any element of the list as the ‘pivot’ except ‘∞’. The insertion of ‘∞’ is mainly to
compute the position of pivot (middle element).
Step 4 : Check the following conditions:
i > pivot j < = pivot
Step 5 : If both the above conditions are satisfied, swap the elements ‘i’ and ‘j’.
Step 6 : If the condition of ‘i’ is not satisfied, perform i++. If the condition of ‘j’ is not satisfied,
perform j--.
Step 7 : In any case, ‘i’ and ‘j’ cross, swap ‘pivot and ‘j’. The pivotal element will be at the sorted
position.
Step 8 : The pivot divides the list into two parts. Quick sort needs to be implemented on each pert of
the list.
Example Consider unsorted list
VI
60 50 10 30 20 40
60 50 10 30 20 40 ∞
Step 2: Let the first element be ‘i’ and the last as ‘j’
60 50 10 30 20 40 ∞
i j
Step 3: Consider any element of the list as the ‘pivot’ except ‘∞’
60 50 10 30 20 40 ∞
i pivot j
Step 4: Consider any element of the list as the ‘pivot’ except ‘∞’
VI 60 50 10 30 20 40 ∞
i pivot j
Case 1
60 > 30 ∞ < = 30
Here, the first condition is satisfied but not the second. Hence, ‘i’ will
retain its position but ‘j’ will be decremented (j--) as shown below:
60 50 10 30 20 40 ∞
i pivot j
Now, again check for the conditions as shown:
VI 60 50 10 30 20 40 ∞
i pivot j
Case 2
60 > 30 40 < = 30
Again, the first condition is satisfied but not the second. Hence, ‘i’ will
retain its position but ‘j’ will be decremented (j--) as shown below:
60 50 10 30 20 40 ∞
i pivot j
Case 3
Now, again check for the conditions as shown:
60 > 30 20 < = 30
Here, both the conditions are satisfied. Hence, elements at ‘i’ and ‘j’ will
VI be swapped as shown below:
20 50 10 30 60 40 ∞
i pivot j
Once ‘i’ and ‘j’ are swapped, increment i and decrement j as shown:
20 50 10 30 60 40 ∞
i pivot
Case 4 j
50 > 30 30 < = 30
Here, both the conditions are satisfied. Hence, elements at ‘i’ and ‘j’ will
be swapped as shown below:
VI 20 30 10 50 60 40 ∞
pivot i
Case 5 j
Now, again check for the conditions as shown:
10 > 30 10 < = 30
Here, the second condition is satisfied but not the first one. Hence, ‘j’ will
retain its position but ‘i’ will be incremented (i++) as shown below:
20 30 10 50 60 40 ∞
pivot j i
VI Once ‘i’ and ‘j’ cross each other, we simply swap ‘pivot’ and ‘j’ as shown:
20 10 30 50 60 40 ∞
2. Using Quick Sort in the below given array, find the total number of
comparisons needed for sorting the array.
23 5 14 24 16 27 12 19 6 21
3. Using Quick Sort in the below given array, find the total number of
times when the ‘i’ and ‘j’ conditions are satisfied.
23 5 14 24 16 27 12 19 6 21
VI
The Concept of
Merge Sort
Merge Sort
VI
• Merge sort is a sorting technique based on divide and conquer technique.
• It consumes less time
• It is the best Sorting technique used for sorting Linked Lists.
• Merge sort first divides the array into equal halves and then combines them in a sorted
manner.
• It keeps on dividing the list into equal halves until it can no more be divided i.e. only one
element in the list.
• Then, merge sort combines the smaller sorted lists
• i.e. In Merge Sort, the given unsorted array with n elements, is divided into n sub arrays,
each having one element, because a single element is always sorted in itself. Then, it
repeatedly merges these sub arrays, to produce new sorted sub arrays, and in the end, one
complete sorted array is produced.
• It requires equal amount of additional space as the unsorted array. Hence its not at all
recommended for searching large unsorted arrays
Merge Sort Merge Sort Algorithm
VI 1. We take a variable p and store the
starting index of our array in this. And
Step 1: if it is only one element in
we take another variable r and store the
the list it is already sorted,
last index of array in it.
2. Then we find the middle of the array return.
using the formula (p + r)/2 and mark the
middle index as q, and break the array
Step 2 : divide a the list recursively
into two sub arrays, from p to q and
into two halves until it can
from q + 1 to r index.
3. Then we divide these 2 sub arrays again, no more be divided.
just like we divided our main array and
this continues.
Step 3: merge the smaller lists into
4. Once we have divided the main array
new list in sorted order.
into sub arrays with single elements,
then we start merging the sub arrays.
VI Example Consider unsorted list
14 33 27 10 35 19 42 44
14 33 27 10 35 19 42 44
14 33 27 10 35 19 42 44
14 33 27 10 35 19 42 44
STEP 4 Now we will merge them in sorted array
VI 14 33 27 10 35 19 42 44
Compare 14 and 33, 27 and 10, 35 and 19 and 42, 44 to prepare target list
with 2 values in ordered form
14 33 10 27 19 35 42 44
STEP 4 Now we will merge two data values and put them in ordered form
10 14 27 33 19 35 42 44
STEP 5 Now we will merge 4 data values and put them in ordered form
10 14 19 27 33 35 42 44
23 5 14 24 16 27 12 19 6 21
2. Using Merge Sort in the below given array, find the total number of
comparisons needed for sorting the array.
23 5 14 24 16 27 12 19 6 21