| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 |
- 티스토리챌린지
- raw data
- Pre-processing
- assignment operator
- Data Science
- Python
- OOP
- Object Oriented Programming
- 파이썬
- vscode
- 백준
- 배열
- 문자열
- baekjoon
- predictive analysis
- programming
- const
- 함수
- 반복문
- 알고리즘
- function
- pass by reference
- pointer
- array
- Deep Learning
- Class
- 오블완
- 포인터
- string
- C++
- Today
- Total
Channi Studies
[Sorting] Shell Sort 본문
In the final post, we looked into the selection sorting algorithm.
https://code-studies.tistory.com/124
[Sorting] Insertion Sort
In the final post, we have studied about the selection sort and its implementation in Python. https://code-studies.tistory.com/123 [Sorting] Introduction & Selection SortSorting Sorting is the process of converting a list of elements into ascending (
code-studies.tistory.com
In this post, we will check out shell sort.
Shell Sort
Shell sort is a sorting algorithm that treats the input as a collection of interleaved lists, and sorts each list individually with a variant of the insertion sort algorithm.
Shell sort uses gap values to determine the number of interleaved lists.
A gap value is a positive integer representing the distance between elements in an interleaved list.
For each interleaved list, if an element is at index [i], the next element is at index [i + gap value].
Shell sort begins by choosing a gap value K and sorting K interleaved lists in place.
Shell sort finishes by performing a standard insertion sort on the entire array.
Because the interleaved parts have already been sorted, smaller elements will be close to the array's beginning and larger elements towards the end.
Insertion sort can then quickly sort the nearly-sorted array.
Any positive integer gap value can be chosen.
In the case that the array size is not evenly divisible by the gap value, some interleaved lists will have fewer items than others.
https://www.youtube.com/watch?v=qzXAVXddcPU&ab_channel=RunTimeClips
This video on YouTube visually demonstrates Shell Sort effectively.
As you can assume at this point, shell sorting ends up with the gap value K being 1.
Shell sort tends to work better when we choose descending K value.
A common option is to choose powers of 2 minus 1. (= 2 ^ t - 1)
Ex. If N = 100, K = 63, 31, 15, 7, 3, and 1
This gap selection technique results in shell sort's time complexity being no worse than O(N^3/2).
Python Implementation
def insertion_sort_interleaved(arr: list[int], startIndex: int, gap: int) -> None:
for i in range(startIndex + gap, len(arr), gap):
j = i
while (j - gap >= startIndex) and arr[j] < arr[j-gap]:
arr[j-gap], arr[j] = arr[j], arr[j-gap]
j -= gap
def shell_sort(arr: list[int], gapValues: list[int]) -> None:
for gap in gapValues:
for startIndex in range(0, len(arr)):
insertion_sort_interleaved(arr, startIndex, gap)
Implementation of shell sort into code is variation of insertion sort with different gap values.
Compare the similarities of the code of shell sort and insertion sort to get a better understanding.
'Data Science > Data Structure & Algorithm' 카테고리의 다른 글
| [Sorting] Merge Sort (0) | 2025.03.18 |
|---|---|
| [Sorting] Quick Sort (0) | 2025.03.17 |
| [Sorting] Insertion Sort (0) | 2025.03.17 |
| [Sorting] Introduction & Selection Sort (0) | 2025.03.16 |
| [Algorithm] Big-O Notation | 빅 오 표기법 (0) | 2025.03.12 |