Computer Science/Data Structure, Algorithm

Algorithm] Insertion Sort(삽입정렬)

TwinParadox 2015. 7. 11. 12:58
728x90

Insertion Sort(삽입정렬)


#include <stdio.h>


void InsertSort(int arr[], int n)

{

int i, j;

int ins_data;


for (i = 1; i < n; i++)

{

ins_data = arr[i];

for (j = i - 1; j >= 0; j--)

{

if (arr[j]>ins_data)

arr[j + 1] = arr[j];

else

break;

}


arr[j + 1] = ins_data;

}

}

void main()

{

int arr[5] = { 5, 3, 2, 4, 1 };

int i;


InsertSort(arr, sizeof(arr) / sizeof(int));


for (i = 0; i < 5; i++)

printf("%d ", arr[i]);


printf("\n");

}

728x90