Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
hw4nx02
STUDY-Algorithm
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
STUDY-Algorithm
/
shellSort.c
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
44 lines (37 loc) · 1.81 KB
master
Breadcrumbs
STUDY-Algorithm
/
shellSort.c
Copy path
Top
File metadata and controls
Code
Blame
44 lines (37 loc) · 1.81 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
/**
* 쉘 정렬
*
* 배열을 부분 배열로 나눔
* -> 이때, 부분 배열은 각 부분 배열의 첫 인덱스 원소부터 h만큼의 간격으로 원소들을 선택해서 구성
* -> 각 부분 배열별로 삽입 정렬을 수행
* -> 간격 h를 줄여 가면서 위와 같은 정렬을 반복 수행
*/
int solution(int a[], int n) {
int result = 0;
int h = 1; // 부분 배열 간 간격. 부분 배열은 h만큼의 간격에 있는 원소들을 원소로 가진다.
int value = 0; // 자리 찾을 값 (삽입할 값)
int i, j = 0; // i: 자리 찾을 값 인덱스, j: 현재 위치
do {
h = 3*h + 1; // n보다 작은 최대 간격 찾기
} while (h < n); // n을 갓 넘은 h값
do {
h = h / 3; // 부분 배열의 간격 줄여가면서 정렬 수행. 최종적으로는 간격이 1인 부분 배열을 결과로 산출하게 됨.
for (i = h; i < n; i++) { // h 만큼을 띄워서 초깃값 설정. 이를 통해 h 간격의 원소들끼리 비교할 수 있게됨
value = a[i]; // 자리를 찾을 값
j = i; // 현재 자리
while (a[j-h] > value) { // 자리를 찾을 값과 h 전의 값을 비교해서 순서가 안 맞다면 정렬
a[j] = a[j-h]; //
j -= h; // 이를 통해 간격 h마다 대응되는 원소끼리 비교할 수 있게 됨
if (j < h-1) break; // 리스트의 하한치를 넘어가서 j가 설정되면 탈출
}
a[j] = value; // 정해진 위치에 자리를 찾는 값을 대입
}
} while (h > 1); // 간격이 1보다는 클 때까지 수행
return result;
}
void main() {
int a[] = {30, 50, 15, 60, 35, 10, 40, 45, 5, 25, 55, 20};
int n = 12;
int sol = solution(a, n);
}
You can’t perform that action at this time.