diff --git a/basic/sorting/SelectionSort/SelectionSort.py b/basic/sorting/SelectionSort/SelectionSort.py new file mode 100644 index 0000000000000..7f6de8703f67c --- /dev/null +++ b/basic/sorting/SelectionSort/SelectionSort.py @@ -0,0 +1,13 @@ +def selectionSort(arr): + n = len(arr) + for i in range(n - 1): + min_index = i + for j in range(i + 1, n): + if arr[j] < arr[min_index]: + min_index = j + arr[min_index], arr[i] = arr[i], arr[min_index] + + +arr = [26, 11, 99, 33, 69, 77, 55, 56, 67] +selectionSort(arr) +print(arr) diff --git a/basic/sorting/ShellSort/ShellSort.py b/basic/sorting/ShellSort/ShellSort.py new file mode 100644 index 0000000000000..95c9751605449 --- /dev/null +++ b/basic/sorting/ShellSort/ShellSort.py @@ -0,0 +1,17 @@ +def shellSort(arr): + n = len(arr) + gap = int(n / 2) + while gap > 0: + for i in range(gap, n): + temp = arr[i] + j = i + while j >= gap and arr[j - gap] > temp: + arr[j] = arr[j - gap] + j -= gap + arr[j] = temp + gap = int(gap / 2) + + +arr = [12, 34, 54, 2, 3] +shellSort(arr) +print(arr)