Download as pdf or txt
Download as pdf or txt
You are on page 1of 46

VI

DATA STRUCTURES

Prof. Kapil Mundada


Prof. Vedant Shukla
Dept. of Instrumentation Engineering
VIT Pune
VI

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

• Bubble sort is comparison-based algorithm in which each pair of adjacent


elements is compared and the elements are swapped if they are not in order.
• This algorithm is not suitable for large data sets as its average and worst case
complexity are of Ο(n2) where ’n’ is the number of items.
• Bubble Sort compares all the element one by one and sort them based on their
values. If we have total n elements, then we need to repeat this process for n-
1 times.
• It is called bubble sort, as with every complete iteration the largest element
in the given array, bubbles up towards the last place, just like a water bubble
rises up to the water surface.
VI Bubble Sort Algorithm

STEP 1: Start with the first element (index = 0), compare the current element

with the next element of the array.

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.

STEP 4: Repeat Step 1.


Example Consider unsorted list

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

Compare 33 > 27 it is true so swap.

14 33 27 35 10

Compare 33 > 35 it is False No swapping


14 27 33 35 10

Compare 35 >10 it is true so swap.


14 27 33 35 10
List after Iteration 1

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

Best case : O (n^2 )

Average case : O (n^2 )

Worst case : O (n^2 )

Space Complexity of Bubble Sort


Since only one temp variable is needed, Space complexity is O(1)
VI
Try This !

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

17th Jan, 2014 14


Insertion Sort
VI
• Insertion sort algorithm arranges a list of elements in a particular order.
• Every iteration moves an element from unsorted portion to sorted
portion until all the elements are sorted in the list.

The insertion sort algorithm is performed using following steps...


Step 1: Assume that first element in the list is in sorted portion of the list and
remaining all elements are in unsorted portion.
Step 2: Consider first element from the unsorted list and insert that element
into the sorted list in order specified.
Step 3: Repeat the above process until all the elements from the unsorted list
are moved into the sorted list.
Insertion Sort Algorithm
VI
Step 1 − If it is the first element, it is already sorted. return 1;

Step 2 − Pick next element

Step 3 − Compare with all elements in the sorted sub-list

Step 4 − Shift all the elements in the sorted sub-list that is greater than

the value to be sorted

Step 5 − Insert the value

Step 6 − Repeat until list is sorted


Example Consider unsorted list

VI
0 1 2 3 4 5

15 20 10 55 30 50

Lets assume that sorted portion is empty


Sorted Unsorted
15 20 10 55 30 50

Move first element 15 from unsorted portion to sorted portion

Sorted Unsorted
15 20 10 55 30 50

To move 20 from unsorted to sorted portion compare 20 with 15 and insert it at


correct position
Sorted Unsorted
15 20 10 55 30 50
To move 10 from unsorted to sorted portion compare 10 with 20 , as it is smaller swap.
Again compare 10 with 15, as it is smaller again swap and insert it at correct position

VI Sorted Unsorted
10 15 20 55 30 50

To move 55 from unsorted to sorted portion compare 55 with 20,15,10 resp. as it is


larger than all, will be placed at last position
Sorted Unsorted
10 15 20 55 30 50
To move 30 from unsorted to sorted portion compare 30 with 55 as it is smaller swap,
and compare it with 20,15,10 resp. and put it at correct place
Sorted Unsorted
10 15 20 30 55 50
To move 50 from unsorted to sorted portion compare 50 with 55 , as smaller swap and
compare 50 with 30, 20, 15, 10 resp. and place it correctly.
Sorted Unsorted
10 15 20 30 50 55

Stop sorting as unsorted portion is empty


VI Complexity of the Insertion Sort Algorithm
To sort a unsorted list with 'n' number of elements we need to make (1+2+3+......+n-
1) = (n (n-1))/2 number of comparisions in the worst case.
If the list already sorted, then it requires 'n' number of comparisions.

Worst Case : O(n2)

Best Case : Ω(n)

Average Case : Θ(n2)

Space Complexity of Insertion Sort


Since only one temp variable is needed, Space complexity is O(1)
VI
Try This !

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

15 < 10 is false Swap values

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

20 < 15 is false swap values

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

Final sorted list

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.

Worst Case : O(n2)

Best Case : Ω(n2)

Average Case : Θ(n2)

Space Complexity of Selection Sort


Space complexity is O(1)
VI
Try This !

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

Step 1: Introduce the last element as ‘∞’

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

Step 5: Now check for the conditions of ‘i’ and ‘j’

i > pivot j < = pivot

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

Now, again check for the conditions as shown:

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 ∞

For this Our pivot For this unsorted part, repeat


unsorted part, is now the the procedure of sorting.
repeat the sorted Consider ‘50’ as ‘i’ and ’ ∞’ as
procedure of element ‘j’. Select any element as
sorting. pivot except ‘∞’.
Consider ‘20’
as ‘i’ and ’30’
as ‘j’. Select
any element
as pivot
except ‘30’.
Time Complexity of Quick Sort
VI
• Space required by quick sort is very less, only O(n*log n) additional space is required.
• Quick sort is not a stable sorting technique, so it might change the occurrence of two
similar elements in the list while sorting.

Worst Case Time Complexity [ Big-O ]: O(n2)

Best Case Time Complexity [Big-omega]: O(n*log n)

Average Time Complexity [Big-theta]: O(n*log n)

Space Complexity of Quick Sort


Space complexity is O(log n)
VI
Try This !
1. Using Quick Sort in the below given array, determine the total
number of swapping when index 0 element is chosen as pivot and
the first element is ‘i’ and last is ‘j’
23 5 14 24 16 27 12 19 6 21

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

STEP 1 Divide array of size 8 into two equal halves

14 33 27 10 35 19 42 44

Sequence of element does not change


STEP 2 Divide arrays into equal halves

14 33 27 10 35 19 42 44

STEP 3 Divide arrays into equal halves

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

Final sorted array


Time Complexity of Merge Sort
VI
Merge Sort is quite fast, and has a time complexity of O(n*log n). It is also a
stable sort, which means the "equal" elements are ordered in the same order in
the sorted list.

The time complexity of merge sort is as follows

Worst Case Time Complexity [ Big-O ]: O(n*log n)

Best Case Time Complexity [Big-omega]: O(n*log n)

Average Time Complexity [Big-theta]: O(n*log n)

Space Complexity of Merge Sort


Space complexity is O(n)
VI
Try This !
1. Using Merge Sort in the below given array, how many times will the
merging algorithm takes place to sort the following array.

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

3. Find the total number of list comparisons for implementation of


Merge Sort for the following array.
23 5 14 24 16 27 12 19 6 21

You might also like