Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views27 pages

DS - Lecture Week 4 Sorting

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

DS - Lecture Week 4 Sorting

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

Week-4

Sorting
Sorting
why sorting ?

 Efficient Searching: Sorting enables quicker and more efficient data


retrieval by facilitating algorithms like binary search.
 Optimizing other algorithms: Many algorithms perform optimally or
rely on data being in a sorted order, thus requiring sorting as a preliminary
step.
 Data Management: Sorting is essential for organizing data in databases,
file systems, and other data structures for efficient storage and retrieval.
 Enhanced User Experience: Sorting improves data readability and
usability by presenting information in a structured, organized manner,
such as listing files alphabetically in a directory.
2
Sorting

 Sorting
 Bubble sort
 Selection Sort
 Insertion Sort
 Merge Sort

3
Selection Sort
 Selection sort is a simple sorting algorithm.
 The list is divided into two parts,
 The sorted part at the left end and
 The unsorted part at the right end.
 Initially, the sorted part is empty and the unsorted part is the entire list.

 The smallest element is selected from the unsorted array and


swapped with the leftmost element, and that element becomes a part
of the sorted array.
 This process continues moving unsorted array boundary by one element
to the right.
 This algorithm is not suitable for large data sets as its average and
worst case complexities are of Ο(n2), where n is the number of items.
4
Selection Sort
Unsorted Array
5 1 12 -5 16 2 12 14

Step 1 :
Unsorted Array
5 1 12 -5 16 2 12 14
0 1 2 3 4 5 6 7

Step 2 :
Unsorted Array (elements Min index = 0, value = 5
0 to 7)
1 1 1 1
-5
5 1 -5
5 2 Find min value from
2 6 2 4 Unsorted array
0 1 2 3 4 5 6 7
Index = 3, value = -5
Swap

5
Selection Sort
Step 3 : Min index = 1, value = 1
Unsorted Array
(elements 1 to 7)
1 1 1 1 Find min value from
-5 1 5 2 Unsorted array
2 6 2 4 Index = 1, value = 1
0 1 2 3 4 5 6 7

No Swapping as min value is already at right place

Step 4 :
Unsorted Array
(elements 2 to 7)
Min index = 2, value = 12
1 1 1 1 1
-5 1 2 5 2 Find min value from
2 6 2 2 4
0 1 2 3 4 5 6 7 Unsorted array
Index = 5, value = 2
Swap

6
Selection Sort
Step 5 :
Unsorted Array
(elements 3 to 7)
Min index = 3, value = 5
1 1 1 1
-5 1 2 5 Find min value from
6 2 2 4
0 1 2 3 4 5 6 7 Unsorted array
Index = 3, value = 5

No Swapping as min value is already at right place

Step 6 :
Unsorted Array
(elements 5 to 7)
Min index = 4, value = 16
1 1 1 1
-5 1 2 5 Find min value from
6
2 2
6 2 4
0 1 2 3 4 5 6 7 Unsorted array
Index = 5, value = 12
Swap

7
Selection Sort
Step 7 :
Unsorted Array
(elements 5 to 7)
Min index = 5, value = 16
1 1 1 1
-5 1 2 5 Find min value from
2 6
2 2
6 4
0 1 2 3 4 5 6 7 Unsorted array
Index = 6, value = 12
Swap

Step 8 :
Unsorted Array
(elements 6 to 7)
Min index = 6, value = 16
1 1 1 1
-5 1 2 5 Find min value from
2 2 6
4 4
6
0 1 2 3 4 5 6 7 Unsorted array
Index = 7, value = 14
Swap

8
SELECTION_SORT()
Steps to Implement Selection Sort
 for i ← 1 to n-1 do
 min ← i
 for j ← i+1 to n do
 { if A[j] < A[min] then{
 min ← j}}
 swap A[i], A[min]

9
SELECTION_SORT()
The time complexity of Selection Sort:

Worst Case: O(n²)

Best Case: ?
Average Case: ?

10
SELECTION_SORT()
The time complexity of Selection Sort:

Worst Case: O(n²)

Best Case: O(n²)


Average Case: O(n²)

Selection sort consistently performs the same number of


comparisons and swaps regardless of the initial order of
elements in the array. In each pass, it finds the minimum
element from the unsorted portion and places it at the
correct position, which always requires iterating through
the remaining unsorted elements.

11
Bubble Sort
 Unlike selection sort, instead of finding the smallest record and
performing the interchange, two records are interchanged immediately
upon discovering that they are out of order
 During the first pass R1 and R2 are compared and interchanged in
case of out of order, this process is repeated for records R2 and R3, and
so on.
 This method will cause records with small key to move “bubble up”,
 After the first pass, the record with largest key will be in the nth position.
 On each successive pass, the records with the next largest key will be
placed in position n-1, n-2 ….., 2 respectively
 This approached required at most n–1 passes, The complexity of bubble
sort is O(n2)
12
Bubble Sort
Unsorted Array
45 34 56 23 12

Pass 1 : Pass 2 : Pass 3 : Pass 4 :


3
4 3 3 3 3 3 3 2
3 2 1
2

swap
swap
swap

5
4
3 4 4 4 4 4
2 4
2 4
3
2 3
1 3
2
1

swap
swap
4
5 5 5
2 swap 5
2 5
2 5
3
2
4 3
4
1 3
4
1 4
2
1
3 2
3

swap
6
2 6
2 6
3
2
5 3
5
1 3
1 3
5
1 5
2
1
4 2
4 2
4 4

