fork download
  1. #include <stdio.h>
  2.  
  3. typedef int element;
  4.  
  5. void insertionSort(element a[], int size) {
  6. int i, j, t;
  7. element temp;
  8.  
  9. printf("\n정렬할 원소 : ");
  10. for (t = 0; t < size; t++) {
  11. printf("%d ", a[t]);
  12. }
  13. printf("\n\n< 삽입 정렬 수행 >\n");
  14.  
  15. for (i = 1; i < size; i++) {
  16. temp = a[i];
  17. j = i;
  18. while (j > 0 && a[j - 1] > temp) {
  19. a[j] = a[j - 1];
  20. j--;
  21. }
  22. a[j] = temp;
  23.  
  24. printf("\n %d단계 : ", i);
  25. for (t = 0; t < size; t++) {
  26. printf("%3d ", a[t]);
  27. }
  28. }
  29. printf("\n");
  30. }
  31.  
  32. int main(void) {
  33. int a[] = {69, 10, 30, 2, 16, 8, 31, 22};
  34. int size = sizeof(a) / sizeof(a[0]);
  35. insertionSort(a, size);
  36. return 0;
  37. }
  38.  
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
정렬할 원소 : 69 10 30 2 16 8 31 22 

< 삽입 정렬 수행 >

 1단계 :  10  69  30   2  16   8  31  22 
 2단계 :  10  30  69   2  16   8  31  22 
 3단계 :   2  10  30  69  16   8  31  22 
 4단계 :   2  10  16  30  69   8  31  22 
 5단계 :   2   8  10  16  30  69  31  22 
 6단계 :   2   8  10  16  30  31  69  22 
 7단계 :   2   8  10  16  22  30  31  69