Lecture 1 Sorting I¶
Selection Sort¶
Given an array of N integers, write a program to implement the Selection sorting algorithm.
Example 1:
Input: N = 6, array[] = {13,46,24,52,20,9}
Output: 9,13,20,24,46,52
Explanation: After sorting the array is: 9, 13, 20, 24, 46, 52
Example 2:
Input: N=5, array[] = {5,4,3,2,1}
Output: 1,2,3,4,5
Explanation: After sorting the array is: 1, 2, 3, 4, 5
Approach 1¶
- Take the smallest and replace with i
In [1]:
def selection_sort(nums):
n = len(nums)
for i in range(n):
for j in range(i+1,n):
if nums[j] < nums[i]:
nums[i],nums[j] = nums[j],nums[i]
return nums
print(selection_sort([13,46,24,52,20,9]))
print(selection_sort([5,4,3,2,1]))
[9, 13, 20, 24, 46, 52] [1, 2, 3, 4, 5]
complexity analysus¶
- O(N^2) : N = len(nums)
- O(1)
Approach 2 (Using Recusrsion)¶
In [2]:
def selection_sort(nums):
n = len(nums)
def swap(i,j):
nums[i],nums[j] = nums[j],nums[i]
def set_min(pos, start):
if start ==n:
return
if nums[start] < nums[pos]:
swap(start,pos)
set_min(pos,start+1)
for i in range(n):
set_min(i,i+1)
return nums
print(selection_sort([13,46,24,52,20,9]))
print(selection_sort([5,4,3,2,1]))
[9, 13, 20, 24, 46, 52] [1, 2, 3, 4, 5]
Approach 3 (Full Recusrion)¶
In [4]:
def selection_sort(nums):
n = len(nums)
def swap(i,j):
nums[i],nums[j] = nums[j],nums[i]
def get_min_idx(pos,start):
if start ==n:
return pos
if nums[start] < nums[pos]:
return get_min_idx(start,pos+1)
return get_min_idx(pos,start+1)
def s_sort(pos):
if pos >= n:
return
min_idx = get_min_idx(pos,pos+1)
swap(min_idx,pos)
s_sort(pos+1)
s_sort(0)
return nums
print(selection_sort([13,46,24,52,20,9]))
print(selection_sort([5,4,3,2,1]))
[9, 13, 20, 24, 46, 52] [1, 2, 3, 4, 5]
Bubble Sort¶
Given an array of N integers, write a program to implement the Bubble sorting algorithm.
Example 1:
Input: N = 6, array[] = {13,46,24,52,20,9}
Output: 9,13,20,24,46,52
Explanation: After sorting the array is: 9, 13, 20, 24, 46, 52
Example 2:
Input: N=5, array[] = {5,4,3,2,1}
Output: 1,2,3,4,5
Explanation: After sorting the array is: 1, 2, 3, 4, 5
Approach 1¶
- just keep swapping left and right
- once there are not swaps , it means sorted
In [4]:
def bubble_sort(nums):
n = len(nums)
for i in range(n):
swapped = False
for j in range(n-1):
if nums[j] > nums[j+1]:
nums[j],nums[j+1] = nums[j+1],nums[j]
swapped = True
if not swapped:
return nums
return nums
print(bubble_sort([13,46,24,52,20,9]))
print(bubble_sort([5,4,3,2,1]))
[9, 13, 20, 24, 46, 52] [1, 2, 3, 4, 5]
Approach 2 (Without swapped check)¶
- we know every iteartion , last element will be the largest
- later we can even check of there is no swap happend in any iteration , return early
In [7]:
def bubble_sort(nums):
n = len(nums)
def swap(i,j):
nums[i],nums[j] = nums[j],nums[i]
for i in range(n):
for j in range(0,n-i-1):
if nums[j] > nums[j+1]:
swap(j,j+1)
return nums
print(bubble_sort([13,46,24,52,20,9]))
print(bubble_sort([5,4,3,2,1]))
[9, 13, 20, 24, 46, 52] [1, 2, 3, 4, 5]
complexity analysus¶
- O(N^2) : N = len(nums)
- O(1)
Using recursion¶
In [12]:
def bubble_sort(nums):
n = len(nums)
def swap(i,j):
nums[i],nums[j] = nums[j],nums[i]
def bubble(pos,end):
if pos == end-1:
return
if nums[pos] > nums[pos+1]:
swap(pos,pos+1)
return bubble(pos+1,end)
def b_sort(pos):
if pos==n:
return
bubble(0,n-pos)
b_sort(pos+1)
b_sort(0)
return nums
print(bubble_sort([13,46,24,52,20,9]))
print(bubble_sort([5,4,3,2,1]))
[9, 13, 20, 24, 46, 52] [1, 2, 3, 4, 5]
Insertion Sort¶
Given an array of N integers, write a program to implement the Insertion sorting algorithm.
Example 1:
Input: N = 6, array[] = {13,46,24,52,20,9}
Output: 9,13,20,24,46,52
Explanation: After sorting the array is: 9, 13, 20, 24, 46, 52
Example 2:
Input: N=5, array[] = {5,4,3,2,1}
Output: 1,2,3,4,5
Explanation: After sorting the array is: 1, 2, 3, 4, 5
Approach 1¶
- assume first element is sorted
- not insert the elements from 1 to n to correct sorted position
In [13]:
def insertion_sort(nums):
n = len(nums)
def swap(i,j):
nums[i],nums[j] = nums[j],nums[i]
for i in range(1,n):
j = i-1
while j >=0:
if nums[j] > nums[j+1]:
swap(j,j+1)
j-=1
return nums
print(insertion_sort([13,46,24,52,20,9]))
print(insertion_sort([5,4,3,2,1]))
[9, 13, 20, 24, 46, 52] [1, 2, 3, 4, 5]
complexity analysus¶
- O(N^2) : N = len(nums)
- O(1)
Approach 2(Using Recursion)¶
In [15]:
def insertion_sort(nums):
n = len(nums)
def swap(i,j):
nums[i],nums[j] = nums[j],nums[i]
def insert(pos):
if pos <0:
return
if nums[pos] > nums[pos+1]:
swap(pos,pos+1)
insert(pos-1)
def i_sort(pos):
if pos ==n-1:
return
insert(pos)
i_sort(pos+1)
i_sort(1)
return nums
print(insertion_sort([13,46,24,52,20,9]))
print(insertion_sort([5,4,3,2,1]))
[9, 13, 20, 24, 46, 52] [1, 2, 3, 5, 4]