Skip to content

Commit 1d5d1b6

Browse files
committed
initial commit
1 parent 27a1e85 commit 1d5d1b6

8 files changed

+394
-0
lines changed

Bucket sort.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Bucket Sort in Python
2+
3+
4+
def bucketSort(array):
5+
bucket = []
6+
7+
# Create empty buckets
8+
for i in range(len(array)):
9+
bucket.append([])
10+
print("Bucket " , bucket , "\n")
11+
12+
# Insert elements into their respective buckets
13+
for j in array:
14+
index_b = int(10 * j)
15+
bucket[index_b].append(j)
16+
print("Bucket " , bucket , "\n")
17+
18+
# Sort the elements of each bucket
19+
for i in range(len(array)):
20+
bucket[i] = sorted(bucket[i])
21+
print("Bucket " , bucket , "\n")
22+
23+
a = []
24+
# Get the sorted elements
25+
for i in range(len(bucket)):
26+
for j in range(len(bucket[i])):
27+
if(bucket[i] != None ):
28+
a.append(bucket[i][j])
29+
30+
return a
31+
32+
33+
array = [.42, .32, .33, .52, .37, .47, .51]
34+
arr = [9.8, 0.6, 10.1, 1.9, 3.07, 3.04, 5.0, 8.0, 4.8, 7.68]
35+
print("Sorted Array in descending order is")
36+
a = bucketSort(arr)
37+
print(a)
38+
39+
40+
41+
42+
43+
# k = 0
44+
# for i in range(len(array)):
45+
# for j in range(len(bucket[i])):
46+
# array[k] = bucket[i][j]
47+
# print("Bucket sortin " , array , "\n")
48+
# k += 1

Exam 1.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
def Sort(array , n ):
2+
for i in range(n):
3+
4+
for j in range(n-i-1):
5+
6+
if(array[j+1] < array[j]):
7+
t = array[j]
8+
array[j] = array[j+1]
9+
array[j+1] = t
10+
return array
11+
12+
13+
14+
def SpecificSort(array , k ):
15+
for i in range(3):
16+
17+
for j in range(3-i-1):
18+
19+
if(array[j+1][k] < array[j][k]):
20+
t = array[j]
21+
array[j] = array[j+1]
22+
array[j+1] = t
23+
return array
24+
25+
26+
27+
A = [[9,8,5] ,[7,1,2],[4,3,6] ]
28+
M = []
29+
30+
for i in range(3):
31+
for j in range(3):
32+
M.append(A[i][j])
33+
34+
35+
M = Sort(M , len(M))
36+
37+
s = 0
38+
for i in range(3):
39+
for j in range(3):
40+
A[i][j] = M[s]
41+
s += 1
42+
43+
print(A)
44+
45+
46+
#for i in range(3):
47+
# print(Sort(A[i] , len(A)))

External merge sort (Heap sort ).py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
sample_list = [45, 345, 2, 4, 6, 645, 56, 65, 78, 34, 34, 34, 345, 90, 55, 23, 12, 40, 11]
2+
# split 1 = L
3+
# split 2 = M
4+
5+
def external_merge_sort(list):
6+
L = []
7+
M= []
8+
n = len(list)
9+
10+
while len(L) == 0 or len(M) == 0:
11+
for i in range(n):
12+
13+
if i == 0:
14+
L.append(list[i])
15+
split_list = True
16+
print("Split list 1 = " , split_list)
17+
18+
else:
19+
if list[i] < list[i - 1]:
20+
if split_list == True:
21+
print("Split list 2 = " , split_list)
22+
split_list = False
23+
24+
else:
25+
split_list = True
26+
27+
if split_list == True:
28+
print("Split list 3 = " , split_list)
29+
L.append(list[i])
30+
else:
31+
print("Split list 4 = " , split_list)
32+
M.append(list[i])
33+
34+
print("L = " , L , "\n")
35+
print("M = " , M , "\n")
36+
37+
38+
39+
list = []
40+
Li = 0
41+
Mi = 0
42+
if len(M) == 0:
43+
return L
44+
45+
for i in range(len(L) + len(M)):
46+
if i == 0:
47+
if L[i] < M[i]:
48+
list.append(L[Li])
49+
Li += 1
50+
else:
51+
list.append(M[Mi])
52+
Mi += 1
53+
else:
54+
if Li == len(L):
55+
list.append(M[Mi])
56+
Mi += 1
57+
elif Mi == len(M):
58+
list.append(L[Li ])
59+
Li += 1
60+
else:
61+
if L[Li ] < M[Mi]:
62+
list.append(L[Li ])
63+
Li += 1
64+
else:
65+
list.append(M[Mi])
66+
Mi += 1
67+
L = []
68+
M = []
69+
70+
print("Unsorted List:", sample_list)
71+
print("Sorted List:", external_merge_sort(sample_list))

Linear_selection sort.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Selection sort
2+
3+
# Selection sort is a sorting algorithm that selects the smallest element
4+
# from an unsorted list in each iteration and places that element at the beginning of the unsorted list.
5+
6+
7+
8+
9+
10+
11+
def selectionSort(array, size):
12+
13+
for i in range(size):
14+
min_idx = i
15+
16+
for j in range(i + 1, size):
17+
18+
# to sort in descending order, change > to < in this line
19+
20+
if array[j] < array[min_idx]:
21+
min_idx = j
22+
23+
24+
(array[i], array[min_idx]) = (array[min_idx], array[i])
25+
26+
27+
28+
29+
data = [-2, 45, 0, 11, -9]
30+
size = len(data)
31+
selectionSort(data, size)
32+
print("\n", 'Sorted Array in Ascending Order: ' , "\n")
33+
print(data , "\n")

bubble sort.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Bubble sort in Python
2+
3+
def bubbleSort(array):
4+
5+
# loop to access each array element
6+
for i in range(len(array)):
7+
8+
# loop to compare array elements
9+
for j in range(len(array) - i -1):
10+
11+
# compare two adjacent elements
12+
# change > to < to sort in descending order
13+
if array[j] > array[j + 1]:
14+
15+
# swapping elements if elements
16+
# are not in the intended order
17+
t = array[j]
18+
array[j] = array[j+1]
19+
array[j+1] = t
20+
21+
22+
23+
data = [-2, 45, 0, 11, -9 , 0 , 8 , 10 , -9]
24+
bubbleSort(data)
25+
26+
print('Sorted Array in Ascending Order:')
27+
print(data)

merge sort.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Merge Sort is one of the most popular sorting algorithms that is based on the principle of Divide and Conquer Algorithm.
2+
3+
4+
# MergeSort in Python
5+
6+
7+
def mergeSort(array):
8+
if len(array) > 1:
9+
10+
11+
12+
r = len(array)//2
13+
L = array[:r]
14+
M = array[r:]
15+
16+
17+
18+
19+
mergeSort(L)
20+
mergeSort(M)
21+
22+
i = j = k = 0
23+
24+
25+
while i < len(L) and j < len(M):
26+
if L[i] < M[j]:
27+
array[k] = L[i]
28+
i += 1
29+
else:
30+
array[k] = M[j]
31+
j += 1
32+
k += 1
33+
34+
35+
36+
while i < len(L):
37+
array[k] = L[i]
38+
i += 1
39+
k += 1
40+
41+
while j < len(M):
42+
array[k] = M[j]
43+
j += 1
44+
k += 1
45+
46+
47+
48+
def printList(array):
49+
for i in range(len(array)):
50+
print(array[i], end=" ")
51+
print()
52+
53+
54+
# Driver program
55+
if __name__ == '__main__':
56+
array = [6, 5, 12, 10, 9, 1]
57+
58+
mergeSort(array)
59+
60+
print("Sorted array is: ")
61+
printList(array)

quick sort.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# There are different variations of quicksort
2+
# where the pivot element is selected from different positions.
3+
# Here, we will be selecting the rightmost element of the array as the pivot element.
4+
5+
# pivoit number = left side per less than values or right side greater than pivot values
6+
7+
8+
9+
10+
11+
12+
13+
14+
15+
16+
def partition(array, low, high):
17+
18+
19+
pivot = array[high]
20+
i = low - 1
21+
22+
for j in range(low, high):
23+
if array[j] <= pivot:
24+
i += 1
25+
(array[i], array[j]) = (array[j], array[i])
26+
27+
28+
(array[i + 1], array[high]) = (array[high], array[i + 1])
29+
30+
31+
return i + 1
32+
33+
34+
35+
36+
37+
38+
39+
40+
def quickSort(array, low, high):
41+
if low < high:
42+
43+
44+
pi = partition(array, low, high)
45+
46+
47+
quickSort(array, low, pi - 1)
48+
49+
quickSort(array, pi + 1, high)
50+
51+
52+
data = [8, 7, 2, 1, 0, 9, 6]
53+
print("Unsorted Array")
54+
print(data)
55+
56+
size = len(data)
57+
58+
quickSort(data, 0, size - 1)
59+
60+
print('Sorted Array in Ascending Order:')
61+
print(data)

0 commit comments

Comments
 (0)