3. 3
Introduction
0 1
• Sorting means arranging the elements of an array so that they are
placed in some relevant order which may be either ascending or
descending.
• sorting is also used to represent data in a more readable format. Some
real-life examples of sorting are:-
Contact List in Your Mobile Phone also contains all contacts arranged
alphabetically (lexicographically). So if you look for contact then you don’t have
to look randomly and can be searched easily and many others like Apps on your
phone.
Keywords in Your book are also in a lexicographical manner and you can find it
according to Chapter.
4. 4
Why Study Sorting?
0 2
• When you perform sorting on an array/elements, many problems
become easy (e.g. min/max, kth smallest/largest)
• Performing Sorting also givesnumber of algorithmic solutions that
contain many other ideas such as:
o Iterative
o Divide-and-conquer
o Comparison vs non-comparison based
o Recursive
5. 5
Why Study Sorting?
0 2
The main advantage of sorting is time complexity and that’s the most
important thing when you solve a problem because it’s not enough
you’re able to solve a problem but you should be able to solve it in the
minimum time possible.
Sometimes problems can be solved easily and quickly based on sorting
which can prevent you from every Coder’s Nightmare i.e. TLE (Time Limit
Exceeded).
6. 6
Sorting Categories
0 3
There are two different categories in sorting:
1. Internal sorting: If the input data is such that it can be
adjusted in the main memory at once, it is called internal
sorting.
2. External sorting: If the input data is such that it cannot be
adjusted in the memory entirely at once, it needs to be
stored in a hard disk, floppy disk, or any other storage
device. This is called external sorting.
7. 7
Types of Sorting algorithms
0 4
Although there is number of sorting algorithms best algorithm
is which can solve a problem in the minimum
time and minimum space required to do so. Some types of the
Sorting algorithm are:-
1. Bubble sort
2. Insertion Sort
3. Select Sort
8. 8
Types of Sorting algorithms
0 4
It’s one of the simplest algorithms which repeatedly iterates through
the list, compares the elements next to each other, and swaps
according to the condition passed to them.
This sorting algorithm 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
10. 10
Types of Sorting algorithms
0 4
Repeat “bubble up” how many times?
• If we have N elements
• And if each time we bubble an element, we place it in its correct
location…..
• Then we repeat the “bubble up” process N-1 times.
• This guarantees we’ll correctly place all N elements.
Bubble Sort
11. 11
Types of Sorting algorithms
0 4
Example 1 : 5, 12, 3, 9, 16
Pass 1
● 5, 12, 3, 9, 16
○ The list stays the same because 5 is less than 12.
● 5, 3, 12, 9, 16
○ 3 and 12 are switched because 3 is less than 12
● 5, 3, 9, 12, 16
○ 9 and 12 are switched since 9 is less than 12
● 5, 3, 9, 12, 16
○ 12 and 16 do not switch because 12 is less than 16
Bubble Sort
12. 12
Types of Sorting algorithms
0 4
Example 1 : 5, 12, 3, 9, 16
Pass 2
● 3, 5, 9, 12, 16
○ 3 is less than 5, so they switch
● 3, 5, 9, 12, 16
○ 5 is less than 9 so they remain in the same places
● 3, 5, 9, 12, 16
○ 12 is greater than 9 so they do not switch places
● 3, 5, 9, 12, 16
○ 12 and 16 are in numerical order so they don't switch
Bubble Sort
14. 14
Types of Sorting algorithms
0 4
Example 2
"Bubbling Up" the Largest Element
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons
and swapping
Bubble Sort
5
12
35
42
77 101
0 1 2 3 4 5
77 42 35 12 101 5
15. 15
Types of Sorting algorithms
0 4
Example 2
"Bubbling Up" the Largest Element
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons
and swapping
Bubble Sort
5
12
35
42
77 101
0 1 2 3 4 5
Swap
42 77
77 42 35 12 101 5
16. 16
Types of Sorting algorithms
0 4
Example 2
"Bubbling Up" the Largest Element
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons
and swapping
Bubble Sort
5
12
35
77
42 101
0 1 2 3 4 5
Swap
35 77
77 42 35 12 101 5
17. 17
Types of Sorting algorithms
0 4
Example 2
"Bubbling Up" the Largest Element
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons
and swapping
Bubble Sort
0 1 2 3 4 5
5
12
77
35
42 101
12 77
77 42 35 12 101 5
18. 18
Types of Sorting algorithms
0 4
Example 2
"Bubbling Up" the Largest Element
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons
and swapping
Bubble Sort
5
77
12
35
42 101
No need to swap
77 42 35 12 101 5
19. 19
Types of Sorting algorithms
0 4
Example 2
"Bubbling Up" the Largest Element
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons
and swapping
Bubble Sort
5
77
12
35
42 101
0 1 2 3 4 5
5 101
77 42 35 12 101 5
20. 20
Types of Sorting algorithms
0 4
Example 2
"Bubbling Up" the Largest Element
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons
and swapping
Bubble Sort
0 1 2 3 4 5
77
12
35
42 5 101
Largest value correctly placed
77 42 35 12 101 5
21. 21
Types of Sorting algorithms
0 4
for (i = 0; i < n; i++) {
for (j = 0; j < n - i - 1; j++) {
if (a[j] > a[j + 1]) {
tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
}
}
}
Bubble Sort
22. 22
Types of Sorting algorithms
0 4
Bubble Sort
a = [35, 10, 31, 11, 26]
print("Before sorting array elements are - ")
for i in a:
print(i, end = " ")
for i in range(0,len(a)):
for j in range(i+1,len(a)):
if a[j]<a[i]:
temp = a[j]
a[j]=a[i]
a[i]=temp
print("nAfter sorting array elements are - ")
for i in a:
print(i, end = " ")
23. 23
Types of Sorting algorithms
0 4
No it is your turn.
Bubble Sort
9 7 3 0 1 7 10
Sort these elements using bubble
sort algorithm
24. 24
Types of Sorting algorithms
0 4
Insertion sort is the sorting mechanism where the sorted array is built
having one item at a time.
The array elements are compared with each other sequentially
and then arranged simultaneously in some particular order.
The idea of the insertion sort is similar to the idea of sorting the
playing cards.
Insertion Sort
25. 25
Types of Sorting algorithms
0 4
• The array of values to be sorted is divided into two sets. One that stores sorted
values and another that contains unsorted values.
• The sorting algorithm will proceed until there are elements in the unsorted set.
• Suppose there are n elements in the array. Initially, the element with index 0
(assuming LB = 0) is in the sorted set. Rest of the elements are in the unsorted set.
• The first element of the unsorted partition has array index 1 (if LB = 0).
• During each iteration of the algorithm, the first element in the unsorted set is picked
up and inserted into the correct position in the sorted set.
Insertion Sort
26. 26
Types of Sorting algorithms
0 4
• Insertion sort algorithm divides the list into two parts, sorted and unsorted.
• initially sorted list contain only one element.
• in each pass, one element from the unsorted list is inserted at its correct position in
sorted list.
• Consider an unsorted list in an array.
Insertion Sort
32. 32
Types of Sorting algorithms
0 4
Insertion Sort INSERTION-SORT(A)
for i = 1 to n
key A [i]
←
j i
← – 1
while j > = 0 and A[j] > key
A[j+1] A[j]
←
j j
← – 1
End while
A[j+1] key
←
End for
33. 33
Types of Sorting algorithms
0 4
Insertion Sort #Function to do insertion sort
def insertionSort(arr):
# Traverse through 1 to len(arr)
for i in range(1, len(arr)):
key = arr[i]
# Move elements of arr[0..i-1], that are greater than key, to one
position ahead of their current position
j = i - 1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
# Driver code to test above
arr = [12, 11, 13, 5, 6]
insertionSort(arr)
print ("Sorted array is:")
for i in range(len(arr)):
print(arr[i])
Python program for implementation of Insertion Sort
34. 34
Types of Sorting algorithms
0 4
Insertion Sort
12 31 3 5 1 7 25 8 17 2
Sort these elements using insertion sort
algorithm
35. 35
Types of Sorting algorithms
0 4
• Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place
comparison-based algorithm in which 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.
Selection sort
36. 36
Types of Sorting algorithms
0 4
1. Find the smallest element in the array and swap it with the first element
of the array i.e. a[0].
2. The elements left for sorting are n-1 so far. Find the smallest element in
the array from index 1 to n-1 i.e. a[1] to a[n-1] and swap it with a[1].
3. Continue this process for all the elements in the array until we get a
sorted list.
Selection sort
37. 37
Types of Sorting algorithms
0 4
Selection sort
The picture shows an array of
six integers that we want to
sort from smallest to largest.
Start by finding the smallest
entry.
37
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
38. 38
Types of Sorting algorithms
0 4
Selection sort
Start by finding the smallest entry.
Swap the smallest entry with the first
entry.
38
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
39. 39
Types of Sorting algorithms
0 4
Selection sort
Start by finding the smallest entry.
Swap the smallest entry with the first
entry.
39
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
40. 40
Types of Sorting algorithms
0 4
Selection sort
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Find the smallest element in the unsorted side.
Swap with the front of the unsorted side.
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
41. 41
Types of Sorting algorithms
0 4
Selection sort
The process continues.
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Sorted side Unsorted side
Smallest
from
unsorted
[0] [1] [2] [3] [4] [5]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
Swap
with
front
43. 43
Types of Sorting algorithms
0 4
Selection sort
We can stop when the unsorted side has just one number, since that number
must be the largest number.
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
Sorted side Unsorted side
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
[0] [1] [2] [3] [4] [5]
The array is now sorted.
We repeatedly
selected the smallest
element, and moved
this element to the
front of the unsorted
side.
45. 45
Types of Sorting algorithms
0 4
Selection sort
for (i = 0; i < n-1; i++)
{
small = i; //minimum element in unsorted array
for (j = i+1; j < n; j++) {
if (arr[j] < arr[small])
small = j;
// Swap the minimum element with the first element
temp = arr[small];
arr[small] = arr[i];
arr[i] = temp;
}
46. 46
Types of Sorting algorithms
0 4
Selection sort Python program for implementation of selection Sort
def selectionSort(array, size):
for step in range(size):
min_idx = step
for i in range(step + 1, size):
# to sort in descending order, change > to < in this line
# select the minimum element in each loop
if array[i] < array[min_idx]:
min_idx = i
# put min at the correct position
(array[step], array[min_idx]) = (array[min_idx], array[step])
data = [64, 25, 12, 22, 11]
size = len(data)
selectionSort(data, size)
print('Sorted Array in Ascending Order:')
print(data)
47. 47
Types of Sorting algorithms
0 4
Selection sort
12 31 3 5 1 7 25 8 17 2
Sort these elements using selection sort
algorithm
48. 48
Linear Search
0 5
• The simplest solution to the sequence search problem is the
sequential or linear
search algorithm.
• This technique iterates over the sequence, one item at a
time, until the specific item is found or all items have been
examined.
49. 49
Linear Search
0 5
Linear Search Algorithm
procedure LINEAR_SEARCH (array, key)
for each item in the array
if match element == key
return element's index
end if
end for
end procedure
50. 50
Linear Search
0 5
Best Case Analysis:
• In the best case analysis, we calculate the lower bound of the execution
time of an algorithm. It is necessary to know the case which causes the
execution of the minimum number of operations. In the linear search
problem, the best case occurs when x is present at the first location.
• Best Case − Minimum time required for program execution.
Case Time Complexity
51. 51
Linear Search
0 5
Average Case Analysis:
• In the average case analysis, we take all possible inputs and calculate
the computation time for all inputs. Add up all the calculated values
and
divide the sum by the total number of entries.
• Average Case − Average time required for program execution.
Case Time Complexity
52. 52
Linear Search
0 5
Worst Case Analysis:
• In the worst-case analysis, we calculate the upper limit of the execution
time of an algorithm. It is necessary to know the case which causes the
execution of the maximum number of operations.
• Worst Case − Maximum time required for program execution.
Case Time Complexity
53. 53
Comparing the Simple Sorts
0 5
• Bubble Sort – simplest.
• Use only if you don’t have other algorithms available and ‘n’ is small.
• Selection Sort
• Minimizes number of swaps, but number of comparisons still high.
• Useful when amount of data is small and swapping is very time consuming – like when sorting
records in tables – internal sorts.
• Insertion Sort
• The most versatile, and is usually best bet in most situations, if amount of data is small or data is
almost sorted. For large n, other sorts are better. We will cover advanced sorts later.
• All require very little space and they sort in place.
• Can’t see the real efficiencies / differences unless you apply the different sources to large
amounts of data.
#4:iterative definition: 1. doing something again and again, usually to improve
A divide and conquer algorithm is a strategy of solving a large problem by breaking the problem it into smaller sub-problems,
involving doing or saying the same thing several times in order to produce a particular result…
The process in which a function calls itself directly or indirectly is called recursion