Changed QuickSort.py

Converted all indentations to spaces (different files had spaces or tabs)
This commit is contained in:
Tony Sappe 2016-07-29 15:31:20 -04:00
parent b7eae6b0e3
commit 37ddd2c8d0
5 changed files with 104 additions and 89 deletions

View File

@ -1,32 +1,32 @@
def simple_bubble_sort(int_list): def simple_bubble_sort(int_list):
count = len(int_list) count = len(int_list)
swapped = True swapped = True
while (swapped): while (swapped):
swapped = False swapped = False
for j in range(count - 1): for j in range(count - 1):
if (int_list[j] > int_list[j + 1]): if (int_list[j] > int_list[j + 1]):
int_list[j], int_list[j + 1] = int_list[j + 1], int_list[j] int_list[j], int_list[j + 1] = int_list[j + 1], int_list[j]
swapped = True swapped = True
return int_list return int_list
def main(): def main():
try: try:
print("Enter numbers separated by spaces:") print("Enter numbers separated by spaces:")
s = raw_input() s = raw_input()
inputs = list(map(int, s.split(' '))) inputs = list(map(int, s.split(' ')))
if len(inputs) < 2: if len(inputs) < 2:
print('No Enough values to sort!') print('No Enough values to sort!')
raise Exception raise Exception
except Exception as e: except Exception as e:
print(e) print(e)
else: else:
sorted_input = simple_bubble_sort(inputs) sorted_input = simple_bubble_sort(inputs)
print('\nSorted list (min to max): {}'.format(sorted_input)) print('\nSorted list (min to max): {}'.format(sorted_input))
if __name__ == '__main__': if __name__ == '__main__':
print('==== Bubble Sort ====\n') print('==== Bubble Sort ====\n')
main() main()

View File

@ -1,32 +1,32 @@
def simple_insertion_sort(int_list): def simple_insertion_sort(int_list):
count = len(int_list) count = len(int_list)
for i in range(1, count): for i in range(1, count):
temp = int_list[i] temp = int_list[i]
j = i - 1 j = i - 1
while(j >= 0 and temp < int_list[j]): while(j >= 0 and temp < int_list[j]):
int_list[j + 1] = int_list[j] int_list[j + 1] = int_list[j]
j -= 1 j -= 1
int_list[j + 1] = temp int_list[j + 1] = temp
return int_list return int_list
def main(): def main():
try: try:
print("Enter numbers separated by spaces:") print("Enter numbers separated by spaces:")
s = raw_input() s = raw_input()
inputs = list(map(int, s.split(' '))) inputs = list(map(int, s.split(' ')))
if len(inputs) < 2: if len(inputs) < 2:
print('No Enough values to sort!') print('No Enough values to sort!')
raise Exception raise Exception
except Exception as e: except Exception as e:
print(e) print(e)
else: else:
sorted_input = simple_insertion_sort(inputs) sorted_input = simple_insertion_sort(inputs)
print('\nSorted list (min to max): {}'.format(sorted_input)) print('\nSorted list (min to max): {}'.format(sorted_input))
if __name__ == '__main__': if __name__ == '__main__':
print('==== Insertion Sort ====\n') print('==== Insertion Sort ====\n')
main() main()

View File

@ -1,24 +1,24 @@
def sequential_search(alist, target): def sequential_search(alist, target):
for index, item in enumerate(alist): for index, item in enumerate(alist):
if item == target: if item == target:
print("Found target {} at index {}".format(target, index)) print("Found target {} at index {}".format(target, index))
break break
else: else:
print("Not found") print("Not found")
def main(): def main():
try: try:
print("Enter numbers separated by spaces") print("Enter numbers separated by spaces")
s = raw_input() s = raw_input()
inputs = list(map(int, s.split(' '))) inputs = list(map(int, s.split(' ')))
target = int(raw_input('\nEnter a single number to be found in the list: ')) target = int(raw_input('\nEnter a single number to be found in the list: '))
except Exception as e: except Exception as e:
print(e) print(e)
else: else:
sequential_search(inputs, target) sequential_search(inputs, target)
if __name__ == '__main__': if __name__ == '__main__':
print('==== Insertion Sort ====\n') print('==== Linear Search ====\n')
main() main()

View File

