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,25 +1,30 @@
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(1, 6):
temp=array[i]
j=i-1;
while(j>=0 and temp<array[j]):
array[j+1]=array[j];
j-=1;
array[j+1]=temp;
# Output
for i in range(0,6):
print(array[i]);
def simple_insertion_sort(int_list):
for i in range(1, 6):
temp = int_list[i]
j = i - 1
while(j >= 0 and temp < int_list[j]):
int_list[j + 1] = int_list[j]
j -= 1
int_list[j + 1] = temp
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_insertion_sort(inputs)
print('\nSorted list (min to max): {}'.format(sorted_input))
if __name__ == '__main__':
print('==== Insertion Sort ====\n')
list_count = 6
main(list_count)