자이의 프로그래밍
삽입정렬의 구현 본문
#include <iostream>
using namespace std;
int main() {
// i
// 1 2 4 8 | 5 8 4 3 2
// 1 2 4 5 8 | 8 4 3 2
//i가 가리키고 있는 값이 왼쪽으로 들어가야 함
//단, i 왼쪽의 모든 원소는 정렬이 되어 있음
int n, data[100];
cin>>n;
for(int i=0; i<n; i++)
cin>>data[i];
for(int i=0; i<n; i++)
{
//i가 가리키고 있는 값을 넣어야 한다
//단 i 왼쪽은 모두 정렬되어있다
for(int j=i; j>=1; j--)
{
if(data[j-1]>data[j]){
int temp;
temp=data[j-1];
data[j-1]=data[j];
data[j]=temp;
}
else break;
}
for(int j=0; j<n; j++)
{
cout<<data[j];
}
cout<<endl;
}
return 0;
}