Made improvements to Bubble and Insertion algorithms

* Placed the algorithms within their own functions separate from the input and output code
* Updated README
This commit is contained in:
Tony Sappe
2016-07-29 14:47:32 -04:00
parent 53780885a3
commit 549915acd4
3 changed files with 103 additions and 43 deletions

View File

@@ -1,26 +1,31 @@
array=[];
# input
print ("Enter any 6 Numbers for Unsorted Array : ");
for i in range(0, 6):
n=input();
array.append(int(n));
# Sorting
print("")
for i in range(0, 6):
for j in range(0,5):
if (array[j]>array[j+1]):
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
# Output
for i in range(0,6):
print(array[i]);
def simple_bubble_sort(int_list):
count = len(int_list)
swapped = True
while (swapped):
swapped = False
for j in range(count - 1):
if (int_list[j] > int_list[j + 1]):
int_list[j], int_list[j + 1] = int_list[j + 1], int_list[j]
swapped = True
return int_list
def main(num):
inputs = []
print("Enter any {} numbers for unsorted list: ".format(num))
try:
for i in range(num):
n = input()
inputs.append(n)
except Exception as e:
print(e)
else:
sorted_input = simple_bubble_sort(inputs)
print('\nSorted list (min to max): {}'.format(sorted_input))
if __name__ == '__main__':
print('==== Bubble Sort ====\n')
list_count = 6
main(list_count)