Skip to content

Commit 1c81120

Browse files
committed
Recursion on Arrays
1 parent 5f802f1 commit 1c81120

File tree

3 files changed

+123
-1
lines changed

3 files changed

+123
-1
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ Data Structures and Algorithms implementation in Python.
88
- [x] [Factorial](Recursion/Basic-Problems/factorial.py)
99
- [x] [Fibonacci](Recursion/Basic-Problems/fibonacci.py)
1010
- [x] [Sum of Geometric Progression](Recursion/Basic-Problems/GP.py)
11-
- [x] [Tower of Hanoi](Recursion/Basic-Problems/TOH.py)
11+
- [x] [Tower of Hanoi](Recursion/Basic-Problems/TOH.py)
12+
- [x] [Recursion on Arrays](Recursion/Arrays)
13+
- [x] [Merge Sort Array](Recursion/Arrays/mergesort.py)
14+
- [x] [Quick Sort Array](Recursion/Arrays/quicksort.py)

Recursion/Arrays/mergesort.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'''
2+
Merge Sort
3+
4+
Merge sort is a Divide and Conquer based Algorithm. It divides the input array into two-parts, until the size of the input array is not ‘1’. In the return part, it will merge two sorted arrays a return a whole merged sorted array.
5+
6+
This is the recursive approach for implementing merge sort. The steps needed to obtain the sorted array through this method can be found below:
7+
8+
1.The list is divided into left and right in each recursive call until two adjacent elements are obtained.
9+
10+
2.Now begins the sorting process. The i and j iterators traverse the two halves in each call. The k iterator traverses the whole lists and makes changes along the way.
11+
12+
3.If the value at i is smaller than the value at j, left[i] is assigned to the arr[k] slot and i is incremented. If not, then right[j] is chosen.
13+
14+
4.This way, the values being assigned through k are all sorted.
15+
16+
5.At the end of this loop, one of the halves may not have been traversed completely. Its values are simply assigned to the remaining slots in the list.
17+
18+
And with that, the merge sort has been implemented!
19+
20+
Time Complexity - O(nlogn)
21+
22+
'''
23+
24+
def mergesort(arr):
25+
if len(arr)>1:
26+
left = arr[:len(arr)//2]
27+
right = arr[len(arr)//2:]
28+
29+
#recursion
30+
mergesort(left)
31+
mergesort(right)
32+
33+
#merge
34+
i=0 #left array index
35+
j=0 #right array index
36+
k=0 #merged array index
37+
38+
#to look at every element in the right array and transfer every element from the right to the merged array
39+
while i<len(left) and j<len(right):
40+
if left[i] <right[j]:
41+
arr[k] = left[i]
42+
i += 1
43+
#k += 1
44+
else:
45+
arr[k] = right[j]
46+
j += 1
47+
#k += 1
48+
k += 1 #in both cases we need to increment k
49+
50+
#to look at every element in the left array and transfer every element from the left to the merged array
51+
while i<len(left): #because there's element missing from the left array to transfer
52+
arr[k] = left[i]
53+
i += 1
54+
k += 1
55+
56+
while j<len(right): #to check if we are not at the end of array
57+
arr[k] = right[j]
58+
j += 1
59+
k += 1
60+
61+
return arr
62+
63+
print(mergesort([8, 3, 5, 6, 11, 19, 7])) #[3, 5, 6, 7, 8, 11, 19]
64+
65+
66+

Recursion/Arrays/quicksort.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'''
2+
Quick Sort
3+
4+
Divide and conquer: Quicksort splits the array into smaller arrays until it ends up with an empty array, or one that has only one element, before recursively sorting the larger arrays.
5+
6+
The basic version of the algorithm does the following:
7+
8+
Divide the collection in two (roughly) equal parts by taking a pseudo-random element and using it as a pivot.
9+
10+
Elements smaller than the pivot get moved to the left of the pivot, and elements larger than the pivot to the right of it.
11+
12+
This process is repeated for the collection to the left of the pivot, as well as for the array of elements to the right of the pivot until the whole array is sorted.
13+
14+
Time Complexity - O(n^2)
15+
16+
'''
17+
18+
19+
def quicksort(arr,left,right):
20+
if left<right:
21+
partition_pos = partition(arr,left,right)
22+
quicksort(arr,left,partition_pos-1)
23+
quicksort(arr,partition_pos+1,right)
24+
return arr
25+
26+
#this function returns the index of the pivot element
27+
#after the first step of quicksort, when we have this index in partition_pos we can call quicksort on the original array
28+
# from index left to index partition position minus onewhich just means that we call quicksort on all elements that are less than the pivot element
29+
#we can call quick sort on the array from partition position plus one to the right so we call quicksort on the sub-array that contains all elements that are greater than the pivot element
30+
def partition(arr,left,right):
31+
i = left
32+
j = right-1
33+
pivot = arr[right] # choose the rightmost element as pivot
34+
35+
#i moves to the right and j moves to the left until i and j cross will be checked by this while loop
36+
while i<j:
37+
while i<right and arr[i] < pivot: #i to the right while i is not at the end of the array and element is less than pivot we increase i
38+
i+=1
39+
while j>left and arr[i] >= pivot: #similiar for j decrease j because j moves to the left
40+
j-=1
41+
42+
if i<j: #if i and j didn't cross we swap elements
43+
arr[i], arr[j] = arr[j], arr[i]
44+
45+
if arr[i]>pivot: #if i and j crossed means element at i is greater than pivot then do swap those elements
46+
arr[i],arr[right] = arr[right],arr[i]
47+
48+
return i #to determine where to split the array to call quick sort recursively
49+
50+
arr=[7,9,10,3,5,12,90,43]
51+
print(quicksort(arr,0,len(arr)-1)) #[5, 7, 9, 3, 10, 12, 43, 90]
52+
53+

0 commit comments

Comments
 (0)