@ -1,31 +1,33 @@
def quicksort(A, p, r): def quick_sort(A, p, r):
if p < r: if p < r:
q = partition(A, p, r) q = partition(A, p, r)
quicksort(A, p, q - 1) quick_sort(A, p, q - 1)
quicksort(A, q + 1, r) quick_sort(A, q + 1, r)
return A
def partition(A, p, r): def partition(A, p, r):
x = A[r]
i = p - 1 i = p - 1
for j in range(p, r): for j in range(p, r):
if A[j] <= x: if A[j] <= A[r]:
i += 1 i += 1
tmp = A[i] A[i], A[j] = A[j], A[i]
A[i] = A[j] A[i + 1], A[r] = A[r], A[i + 1]
A[j] = tmp
tmp = A[i+1]
A[i+1] = A[r]
A[r] = tmp
return i + 1 return i + 1
if __name__ == "__main__": def main():
print('Enter values seperated by space:') try:
A = [int (item) for item in input().split(' ')] print("Enter numbers separated by spaces")
# A = [23, 45, 43, 12, 67, 98, 123, 99] s = raw_input()
# partition(A, 0, 7) inputs = list(map(int, s.split(' ')))
print(A) except Exception as e:
quicksort(A, 0, 7) print(e)
print(A) else:
sorted_input = quick_sort(inputs, 0, len(inputs) - 1)
print('\nSorted list (min to max): {}'.format(sorted_input))
if __name__ == '__main__':
print('==== Quick Sort ====\n')
main()

View File

@ -1,14 +1,13 @@
# The Algoritms - Python # The Algoritms - Python
### **All Algorithms implemented in Python!** ### All Algorithms implemented in Python!
These are for demonstration purposes only. There are many implementations of sorts in the Python standard library that are much better for performance reasons. These are for demonstration purposes only. There are many implementations of sorts in the Python standard library that are much better for performance reasons.
## Sorting ## Sorting Algorithms
### Binary ### Binary
Add comments here
### Bubble ### Bubble
![alt text][bubble-image] ![alt text][bubble-image]
@ -16,7 +15,6 @@ These are for demonstration purposes only. There are many implementations of sor
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. 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__ __Properties__
* Stable
* Worst case performance O(n^2) * Worst case performance O(n^2)
* Best case performance O(n) * Best case performance O(n)
* Average case performance O(n^2) * Average case performance O(n^2)
@ -26,7 +24,7 @@ __Properties__
### Caesar ### Caesar
Add comments here
### Insertion ### Insertion
![alt text][insertion-image] ![alt text][insertion-image]
@ -34,7 +32,6 @@ __Properties__
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. 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__ __Properties__
* Stable
* Worst case performance O(n^2) * Worst case performance O(n^2)
* Best case performance O(n) * Best case performance O(n)
* Average case performance O(n^2) * Average case performance O(n^2)
@ -43,6 +40,18 @@ __Properties__
###### View the algorithm in [action][insertion-toptal] ###### View the algorithm in [action][insertion-toptal]
## Quick
![alt text][quick-image]
From [Wikipedia][quick-wiki]: Quicksort (sometimes called partition-exchange sort) is an efficient sorting algorithm, serving as a systematic method for placing the elements of an array in order.
__Properties__
* Worst case performance O(n^2)
* Best case performance O(n log n) or O(n) with three-way partition
* Average case performance O(n^2)
###### View the algorithm in [action][quick-toptal]
[bubble-toptal]: https://www.toptal.com/developers/sorting-algorithms/bubble-sort [bubble-toptal]: https://www.toptal.com/developers/sorting-algorithms/bubble-sort
[bubble-wiki]: https://en.wikipedia.org/wiki/Bubble_sort [bubble-wiki]: https://en.wikipedia.org/wiki/Bubble_sort
@ -51,3 +60,7 @@ __Properties__
[insertion-toptal]: https://www.toptal.com/developers/sorting-algorithms/insertion-sort [insertion-toptal]: https://www.toptal.com/developers/sorting-algorithms/insertion-sort
[insertion-wiki]: https://en.wikipedia.org/wiki/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" [insertion-image]: https://upload.wikimedia.org/wikipedia/commons/7/7e/Insertionsort-edited.png "Insertion Sort"
[quick-toptal]: https://www.toptal.com/developers/sorting-algorithms/quick-sort
[quick-wiki]: https://en.wikipedia.org/wiki/Quicksort
[quick-image]: https://upload.wikimedia.org/wikipedia/commons/6/6a/Sorting_quicksort_anim.gif "Quick Sort"