swap
3
1 3
1 3
6
1 6
2
1
5 2
5 2
5 2
5 5 5 5
2 2 2 2
6 6 6 6 6 6 6

13
BUBBLE_SORT()
We assume list is an array of n elements. We further assume that swap
function swaps the values of the given array elements.
 Step 1 − Check if the first element in the input array is greater than the
next element in the array.
 Step 2 − If it is greater, swap the two elements; otherwise move the
pointer forward in the array.
 Step 3 − Repeat Step 2 until we reach the end of the array.
 Step 4 − Check if the elements are sorted; if not, repeat the same
process (Step 1 to Step 3) from the last element of the array to the first.
 Step 5 − The final output achieved is the sorted array.

14
Procedure: BUBBLE_SORT ()

Pseudocode: Bubble-Sort(A)

for i ← 1 to less than n-1 do


for j ← 1 to less than n-i do
if A[j] > A[j+1] then
temp ← A[j]
A[j] ← A[j+1]
A[j+1] ← temp

15
Bubble_SORT()
The time complexity of Bubble Sort:

Worst Case: O(n²)

Best Case: ?
Average Case: ?

16
Bubble_SORT()
The time complexity of Bubble Sort:

Worst Case: O(n²)

Best Case: O(n)


Average Case: O(n²)

Bubble sort's best-case time complexity occurs when the


array is already sorted. In this scenario, it can detect
that no swaps are needed after the first pass and
terminate early.

17
Merge Sort
 The operation of sorting is closely related to process of merging
 Merge Sort is a divide and conquer algorithm
 It is based on the idea of breaking down a list into several sub-lists
until each sub list consists of a single element
 Merging those sub lists in a manner that results into a sorted list
 Procedure
 Divide the unsorted list into N sub lists, each containing 1 element
 Take adjacent pairs of two singleton lists and merge them to form a list of 2
elements. N will now convert into N/2 lists of size 2
 Repeat the process till a single sorted list of obtained

 Time complexity is O(n log n)

18
Merge Sort
Unsorted Array
72 52 2 98 52 31 18 45
40 11 2 3 9
4 5 9
6 1
7

Step 1: Split the selected array (as


evenly as possible)
0 1 2 3 4 5 6 7
72 52 52 18 45
2 98 31
4 1 9 9 1

72 52 52 18 45
2 98 31
4 1 9 9 1
0 1 2 3 0 1 2 3

19
Merge Sort
Step: Select the left subarray, Split the selected array (as evenly as possible)
0 1 2 3 0 1 2 3
72 52 52 18 45
2 98 31
4 1 9 9 1
0 1 0 1 0 1 0 1
72 52 52 18 45
2 98 31
4 1 9 9 1
0 0 0 0 0 0 0 0
72 52 52 18 45
2 98 31
4 1 9 9 1
52 72 52 18 45
2 98 31
1 4 9 9 1
52 72 18 45 52
2 98 31
1 4 9 1 9
18 45 52 52 72
2 31 98
9 1 1 9 4
20
Insertion Sort
In insertion sort, every iteration moves an element from unsorted
portion to sorted portion until all the elements are sorted in the list.

Steps for Insertion Sort


Assume that first element in the list is in sorted portion of the list
1
and remaining all elements are in unsorted portion.
Select first element from the unsorted list and insert that element
2
into the sorted list in order specified.
Repeat the above process until all the elements from the unsorted
3
list are moved into the sorted list.

This algorithm is not suitable for large data sets

21
Insertion Sort cont.
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 comparisons in the worst case.
If the list already sorted, then it requires 'n' number of comparisons.
• Worst Case : Θ(n2)
• Best Case : Ω(n)
• Average Case : Θ(n2)

22
Insertion Sort Example
Sort given array using Insertion Sort

6 5 3 1 8 7 2 4
Pass - 1 : Select First Record and considered as Sorter Sub-array

6 5 3 1 8 7 2 4
Sorted Unsorted

Pass - 2 : Select Second Record and Insert at proper place in sorted array

6 5 3 1 8 7 2 4
5 6 3 1 8 7 2 4
Sorted Unsorted

23
Insertion Sort Example Cont.
Pass - 3 : Select Third record and Insert at proper place in sorted array

5 6 3 1 8 7 2 4

3 5 6 1 8 7 2 4
Sorted Unsorted

Pass - 4 : Select Forth record and Insert at proper place in sorted array

3 5 6 1 8 7 2 4

1 3 5 6 8 7 2 4
Sorted Unsorted
24
Insertion Sort Example Cont.
Pass - 5 : Select Fifth record and Insert at proper place in sorted array

1 3 5 6 8 7 2 4
8 is at proper position

1 3 5 6 8 7 2 4
Sorted Unsorted

Pass - 6 : Select Sixth Record and Insert at proper place in sorted array

1 3 5 6 8 7 2 4

1 3 5 6 7 8 2 4
Sorted Unsorted
25
Insertion Sort Example Cont.
Pass - 7 : Select Seventh record and Insert at proper place in sorted array

1 3 5 6 7 8 2 4

1 2 3 5 6 7 8 4
Sorted Unsorted

Pass - 8 : Select Eighth Record and Insert at proper place in sorted array

1 2 3 5 6 7 8 4

1 2 3 4 5 6 7 8
Sorted Unsorted
26
Insertion_SORT()
The time complexity of Insertion Sort:

Worst Case: O(n²)

Best Case: O(n)


Average Case: O(n²)

27

You might also like