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=[]; def simple_bubble_sort(int_list):
count = len(int_list)
# input swapped = True
print ("Enter any 6 Numbers for Unsorted Array : "); while (swapped):
for i in range(0, 6): swapped = False
n=input(); for j in range(count - 1):
array.append(int(n)); if (int_list[j] > int_list[j + 1]):
int_list[j], int_list[j + 1] = int_list[j + 1], int_list[j]
# Sorting swapped = True
print("") return int_list
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 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)

View File

@ -1,25 +1,30 @@
array=[];
# input def simple_insertion_sort(int_list):
print ("Enter any 6 Numbers for Unsorted Array : "); for i in range(1, 6):
for i in range(0, 6): temp = int_list[i]
n=input(); j = i - 1
array.append(int(n)); while(j >= 0 and temp < int_list[j]):
int_list[j + 1] = int_list[j]
# Sorting j -= 1
print("") int_list[j + 1] = temp
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]);
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)

View File

@ -1,2 +1,52 @@
# Python- # The Algoritms - Python
All Algorithms implemented in Python
### **All Algorithms implemented in Python!**
## Sorting
### Binary
### Bubble
![alt text][bubble-image]
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.
__Properties__
* Stable
* Worst case performance O(n^2)
* Best case performance O(n)
* Average case performance O(n^2)
###### View the algorithm in [action][bubble-toptal]
### Caesar
### Insertion
![alt text][insertion-image]
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.
__Properties__
* Stable
* Worst case performance O(n^2)
* Best case performance O(n)
* Average case performance O(n^2)
###### View the algorithm in [action][insertion-toptal]
[bubble-toptal]: https://www.toptal.com/developers/sorting-algorithms/bubble-sort
[bubble-wiki]: https://en.wikipedia.org/wiki/Bubble_sort
[bubble-image]: https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Bubblesort-edited-color.svg/220px-Bubblesort-edited-color.svg.png "Bubble Sort"
[insertion-toptal]: https://www.toptal.com/developers/sorting-algorithms/insertion-sort
[insertion-wiki]: https://en.wikipedia.org/wiki/Insertion_sort
[insertion-image]: https://upload.wikimedia.org/wikipedia/commons/7/7e/Insertionsort-edited.png "Insertion Sort"