Skip to content

Commit 37ddd2c

Browse files
author
Tony Sappe
committed
Changed QuickSort.py
Converted all indentations to spaces (different files had spaces or tabs)
1 parent b7eae6b commit 37ddd2c

File tree

5 files changed

+104
-89
lines changed

5 files changed

+104
-89
lines changed

BubbleSort.py

+23-23
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11

22

33
def simple_bubble_sort(int_list):
4-
count = len(int_list)
5-
swapped = True
6-
while (swapped):
7-
swapped = False
8-
for j in range(count - 1):
9-
if (int_list[j] > int_list[j + 1]):
10-
int_list[j], int_list[j + 1] = int_list[j + 1], int_list[j]
11-
swapped = True
12-
return int_list
4+
count = len(int_list)
5+
swapped = True
6+
while (swapped):
7+
swapped = False
8+
for j in range(count - 1):
9+
if (int_list[j] > int_list[j + 1]):
10+
int_list[j], int_list[j + 1] = int_list[j + 1], int_list[j]
11+
swapped = True
12+
return int_list
1313

1414

1515
def main():
16-
try:
17-
print("Enter numbers separated by spaces:")
18-
s = raw_input()
19-
inputs = list(map(int, s.split(' ')))
20-
if len(inputs) < 2:
21-
print('No Enough values to sort!')
22-
raise Exception
16+
try:
17+
print("Enter numbers separated by spaces:")
18+
s = raw_input()
19+
inputs = list(map(int, s.split(' ')))
20+
if len(inputs) < 2:
21+
print('No Enough values to sort!')
22+
raise Exception
2323

24-
except Exception as e:
25-
print(e)
26-
else:
27-
sorted_input = simple_bubble_sort(inputs)
28-
print('\nSorted list (min to max): {}'.format(sorted_input))
24+
except Exception as e:
25+
print(e)
26+
else:
27+
sorted_input = simple_bubble_sort(inputs)
28+
print('\nSorted list (min to max): {}'.format(sorted_input))
2929

3030
if __name__ == '__main__':
31-
print('==== Bubble Sort ====\n')
32-
main()
31+
print('==== Bubble Sort ====\n')
32+
main()

InsertionSort.py

+23-23
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11

22
def simple_insertion_sort(int_list):
3-
count = len(int_list)
4-
for i in range(1, count):
5-
temp = int_list[i]
6-
j = i - 1
7-
while(j >= 0 and temp < int_list[j]):
8-
int_list[j + 1] = int_list[j]
9-
j -= 1
10-
int_list[j + 1] = temp
3+
count = len(int_list)
4+
for i in range(1, count):
5+
temp = int_list[i]
6+
j = i - 1
7+
while(j >= 0 and temp < int_list[j]):
8+
int_list[j + 1] = int_list[j]
9+
j -= 1
10+
int_list[j + 1] = temp
1111

12-
return int_list
12+
return int_list
1313

1414

1515
def main():
16-
try:
17-
print("Enter numbers separated by spaces:")
18-
s = raw_input()
19-
inputs = list(map(int, s.split(' ')))
20-
if len(inputs) < 2:
21-
print('No Enough values to sort!')
22-
raise Exception
16+
try:
17+
print("Enter numbers separated by spaces:")
18+
s = raw_input()
19+
inputs = list(map(int, s.split(' ')))
20+
if len(inputs) < 2:
21+
print('No Enough values to sort!')
22+
raise Exception
2323

24-
except Exception as e:
25-
print(e)
26-
else:
27-
sorted_input = simple_insertion_sort(inputs)
28-
print('\nSorted list (min to max): {}'.format(sorted_input))
24+
except Exception as e:
25+
print(e)
26+
else:
27+
sorted_input = simple_insertion_sort(inputs)
28+
print('\nSorted list (min to max): {}'.format(sorted_input))
2929

3030
if __name__ == '__main__':
31-
print('==== Insertion Sort ====\n')
32-
main()
31+
print('==== Insertion Sort ====\n')
32+
main()

LinearSearch.py

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11

22
def sequential_search(alist, target):
3-
for index, item in enumerate(alist):
4-
if item == target:
5-
print("Found target {} at index {}".format(target, index))
6-
break
7-
else:
8-
print("Not found")
3+
for index, item in enumerate(alist):
4+
if item == target:
5+
print("Found target {} at index {}".format(target, index))
6+
break
7+
else:
8+
print("Not found")
99

1010

1111
def main():
12-
try:
13-
print("Enter numbers separated by spaces")
14-
s = raw_input()
15-
inputs = list(map(int, s.split(' ')))
16-
target = int(raw_input('\nEnter a single number to be found in the list: '))
17-
except Exception as e:
18-
print(e)
19-
else:
20-
sequential_search(inputs, target)
12+
try:
13+
print("Enter numbers separated by spaces")
14+
s = raw_input()
15+
inputs = list(map(int, s.split(' ')))
16+
target = int(raw_input('\nEnter a single number to be found in the list: '))
17+
except Exception as e:
18+
print(e)
19+
else:
20+
sequential_search(inputs, target)
2121

