forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Converted all indentations to spaces (different files had spaces or tabs)
- Loading branch information
Tony Sappe
committed
Jul 29, 2016
1 parent
b7eae6b
commit 37ddd2c
Showing
5 changed files
with
104 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,32 @@ | ||
|
||
|
||
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 | ||
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(): | ||
try: | ||
print("Enter numbers separated by spaces:") | ||
s = raw_input() | ||
inputs = list(map(int, s.split(' '))) | ||
if len(inputs) < 2: | ||
print('No Enough values to sort!') | ||
raise Exception | ||
try: | ||
print("Enter numbers separated by spaces:") | ||
s = raw_input() | ||
inputs = list(map(int, s.split(' '))) | ||
if len(inputs) < 2: | ||
print('No Enough values to sort!') | ||
raise Exception | ||
|
||
except Exception as e: | ||
print(e) | ||
else: | ||
sorted_input = simple_bubble_sort(inputs) | ||
print('\nSorted list (min to max): {}'.format(sorted_input)) | ||
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') | ||
main() | ||
print('==== Bubble Sort ====\n') | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,32 @@ | ||
|
||
def simple_insertion_sort(int_list): | ||
count = len(int_list) | ||
for i in range(1, count): | ||
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 | ||
count = len(int_list) | ||
for i in range(1, count): | ||
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 | ||
return int_list | ||
|
||
|
||
def main(): | ||
try: | ||
print("Enter numbers separated by spaces:") | ||
s = raw_input() | ||
inputs = list(map(int, s.split(' '))) | ||
if len(inputs) < 2: | ||
print('No Enough values to sort!') | ||
raise Exception | ||
try: | ||
print("Enter numbers separated by spaces:") | ||
s = raw_input() | ||
inputs = list(map(int, s.split(' '))) | ||
if len(inputs) < 2: | ||
print('No Enough values to sort!') | ||
raise Exception | ||
|
||
except Exception as e: | ||
print(e) | ||
else: | ||
sorted_input = simple_insertion_sort(inputs) | ||
print('\nSorted list (min to max): {}'.format(sorted_input)) | ||
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') | ||
main() | ||
print('==== Insertion Sort ====\n') | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
|
||
def sequential_search(alist, target): | ||
for index, item in enumerate(alist): | ||
if item == target: | ||
print("Found target {} at index {}".format(target, index)) | ||
break | ||
else: | ||
print("Not found") | ||
for index, item in enumerate(alist): | ||
if item == target: | ||
print("Found target {} at index {}".format(target, index)) | ||
break | ||
else: | ||
print("Not found") | ||
|
||
|
||
def main(): | ||
try: | ||
print("Enter numbers separated by spaces") | ||
s = raw_input() | ||
inputs = list(map(int, s.split(' '))) | ||
target = int(raw_input('\nEnter a single number to be found in the list: ')) | ||
except Exception as e: | ||
print(e) | ||
else: | ||
sequential_search(inputs, target) | ||
try: | ||
print("Enter numbers separated by spaces") | ||
s = raw_input() | ||
inputs = list(map(int, s.split(' '))) | ||
target = int(raw_input('\nEnter a single number to be found in the list: ')) | ||
except Exception as e: | ||
print(e) | ||
else: | ||
sequential_search(inputs, target) | ||
|
||
if __name__ == '__main__': | ||
print('==== Insertion Sort ====\n') | ||
main() | ||
print('==== Linear Search ====\n') | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,33 @@ | ||
|
||
def quicksort(A, p, r): | ||
def quick_sort(A, p, r): | ||
if p < r: | ||
q = partition(A, p, r) | ||
quicksort(A, p, q - 1) | ||
quicksort(A, q + 1, r) | ||
quick_sort(A, p, q - 1) | ||
quick_sort(A, q + 1, r) | ||
return A | ||
|
||
|
||
def partition(A, p, r): | ||
x = A[r] | ||
i = p - 1 | ||
for j in range(p, r): | ||
if A[j] <= x: | ||
if A[j] <= A[r]: | ||
i += 1 | ||
tmp = A[i] | ||
A[i] = A[j] | ||
A[j] = tmp | ||
tmp = A[i+1] | ||
A[i+1] = A[r] | ||
A[r] = tmp | ||
A[i], A[j] = A[j], A[i] | ||
A[i + 1], A[r] = A[r], A[i + 1] | ||
return i + 1 | ||
|
||
|
||
if __name__ == "__main__": | ||
print('Enter values seperated by space:') | ||
A = [int (item) for item in input().split(' ')] | ||
# A = [23, 45, 43, 12, 67, 98, 123, 99] | ||
# partition(A, 0, 7) | ||
print(A) | ||
quicksort(A, 0, 7) | ||
print(A) | ||
def main(): | ||
try: | ||
print("Enter numbers separated by spaces") | ||
s = raw_input() | ||
inputs = list(map(int, s.split(' '))) | ||
except Exception as e: | ||
print(e) | ||
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters