Skip to content

Commit 1d968b5

Browse files
Merge pull request geekcomputers#589 from somnath796/firstbranch
Adding tower of hanoi problem's program and bubble sort algorithm.
2 parents f538edd + 215fc73 commit 1d968b5

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

bubble sort.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'''Bubble Sort
2+
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.
3+
Example:
4+
First Pass:
5+
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.
6+
( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4
7+
( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2
8+
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.
9+
10+
Second Pass:
11+
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
12+
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
13+
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
14+
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
15+
Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.
16+
17+
Third Pass:
18+
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
19+
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
20+
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
21+
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )'''
22+
23+
# Python program for implementation of Bubble Sort
24+
25+
def bubbleSort(arr):
26+
n = len(arr)
27+
28+
# Traverse through all array elements
29+
for i in range(n):
30+
31+
# Last i elements are already in place
32+
for j in range(0, n-i-1):
33+
34+
# traverse the array from 0 to n-i-1
35+
# Swap if the element found is greater
36+
# than the next element
37+
if arr[j] > arr[j+1] :
38+
arr[j], arr[j+1] = arr[j+1], arr[j]
39+
40+
# Driver code to test above
41+
arr = [64, 34, 25, 12, 22, 11, 90]
42+
43+
bubbleSort(arr)
44+
45+
print ("Sorted array is:")
46+
for i in range(len(arr)):
47+
print ("%d" %arr[i]),

tower_of_hanoi.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'''Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move
2+
the entire stack to another rod, obeying the following simple rules:
3+
1) Only one disk can be moved at a time.
4+
2) Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk
5+
can only be moved if it is the uppermost disk on a stack.
6+
3) No disk may be placed on top of a smaller disk.
7+
APPROACH:
8+
Take an example for 2 disks :
9+
Let rod 1 = 'SOURCE', rod 2 = 'TEMPORARY', rod 3 = 'DESTINATION'.
10+
11+
Step 1 : Shift first disk from 'SOURCE' to 'TEMPORARY'.
12+
Step 2 : Shift second disk from 'SOURCE' to 'DESTINATION'.
13+
Step 3 : Shift first disk from 'TEMPORARY' to 'DESTINATION'.
14+
15+
The pattern here is :
16+
Shift 'n-1' disks from 'SOURCE' to 'TEMPORARY'.
17+
Shift last disk from 'SOURCE' to 'DESTINATION'.
18+
Shift 'n-1' disks from 'TEMPORARY' to 'DESTINATION'.
19+
'''
20+
def toh(n,s,t,d):
21+
if n==1:
22+
print(s,'-->',d)
23+
return
24+
toh(n-1,s,d,t)
25+
print(s,'-->',d)
26+
toh(n-1,t,s,d)
27+
28+
if __name__=="__main__":
29+
while 1:
30+
31+
n = int(input('''Enter number of disks:'''))
32+
33+
if n<0:
34+
print("Try Again with a valid input")
35+
continue
36+
elif n==0:
37+
break
38+
toh(n,'Source','Temporary','Destination')
39+
40+
print('ENTER 0 TO EXIT')
41+

0 commit comments

Comments
 (0)