2222
if __name__ == '__main__':
23-
print('==== Insertion Sort ====\n')
24-
main()
23+
print('==== Linear Search ====\n')
24+
main()

QuickSort.py

+21-19
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
11

2-
def quicksort(A, p, r):
2+
def quick_sort(A, p, r):
33
if p < r:
44
q = partition(A, p, r)
5-
quicksort(A, p, q - 1)
6-
quicksort(A, q + 1, r)
5+
quick_sort(A, p, q - 1)
6+
quick_sort(A, q + 1, r)
7+
return A
78

89

910
def partition(A, p, r):
10-
x = A[r]
1111
i = p - 1
1212
for j in range(p, r):
13-
if A[j] <= x:
13+
if A[j] <= A[r]:
1414
i += 1
15-
tmp = A[i]
16-
A[i] = A[j]
17-
A[j] = tmp
18-
tmp = A[i+1]
19-
A[i+1] = A[r]
20-
A[r] = tmp
15+
A[i], A[j] = A[j], A[i]
16+
A[i + 1], A[r] = A[r], A[i + 1]
2117
return i + 1
2218

2319

24-
if __name__ == "__main__":
25-
print('Enter values seperated by space:')
26-
A = [int (item) for item in input().split(' ')]
27-
# A = [23, 45, 43, 12, 67, 98, 123, 99]
28-
# partition(A, 0, 7)
29-
print(A)
30-
quicksort(A, 0, 7)
31-
print(A)
20+
def main():
21+
try:
22+
print("Enter numbers separated by spaces")
23+
s = raw_input()
24+
inputs = list(map(int, s.split(' ')))
25+
except Exception as e:
26+
print(e)
27+
else:
28+
sorted_input = quick_sort(inputs, 0, len(inputs) - 1)
29+
print('\nSorted list (min to max): {}'.format(sorted_input))
30+
31+
if __name__ == '__main__':
32+
print('==== Quick Sort ====\n')
33+
main()

README.md

+20-7
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
# The Algoritms - Python
22

3-
### **All Algorithms implemented in Python!**
3+
### All Algorithms implemented in Python!
44

55
These are for demonstration purposes only. There are many implementations of sorts in the Python standard library that are much better for performance reasons.
66

7-
## Sorting
8-
7+
## Sorting Algorithms
98

109
### Binary
11-
10+
Add comments here
1211

1312
### Bubble
1413
![alt text][bubble-image]
1514

1615
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.
1716

1817
__Properties__
19-
* Stable
2018
* Worst case performance O(n^2)
2119
* Best case performance O(n)
2220
* Average case performance O(n^2)
@@ -26,15 +24,14 @@ __Properties__
2624

2725

2826
### Caesar
29-
27+
Add comments here
3028

3129
### Insertion
3230
![alt text][insertion-image]
3331

3432
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.
3533

3634
__Properties__
37-
* Stable
3835
* Worst case performance O(n^2)
3936
* Best case performance O(n)
4037
* Average case performance O(n^2)
@@ -43,6 +40,18 @@ __Properties__
4340
###### View the algorithm in [action][insertion-toptal]
4441

4542

43+
## Quick
44+
![alt text][quick-image]
45+
46+
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.
47+
48+
__Properties__
49+
* Worst case performance O(n^2)
50+
* Best case performance O(n log n) or O(n) with three-way partition
51+
* Average case performance O(n^2)
52+
53+
54+
###### View the algorithm in [action][quick-toptal]
4655

4756
[bubble-toptal]: https://www.toptal.com/developers/sorting-algorithms/bubble-sort
4857
[bubble-wiki]: https://en.wikipedia.org/wiki/Bubble_sort
@@ -51,3 +60,7 @@ __Properties__
5160
[insertion-toptal]: https://www.toptal.com/developers/sorting-algorithms/insertion-sort
5261
[insertion-wiki]: https://en.wikipedia.org/wiki/Insertion_sort
5362
[insertion-image]: https://upload.wikimedia.org/wikipedia/commons/7/7e/Insertionsort-edited.png "Insertion Sort"
63+
64+
[quick-toptal]: https://www.toptal.com/developers/sorting-algorithms/quick-sort
65+
[quick-wiki]: https://en.wikipedia.org/wiki/Quicksort
66+
[quick-image]: https://upload.wikimedia.org/wikipedia/commons/6/6a/Sorting_quicksort_anim.gif "Quick Sort"

0 commit comments

Comments
 (0)