Skip to content

Commit 549915a

Browse files
author
Tony Sappe
committed
Made improvements to Bubble and Insertion algorithms
* Placed the algorithms within their own functions separate from the input and output code * Updated README
1 parent 5378088 commit 549915a

File tree

3 files changed

+106
-46
lines changed

3 files changed

+106
-46
lines changed

BubbleSort.py

+29-24
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11

22

3-
array=[];
4-
5-
# input
6-
print ("Enter any 6 Numbers for Unsorted Array : ");
7-
for i in range(0, 6):
8-
n=input();
9-
array.append(int(n));
10-
11-
# Sorting
12-
print("")
13-
for i in range(0, 6):
14-
for j in range(0,5):
15-
if (array[j]>array[j+1]):
16-
temp=array[j];
17-
array[j]=array[j+1];
18-
array[j+1]=temp;
19-
20-
# Output
21-
for i in range(0,6):
22-
print(array[i]);
23-
24-
25-
26-
3+
def simple_bubble_sort(int_list):
4+
count = len(int_list)
5+
swapped = True
6+
while (swapped):
7+
swapped = False
8+
for j in range(count - 1):
9+
if (int_list[j] > int_list[j + 1]):
10+
int_list[j], int_list[j + 1] = int_list[j + 1], int_list[j]
11+
swapped = True
12+
return int_list
13+
14+
15+
def main(num):
16+
inputs = []
17+
print("Enter any {} numbers for unsorted list: ".format(num))
18+
try:
19+
for i in range(num):
20+
n = input()
21+
inputs.append(n)
22+
except Exception as e:
23+
print(e)
24+
else:
25+
sorted_input = simple_bubble_sort(inputs)
26+
print('\nSorted list (min to max): {}'.format(sorted_input))
27+
28+
if __name__ == '__main__':
29+
print('==== Bubble Sort ====\n')
30+
list_count = 6
31+
main(list_count)

InsertionSort.py

+25-20
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
1-
array=[];
21

3-
# input
4-
print ("Enter any 6 Numbers for Unsorted Array : ");
5-
for i in range(0, 6):
6-
n=input();
7-
array.append(int(n));
8-
9-
# Sorting
10-
print("")
11-
for i in range(1, 6):
12-
temp=array[i]
13-
j=i-1;
14-
while(j>=0 and temp<array[j]):
15-
array[j+1]=array[j];
16-
j-=1;
17-
array[j+1]=temp;
18-
19-
# Output
20-
for i in range(0,6):
21-
print(array[i]);
2+
def simple_insertion_sort(int_list):
3+
for i in range(1, 6):
4+
temp = int_list[i]
5+
j = i - 1
6+
while(j >= 0 and temp < int_list[j]):
7+
int_list[j + 1] = int_list[j]
8+
j -= 1
9+
int_list[j + 1] = temp
2210

11+
return int_list
2312

2413

14+
def main(num):
15+
inputs = []
16+
print('Enter any {} numbers for unsorted list: '.format(num))
17+
try:
18+
for i in range(num):
19+
n = input()
20+
inputs.append(n)
21+
except Exception as e:
22+
print(e)
23+
else:
24+
sorted_input = simple_insertion_sort(inputs)
25+
print('\nSorted list (min to max): {}'.format(sorted_input))
2526

27+
if __name__ == '__main__':
28+
print('==== Insertion Sort ====\n')
29+
list_count = 6
30+
main(list_count)

README.md

+52-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,52 @@
1-
# Python-
2-
All Algorithms implemented in Python
1+
# The Algoritms - Python
2+
3+
### **All Algorithms implemented in Python!**
4+
5+
6+
## Sorting
7+
8+
9+
### Binary
10+
11+
12+
### Bubble
13+
![alt text][bubble-image]
14+
15+
From [Wikipedia][bubble-wiki]: Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.
16+
17+
__Properties__
18+
* Stable
19+
* Worst case performance O(n^2)
20+
* Best case performance O(n)
21+
* Average case performance O(n^2)
22+
23+
24+
###### View the algorithm in [action][bubble-toptal]
25+
26+
27+
### Caesar
28+
29+
30+
### Insertion
31+
![alt text][insertion-image]
32+
33+
From [Wikipedia][insertion-wiki]: Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort.
34+
35+
__Properties__
36+
* Stable
37+
* Worst case performance O(n^2)
38+
* Best case performance O(n)
39+
* Average case performance O(n^2)
40+
41+
42+
###### View the algorithm in [action][insertion-toptal]
43+
44+
45+
46+
[bubble-toptal]: https://www.toptal.com/developers/sorting-algorithms/bubble-sort
47+
[bubble-wiki]: https://en.wikipedia.org/wiki/Bubble_sort
48+
[bubble-image]: https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Bubblesort-edited-color.svg/220px-Bubblesort-edited-color.svg.png "Bubble Sort"
49+
50+
[insertion-toptal]: https://www.toptal.com/developers/sorting-algorithms/insertion-sort
51+
[insertion-wiki]: https://en.wikipedia.org/wiki/Insertion_sort
52+
[insertion-image]: https://upload.wikimedia.org/wikipedia/commons/7/7e/Insertionsort-edited.png "Insertion Sort"

0 commit comments

Comments
 (0)