Notice
Recent Posts
Recent Comments
Link
빙수달 게임 개발 노트
[알고리즘] 단순 삽입 정렬(Straight Insertion Sort) 본문
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
const int num = 5;
int arr[num] = { 0 };
for (int i = 0; i < num; i++) // 배열 요소 입력
{
cin >> arr[i];
}
for (int i = 1; i < num; i++)
{
int temp = arr[i];
for (int j = i-1; j >= 0; j--)
{
if (temp > arr[j] )
{
break;
}
else
{
arr[j+1] = arr[j]; // 값복사
arr[j] = temp;
}
}
}
for (int i = 0; i < num; i++) // 배열 요소 출력
{
cout << arr[i] << " ";
}
cout << endl;
return 0;
}

'Programming > 알고리즘' 카테고리의 다른 글
| [C++] 단일 링크드 리스트(Single Linked List)로 학생 관리 프로그램 만들기 (1) | 2025.01.08 |
|---|---|
| [알고리즘] 브루트-포스(Brute Force) (0) | 2025.01.08 |
| [알고리즘] 단순 선택 정렬(Straight Selection Sort) (0) | 2025.01.08 |
| [알고리즘] Stack 구현하기 (1) | 2025.01.08 |
| [알고리즘] 버블 정렬(Bubble Sort) (0) | 2024.12.19 |