diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index e27496b3..012743a2 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -1,30 +1,43 @@ # TO-DO: Complete the selection_sort() function below def selection_sort( arr ): - # loop through n-1 elements - for i in range(0, len(arr) - 1): - cur_index = i - smallest_index = cur_index - # TO-DO: find next smallest element - # (hint, can do in 3 loc) - - - - - # TO-DO: swap - - - - + # loop through all elements + for i in range(len(arr)): + # flag to see if item i has been inserted in sorted array + replaced = False + # loop through the already sorted part of the list + for j in range(i+1): + # if item i has already been inserted + if replaced: + # move items to the right + temp = arr[i] + arr[i] = arr[j] + arr[j] = temp + elif arr[j] > arr[i]: + # if this is where the item should be inserted + # then insert it and save item to arr[i] + temp = arr[j] + arr[j] = arr[i] + arr[i] = temp + replaced = True return arr - # TO-DO: implement the Bubble Sort function below def bubble_sort( arr ): + changes = True + while changes: + changes = False + for i in range(len(arr)-1): + j = i+1 + if arr[i] > arr[j]: + temp = arr[i] + arr[i] = arr[j] + arr[j] = temp + changes = True return arr # STRETCH: implement the Count Sort function below -def count_sort( arr, maximum=-1 ): +# def count_sort( arr, maximum=-1 ): - return arr \ No newline at end of file +# return arr \ No newline at end of file