mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
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:
parent
53780885a3
commit
549915acd4
@ -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)
|
||||
|
@ -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)
|
||||
|
54
README.md
54
README.md
@ -1,2 +1,52 @@
|
||||
# Python-
|
||||
All Algorithms implemented in Python
|
||||
# The Algoritms - 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"
|
||||
|
Loading…
Reference in New Issue
Block a user