C Insertion Sort로 오름차순 정렬하는 방식입니다.


시간복잡도 : O(n^2)


#include <stdio.h>


void printArray(int value[], int count) {

int i = 0;

for (i = 0; i < count; i++) {

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

}

printf("\n");

}


void insertionSort(int value[], int count) {

int i = 0, j = 0, temp = 0;

for (i = 1; i < count; i++) {

temp = value[i];

j = i;

while ((j > 0) && value[j - 1] > temp) {

value[j] = value[j - 1];

j = j - 1;

}

value[j] = temp;

printf("Step-%d,", i);

printArray(value, count);

}

}

int main(int argc, char *argv[]) {

int values[] = { 80, 50, 70, 10, 60, 20, 40, 30 };

printf("Before InsertionSort\n");

printArray(values, 8);


insertionSort(values, 8);


printf("\nAfter Sort\n");

printArray(values, 8);


return 0;

}

'Programming > C' 카테고리의 다른 글

Sorting(4) Quick Sort(퀵 정렬)  (0) 2016.11.01
Sorting(3) Selection Sort(선택 정렬)  (0) 2016.11.01
Sorting(1) Bubble Sort(버블 정렬)  (0) 2016.11.01
(6) CircularList 사용하기  (0) 2016.10.30
(5) DoubleLinkedList 사용하기  (0) 2016.10.30

WRITTEN BY
SiriusJ

,