From e876bbb6c54d1e838cea684520c163e13b44b453 Mon Sep 17 00:00:00 2001 From: PercivalN Date: Mon, 13 Apr 2020 18:15:36 -0500 Subject: [PATCH 1/3] Initial commit --- src/iterative_sorting/iterative_sorting.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index e27496b3..9e60473c 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -20,7 +20,11 @@ def selection_sort( arr ): # TO-DO: implement the Bubble Sort function below def bubble_sort( arr ): - + # Start with the first 2 numbers + # If the first number is smaller then the second then go onto the next index + # Else swap places + # then go onto the next 2 numbers + return arr From e5be1df4f7e735e34c4b20f3e975f4c3c3c2bc3a Mon Sep 17 00:00:00 2001 From: PercivalN Date: Mon, 13 Apr 2020 23:11:03 -0500 Subject: [PATCH 2/3] Finished bubble sort --- src/.vscode/launch.json | 16 ++++++++++++++++ src/iterative_sorting/iterative_sorting.py | 17 +++++++++++++---- 2 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 src/.vscode/launch.json diff --git a/src/.vscode/launch.json b/src/.vscode/launch.json new file mode 100644 index 00000000..5dba2172 --- /dev/null +++ b/src/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + + { + "name": "Python: Current File", + "type": "python", + "request": "launch", + "program": "${file}", + "console": "integratedTerminal" + } + ] +} \ No newline at end of file diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index 9e60473c..88abf706 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -17,16 +17,25 @@ def selection_sort( arr ): return arr +input_array = [2, 54, 11, 1, 145, 8] # TO-DO: implement the Bubble Sort function below def bubble_sort( arr ): - # Start with the first 2 numbers - # If the first number is smaller then the second then go onto the next index - # Else swap places - # then go onto the next 2 numbers + for i in range(0, len(arr)): + for index in range(1, len(arr)): + current = arr[index] + previous = arr[index - 1] + + if previous <= current: + continue + else: + arr[index] = previous + arr[index - 1] = current return arr +print(bubble_sort(input_array)) + # STRETCH: implement the Count Sort function below def count_sort( arr, maximum=-1 ): From 0ea3d595ba001ca43b3654d28ce4def16a1bce5f Mon Sep 17 00:00:00 2001 From: PercivalN Date: Tue, 14 Apr 2020 03:32:47 -0500 Subject: [PATCH 3/3] Finished selection sort --- src/iterative_sorting/iterative_sorting.py | 55 ++++++++++++---------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index 88abf706..fe7f518f 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -1,43 +1,50 @@ # TO-DO: Complete the selection_sort() function below + +input_array = [2, 54, 11, 1, 145, 8] + def selection_sort( arr ): # loop through n-1 elements for i in range(0, len(arr) - 1): cur_index = i smallest_index = cur_index + for index in range(i, len(arr)): # TO-DO: find next smallest element # (hint, can do in 3 loc) - - - - + print(arr[index]) + if arr[index] <= arr[smallest_index]: + smallest_index = index + + # TO-DO: swap - - - + temp = arr[smallest_index] + arr[smallest_index] = arr[i] + arr[i] = temp + print(arr[i]) return arr -input_array = [2, 54, 11, 1, 145, 8] +print(selection_sort(input_array)) +# input_array = [2, 54, 11, 1, 145, 8] -# TO-DO: implement the Bubble Sort function below -def bubble_sort( arr ): - for i in range(0, len(arr)): - for index in range(1, len(arr)): - current = arr[index] - previous = arr[index - 1] +# # TO-DO: implement the Bubble Sort function below +# def bubble_sort( arr ): +# for i in range(0, len(arr)): +# for index in range(1, len(arr)): +# current = arr[index] +# previous = arr[index - 1] - if previous <= current: - continue - else: - arr[index] = previous - arr[index - 1] = current +# if previous <= current: +# continue +# else: +# arr[index] = previous +# arr[index - 1] = current - return arr +# return arr -print(bubble_sort(input_array)) +# print(bubble_sort(input_array)) -# STRETCH: implement the Count Sort function below -def count_sort( arr, maximum=-1 ): +# # STRETCH: implement the Count Sort function below +# def count_sort( arr, maximum=-1 ): - return arr \ No newline at end of file +# return arr \ No newline at end of file