Skip to content

Commit 73d0c76

Browse files
committed
Updated files
1 parent d796ae9 commit 73d0c76

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+3105
-79
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
2-
def BubbleSort(array):
3-
4-
# Loop to access each array element
5-
for i in range(len(array)):
6-
for j in range(0,len(array)-1-i):
7-
# Replace > with < for descending order
8-
if array[j] > array[j+1]:
9-
10-
# Swapping elements if elements are not in the intended order
11-
temp = array[j]
12-
array[j] = array[j+1]
13-
array[j+1] = temp
14-
return array
15-
16-
17-
# Driver code
18-
if __name__ == '__main__':
19-
Array = [-1,9,21,34,1,5,6,8,10,23,25,27,31]
20-
print("Array before sorting: ", Array)
1+
2+
def BubbleSort(array):
3+
4+
# Loop to access each array element
5+
for i in range(len(array)):
6+
for j in range(0,len(array)-1-i):
7+
# Replace > with < for descending order
8+
if array[j] > array[j+1]:
9+
10+
# Swapping elements if elements are not in the intended order
11+
temp = array[j]
12+
array[j] = array[j+1]
13+
array[j+1] = temp
14+
return array
15+
16+
17+
# Driver code
18+
if __name__ == '__main__':
19+
Array = [-1,9,21,34,1,5,6,8,10,23,25,27,31]
20+
print("Array before sorting: ", Array)
2121
print("Array after sorting: ", BubbleSort(Array))
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
1-
2-
def Heapify(Array, n, i):
3-
# Find largest among root and children
4-
largest = i
5-
l = 2 * i + 1
6-
r = 2 * i + 2
7-
8-
if l < n and Array[l] > Array[largest]:
9-
largest = l
10-
11-
if r < n and Array[r] > Array[largest]:
12-
largest = r
13-
14-
# If root is not largest, swap with largest and continue heapifying
15-
if largest != i:
16-
Array[i], Array[largest] = Array[largest], Array[i]
17-
Heapify(Array, n, largest)
18-
19-
20-
def HeapSort(Array):
21-
n = len(Array)
22-
23-
# Build a max heap
24-
for i in range(n//2, -1, -1):
25-
Heapify(Array, n, i)
26-
27-
# One by one extract elements
28-
for i in range(n-1, 0, -1):
29-
# Swap
30-
Array[i], Array[0] = Array[0], Array[i]
31-
32-
# Heapify root element
33-
Heapify(Array, i, 0)
34-
35-
36-
if __name__ == "__main__":
37-
Array = [-2, -3, -1, 11, 9, 12, 4, -5, -12, 6, 19, 20]
38-
HeapSort(Array)
39-
n = len(Array)
40-
print("Sorted array is")
41-
for i in range(n):
42-
print("%d " % Array[i], end='')
1+
2+
def Heapify(Array, n, i):
3+
# Find largest among root and children
4+
largest = i
5+
l = 2 * i + 1
6+
r = 2 * i + 2
7+
8+
if l < n and Array[l] > Array[largest]:
9+
largest = l
10+
11+
if r < n and Array[r] > Array[largest]:
12+
largest = r
13+
14+
# If root is not largest, swap with largest and continue heapifying
15+
if largest != i:
16+
Array[i], Array[largest] = Array[largest], Array[i]
17+
Heapify(Array, n, largest)
18+
19+
20+
def HeapSort(Array):
21+
n = len(Array)
22+
23+
# Build a max heap
24+
for i in range(n//2, -1, -1):
25+
Heapify(Array, n, i)
26+
27+
# One by one extract elements
28+
for i in range(n-1, 0, -1):
29+
# Swap
30+
Array[i], Array[0] = Array[0], Array[i]
31+
32+
# Heapify root element
33+
Heapify(Array, i, 0)
34+
35+
36+
if __name__ == "__main__":
37+
Array = [-2, -3, -1, 11, 9, 12, 4, -5, -12, 6, 19, 20]
38+
HeapSort(Array)
39+
n = len(Array)
40+
print("Sorted array is")
41+
for i in range(n):
42+
print("%d " % Array[i], end='')
4343

Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
2-
def insertionSort(Array):
3-
for i in range(1, len(Array)):
4-
key = Array[i]
5-
j = i - 1
6-
7-
# Compare key with each element on the left of it until an element
8-
# smaller than it is found
9-
# For descending order, change key<Array[j] to key>Array[j].
10-
while j >= 0 and key < Array[j]:
11-
Array[j + 1] = Array[j]
12-
j -= 1
13-
Array[j + 1] = key
14-
return Array
15-
16-
if __name__ == '__main__':
17-
Array = [-2, -3, -1, 11, 9, 12, 4, -5, -12, 6, 19, 20]
1+
2+
def insertionSort(Array):
3+
for i in range(1, len(Array)):
4+
key = Array[i]
5+
j = i - 1
6+
7+
# Compare key with each element on the left of it until an element
8+
# smaller than it is found
9+
# For descending order, change key<Array[j] to key>Array[j].
10+
while j >= 0 and key < Array[j]:
11+
Array[j + 1] = Array[j]
12+
j -= 1
13+
Array[j + 1] = key
14+
return Array
15+
16+
if __name__ == '__main__':
17+
Array = [-2, -3, -1, 11, 9, 12, 4, -5, -12, 6, 19, 20]
1818
print(insertionSort(Array))

0 commit comments

Comments
 (0)