Patil Konda Reddy
Technical Trainer
[email protected]
Sorting :
Def:
The process of arranging the data elements in an order either ascending or
descending order.
• Sorting arranges data in a sequence which makes searching easier.
• Since the beginning of the programming age, computer scientists have been
working on solving the problem of sorting by coming up with various algorithms to
sort the data.
The two main criteria to judge which algorithm is better than the other have been:
➢ Time taken to sort the given data.(Efficiency and performance)
➢ Memory space required to do so.
Different sorting algorithms :
There are different techniques available for sorting are differentiated by their
efficiency and space requirements. Some of the sorting techniques are:
1. Bubble sort
2. Selection sort
3. Insertion sort
4. Quick sort
5. Merge sort
Heap sort, radix sort, bucket sort
Bubble sort Algorithm:
➢ Bubble sort is a simple algorithm which is used to sort a given set of ‘n’ elements
provided in the form of a list with ‘n’ number of elements.
➢ Bubble sort compares all the elements one by one and sort them based on their
values.
➢ If the given list has to be sorted in ascending order, then the bubble sort will
start by comparing the first element of the list with the second element. If the
Patil Konda Reddy
Technical Trainer
[email protected]
first element is greater than the second element, it swaps both elements and then
move on to compare the second and third element and so on.
➢ If there are ‘n’ elements , the process will be repeated n-1 times.
➢ It is known as ‘bubble sort’, because with every complete iteration, the largest
element in the given list bubbles up towards the last place or towards the height
index.
➢ Sorting takes place by stepping through all the elements one-by-one and
comparing it with the adjacent element and swapping them if required.
Implementing Bubble sort method:
The following steps are involved in bubble sort for sorting an list in ascending order.
➢ Starting with the first element (index=0), compare the current element with the
next element of the list.
➢ If the current element is greater than the next element of the list, swaps them.
➢ If the current element is less than the next element, moves to the next element
and the repeat step-1.
Ex:
5 1 4 2 8
First pass:
5 1 4 2 8 1 5 4 2 8
Here, algorithm compares the first two elements and swap since 5>1
1 5 4 2 8 1 4 5 2 8
Swap since 5>4
Patil Konda Reddy
Technical Trainer
[email protected]
1 4 5 2 8 1 4 2 5 8
1 4 2 5 8 1 4 2 5 8
Since these elements are already in order (5>8), false, hence algorithm does not swap them
Second Pass:
1 4 2 5 8 1 4 2 5 8
1 4 2 5 8 1 2 4 5 8
Swap since 4 >2
1 2 4 5 8 1 2 4 5 8
1 2 4 5 8 1 2 4 5 8
Now, the list is already sorted, but the algorithm doesn’t know whether it is
completed. The algorithm needs one whole pass without any swap to know it is
sorted.
Third pass:
1 2 4 5 8 1 2 4 5 8
1 2 4 5 8 1 2 4 5 8
1 2 4 5 8 1 2 4 5 8
1. INPUT N
2. FOR I=0 TO N-1 DO
3. READ A[I]
[End of step2 FOR loop]
4. FOR I=0 TO I<(N-1) DO
5. FOR J=0 TO J<N-I-1 DO
6. IF (A[J]>A[J+1])
7. TEMP=A[J]
8. A[J]=A[J+1]
9. A[J+1]=TEMP
Patil Konda Reddy
Technical Trainer
[email protected]
[End of IF]
[End of step5 FOR loop]
[End of step4 FOR loop]
10. END
Complexity Analysis:
In bubble sort, n-1 comparisons will be done in the first pass, n-2 in second pass, n-3 in
third pass and so on. So Total no of comparisons will be
(n-1)+ (n-2) + (n-3)+ ………(n-1).
Sum=n(n-1)/2 => o(n2)
Hence the time complexity is O(n2)
# Bubble sort in Python
def bubbleSort(list):
# loop to access each list element
for i in range(len(list)):
# loop to compare list elements
for j in range(0, len(list) - i - 1):
# compare two adjacent elements
# change > to < to sort in descending order
if list[j] > list[j + 1]:
# swapping elements if elements
# are not in the intended order
temp = list[j]
list[j] = list[j+1]
list[j+1] = temp
data = [-2, 45, 0, 11, -9]
bubbleSort(data)
print('Sorted List in Ascending Order:')
print(data)
Selection sort :
Patil Konda Reddy
Technical Trainer
[email protected]
➢ Selection sort is conceptually the most simplest sorting algorithm.
➢ This Algorithm will first find smallest element in the list and swap it with the
element in the first position. Then it will find the second smallest element and swap
it with the element in the second position and it will keep on doing this until the
entire list is sorted.
➢ It is called selection sort because it repeatedly selects the next smallest element and
swaps it into the right place.
How Selection sort works:
The following steps are involved in selection sort for sorting an list in ascending
order.
1. Starting first element , we search the smallest element in the list, and replace it
with the element in the first postion.
2. When we move on to the second position, and look for the smallest element
present in sub list, starting from index 1, to last index.
3. Replace the element at the second position in the original list.
4. This is repeated, until the list is completely sorted.
1 2 4 5 6 8
Implementation:
➢ In the first pass, the smallest element will be 1, so it will be placed at the first
position
➢ Then leaving the first element, next smallest element will be searched from the
remaining elements. We get 2 as smallest, so it will be then placed at the second
position.
➢ Then leaving 1 and 2, we will search for the next smallest element from the rest
of the elements and put it at the third position and keep doing this until list is
sorted.
Patil Konda Reddy
Technical Trainer
[email protected]
Complexity Analysis:
➢ It has O(n²) time complexity. ... This algorithm sorts an list or list by repeatedly
finding the minimum value (if we are sorting in ascending order) from the list or
list and placing it at the beginning of the list.
➢
f(n)= (n-1)+(n-2)+…..+2+1=n(n-1)/2 = O(n2)
Space Complexity Analysis-
➢ Selection sort is an in-place algorithm.
➢ It performs all computation in the original list and no other list is used.
➢ Hence, the space complexity works out to be O(1).
# Selection sort in Python
def selectionSort(list, 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 list[i] < list[min_idx]:
min_idx = i
# put min at the correct position
(list[step], list[min_idx]) = (list[min_idx], list[step])
data = [-2, 45, 0, 11, -9]
size = len(data)
selectionSort(data, size)
print('Sorted List in Ascending Order:')
print(data)
Insertion sort :
Patil Konda Reddy
Technical Trainer
[email protected]
➢ This is an in-place comparison-based sorting algorithm.
➢ The name itself indicates that, compares each element with all the previous
elements and inserts into a desired location.
➢ Insertion sort is a simple sorting algorithm that works similar to the way you sort
playing cards in your hands. The list is virtually split into a sorted and an
unsorted part. Values from the unsorted part are picked and placed at the correct
position in the sorted part.
➢ Insertion sort algorithm picks elements one by one and places it to the right
position where it belongs in the sorted list of elements
How insertion sort works:
➢ In the first step second element compares with the first element.
➢ In the second step the third element compares with the first two elements.
➢ In the last step the nth element compares with all the previous elements.
735426
Algorithm
To sort an list of size n in ascending order:
1: Iterate from arr[1] to arr[n] over the list.
2: Compare the current element (key) to its predecessor.
3: If the key element is smaller than its predecessor, compare it to the elements before.
Move the greater elements one position up to make space for the swapped element.
# Insertion sort in Python
def insertionSort(list):
for step in range(1, len(list)):
key = list[step]
j = step - 1
Patil Konda Reddy
Technical Trainer
[email protected] # Compare key with each element on the left of it until an element smaller than it is
found
# For descending order, change key<list[j] to key>list[j].
while j >= 0 and key < list[j]:
list[j + 1] = list[j]
j=j-1
# Place key at after the element just smaller than it.
list[j + 1] = key
data = [9, 5, 1, 4, 3]
insertionSort(data)
print('Sorted List in Ascending Order:')
print(data)
Complexity Analysis :
Insertion Sort is an easy-to-implement, stable sorting algorithm with time complexity of
O(n²) in the average and worst case, and O(n) in the best case. For very small
n, Insertion Sort is faster than more efficient algorithms such as Quicksort or
Merge Sort.
Quick Sort :
➢ This sorting mechanism follows divide and conquer method.
➢ The name itself indicates, that a complex problem will be divided into sub
problems and each sub problem will be implemented.
➢ The list will be divided into partitions and that will be implemented
Using recursive methods.
➢ Quick sort is a highly efficient sorting algorithm and is based on partitioning of
list of data into smaller lists. A larger list is partitioned into two lists, one of
which holds values smaller than the specified value i.e pivot based on which the
partition is made and the another list holds values greater than the pivot value.
➢ Quick sort partitions an list and then calls itself recursively twice to sort the two
resulting sub lists.
Patil Konda Reddy
Technical Trainer
[email protected]
➢ This algorithm is quite efficient for large sized list as its average and worst case
complexities are of O(n2), where n is no of items.
Algorithm:
➢ 1. Choose starting element as PIVOT
➢ 2. Initialize i to starting element and j to ending element.
50 40 10 30 20
i j
➢ 3. Repeat the following procedure until i less than j
➢ Keep on increment i value until element at position i less than or equal to PIVOT
➢ Keep on decrement j value until element at position j greater than PIVOT
➢ if i less than j then swap element at position i & element at position j
➢ if i greater than j then swap element at position j & PIVOT Finally j position is the
PIVOT position so that elements left to PIVOT are less than PIVOT value and
elements right to PIVOT are greater than PIVOT. Do the same process for two
partitions partition1 - low to PIVOT-1 partition2 - PIVOT+1 to high
# Quick sort in Python
# function to find the partition position
def partition(list, low, high):
# choose the rightmost element as pivot
pivot = list[high]
# pointer for greater element
i = low - 1
# traverse through all elements
# compare each element with pivot
for j in range(low, high):
Patil Konda Reddy
Technical Trainer
[email protected] if list[j] <= pivot:
# if element smaller than pivot is found
# swap it with the greater element pointed by i
i=i+1
# swapping element at i with element at j
(list[i], list[j]) = (list[j], list[i])
# swap the pivot element with the greater element specified by i
(list[i + 1], list[high]) = (list[high], list[i + 1])
# return the position from where partition is done
return i + 1
#[8, 7, 2, 1, 0, 9, 6]
# function to perform quicksort
def quickSort(list, low, high):
if low < high:
# find pivot element such that
# element smaller than pivot are on the left
# element greater than pivot are on the right
pi = partition(list, low, high)
# recursive call on the left of pivot
quickSort(list, low, pi - 1)
# recursive call on the right of pivot
quickSort(list, pi + 1, high)
data = [8, 7, 2, 1, 0, 9, 6]
print("Unsorted List")
print(data)
size = len(data)
quickSort(data, 0, size - 1)
print('Sorted List in Ascending Order:')
print(data)
Patil Konda Reddy
Technical Trainer
[email protected]Merge Sort:
➢ Merge sort is a sorting technique based on divide and conquer technique.
➢ With the worst-case time complexity being Ο(n log n), it is one of the most
respected algorithms.
➢ Merge sort first divides the list into equal halves and then combines them in a
sorted manner.
Implementation:
➢ In Merge Sort, the given unsorted list with n elements, is divided into n sublists,
each having one element, because a single element is always sorted in itself.
Then, it repeatedly merges these sublists, to produce new sorted sublists, and in
the end, one complete sorted list is produced.
➢ The concept of Divide and Conquer involves three steps:
1. Divide the problem into multiple small problems.
2. Conquer the subproblems by solving them. The idea is to break down the
problem into atomic subproblems, where they are actually solved.
3. Combine the solutions of the subproblems to find the solution of the actual
problem
To understand merge sort, we take an unsorted list as the following −
We know that merge sort first divides the whole list iteratively into equal halves unless
the atomic values are achieved. We see here that an list of 8 items is divided into two
lists of size 4.
This does not change the sequence of appearance of items in the original. Now we
divide these two lists into halves.
Patil Konda Reddy
Technical Trainer
[email protected]
We further divide these lists and we achieve atomic value which can no more be
divided.
Now, we combine them in exactly the same manner as they were broken down. Please
note the color codes given to these lists.
We first compare the element for each list and then combine them into another list in a
sorted manner. We see that 14 and 33 are in sorted positions. We compare 27 and 10
and in the target list of 2 values we put 10 first, followed by 27. We change the order of
19 and 35 whereas 42 and 44 are placed sequentially.
In the next iteration of the combining phase, we compare lists of two data values, and
merge them into a list of found data values placing all in a sorted order.
After the final merging, the list should look like this −
Now we should learn some programming aspects of merge sorting.
Algorithm:
Merge sort keeps on dividing the list into equal halves until it can no more be divided.
By definition, if it is only one element in the list, it is sorted. Then, merge sort combines
the smaller sorted lists keeping the new list sorted too.
Step 1 − if it is only one element in the list it is already sorted, return.
Step 2 − divide the list recursively into two halves until it can no more be divided.
Step 3 − merge the smaller lists into new list in sorted order.
Complexity Analysis of Merge Sort:
Patil Konda Reddy
Technical Trainer
[email protected] ➢ Time complexity of Merge Sort is O(n*Log n) in all the 3 cases (worst, average
and best) as merge sort always divides the list in two halves and takes linear time
to merge two halves.
➢ It requires equal amount of additional space as the unsorted list.
# MergeSort in Python
def mergeSort(list):
if len(list) > 1:
# r is the point where the list is divided into two sublists
r = len(list)//2
L = list[:r]
M = list[r:]
# Sort the two halves
mergeSort(L)
mergeSort(M)
i=j=k=0
# Until we reach either end of either L or M, pick larger among
# elements L and M and place them in the correct position at A[p..r]
while i < len(L) and j < len(M):
if L[i] < M[j]:
list[k] = L[i]
i += 1
else:
list[k] = M[j]
j += 1
k += 1
# When we run out of elements in either L or M,
# pick up the remaining elements and put in A[p..r]
while i < len(L):
list[k] = L[i]
i += 1
k += 1
while j < len(M):
list[k] = M[j]
j += 1
Patil Konda Reddy
Technical Trainer
[email protected] k += 1
# Print the list
def printList(list):
for i in range(len(list)):
print(list[i], end=" ")
print()
# Driver program
if __name__ == '__main__':
list = [6, 5, 12, 10, 9, 1]
mergeSort(list)
print("Sorted list is: ")
printList(list)