2
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.
4
Selection Sort
Selectionsort 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.
5.
5
Selection Sort
5 112 -5 16 2 12 14
Unsorted Array
Step 1 :
5 1 12 -5 16 2 12 14
Unsorted Array
0 1 2 3 4 5 6 7
Step 2 :
Min index = 0, value = 5
5 1 12 -5 16 2 12 14
0 1 2 3 4 5 6 7
Find min value from
Unsorted array
Index = 3, value = -5
Unsorted Array (elements 0 to
7)
Swap
-5 5
6.
6
Selection Sort
Step 3:
-5 1 12 5 16 2 12 14
0 1 2 3 4 5 6 7
Unsorted Array (elements 1
to 7)
Min index = 1, value = 1
Find min value from
Unsorted array
Index = 1, value = 1
No Swapping as min value is already at right place
1
Step 4 :
-5 1 12 5 16 2 12 14
0 1 2 3 4 5 6 7
Unsorted Array
(elements 2 to 7)
Min index = 2, value = 12
Find min value from
Unsorted array
Index = 5, value = 2
Swap
2 12
7.
7
Selection Sort
Step 5:
-5 1 2 5 16 12 12 14
0 1 2 3 4 5 6 7
Min index = 3, value = 5
Find min value from
Unsorted array
Index = 3, value = 5
Step 6 :
-5 1 2 5 16 12 12 14
0 1 2 3 4 5 6 7
Min index = 4, value = 16
Find min value from
Unsorted array
Index = 5, value = 12
Swap
Unsorted Array
(elements 3 to 7)
No Swapping as min value is already at right place
5
Unsorted Array
(elements 5 to 7)
12 16
8.
8
Selection Sort
Step 7:
-5 1 2 5 12 16 12 14
0 1 2 3 4 5 6 7
Min index = 5, value = 16
Find min value from
Unsorted array
Index = 6, value = 12
Swap
12 16
Unsorted Array
(elements 5 to 7)
-5 1 2 5 12 12 16 14
0 1 2 3 4 5 6 7
Min index = 6, value = 16
Find min value from
Unsorted array
Index = 7, value = 14
Swap
14 16
Unsorted Array
(elements 6 to 7)
Step 8 :
9.
9
SELECTION_SORT()
Steps to ImplementSelection 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]
11
SELECTION_SORT()
The time complexityof 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.
12.
12
Bubble Sort
Unlikeselection 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
)
14
BUBBLE_SORT()
We assume listis 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.
−
15.
15
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
17
Bubble_SORT()
The time complexityof 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.
18.
18
Merge Sort
Theoperation 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)
21
Insertion Sort
In insertionsort, every iteration moves an element from unsorted portion to
sorted portion until all the elements are sorted in the list.
Steps for Insertion Sort
1
Assume that first element in the list is in sorted portion of the list and
remaining all elements are in unsorted portion.
2
Select first element from the unsorted list and insert that element into
the sorted list in order specified.
3
Repeat the above process until all the elements from the unsorted list are
moved into the sorted list.
This algorithm is not suitable for large data sets
22.
22
Insertion Sort cont.
Complexityof 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
)
23.
23
Insertion Sort Example
Sortgiven array using Insertion Sort
Pass - 1 : Select First Record and considered as Sorter Sub-array
5 3 1 8 7 2 4
6 5 3 1 8 7 2 4
6 5 3 1 8 7 2 4
6
Sorted Unsorted
Pass - 2 : Select Second Record and Insert at proper place in sorted array
5 6 3 1 8 7 2 4
Sorted Unsorted
24.
24
Insertion Sort ExampleCont.
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
25.
25
Insertion Sort ExampleCont.
1 3 5 6 8 7 2 4
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
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
Sorted Unsorted
26.
26
Insertion Sort ExampleCont.
Pass - 7 : Select Seventh record and Insert at proper place in sorted array
1 2 3 5 6 7 8 4
Pass - 8 : Select Eighth Record and Insert at proper place in sorted array
Sorted Unsorted
1 3 5 6 7 8 2 4
1 2 3 5 6 7 8 4
1 2 3 5 6 7 8
4
Sorted Unsorted