<div>#include <stdio.h><br>#include <string.h><br>#include <stdlib.h><br><br>void output_sort(int * list, int n)<br>{<br>int i = 0;<br>int j = 0;<br>int temp = 0;<br>int index = 0;<br><br>for ( i = 0; i < n; i++ ) <br>{ <br>index = i; <br>for ( j = i; j < n; j++ ) <br>{ <br>if ( list[index] > list[j] ) <br>{ <br>index = j; <br>} <br>} <br>temp = list[index]; <br>list[index] = list[i]; <br>list[i] = temp; <br>} <br><br>}<br><br><br>void shell_sort(int *list, int n, int mode) //n = size <br>{ <br><br>int i, j, k;<br>// 분할된 배열에서 시작지점 <br>int start;<br>int next;<br>int gap = n;<br>int data[20];<br>int count = 0;<br><br>char path[20] = "hw1.out"; <br><br>FILE *files;<br>if (mode == 0)<br>{<br>files = fopen(path, "w+") ;<br>}<br>else<br>files = fopen(path, "a+") ;<br><br>fflush(files);<br><br>if (files == NULL)<br>{<br>printf(" FILE OPEN ERROR! \n");<br>return;<br>}<br><br>do <br>{<br>gap /= 2;<br><br>for (start=0; start<gap; start++) <br>{<br>count = 0;<br>// 삽입정렬 알고리즘<br>for (i=start; i<n; i+=gap) <br>{<br>next = list[i];<br><br><br>for(j=i-gap; j>=0 && list[j] > next; j-=gap) <br>{<br>list[j+gap] = list[j];<br>}<br>data[count++] = next;<br><br>list[j+gap] = next;<br><br>}<br><br>output_sort(data, count);<br><br><br>for (k = 0; k < count; k++)<br>{<br>fprintf(files, "%d ", data[k]);<br>printf("%d ",data[k]);<br>}<br><br>fprintf(files, "\n");<br>printf("\n");<br>}<br><br>} while (gap > 1);<br><br>fclose(files);<br><br>} <br><br>void shell_add(int *list, int n, int *list2, int n2, int * output) //n = size <br>{ <br>int i = 0;<br>int j = 0;<br>int count = 0;<br><br>char path[20] = "hw1.out";<br><br>FILE *files;<br>files = fopen(path, "a+") ;<br><br>fflush(files);<br><br>if (files == NULL)<br>{<br>printf(" FILE OPEN ERROR! \n");<br>return;<br>}<br><br>for(i = 0 ;i < n ; i++)<br>{<br>if (list[i] < list2[j])<br>{<br>output[count] = list[i];<br>count++;<br>}<br>else<br>{<br>output[count] = list2[j];<br>count++;<br>j++;<br>i--;<br>}<br><br>if (j >= n2)<br>{<br>break;<br>}<br>}<br><br>if (i < n)<br>{<br>for ( ; i < n; i++)<br>{<br>output[count] = list[i];<br>count++;<br>}<br>}<br><br>if (j < n2)<br>{<br>for ( ; j < n2; j++)<br>{<br>output[count] = list2[j];<br>count++;<br>}<br>}<br><br>for (i = 0; i < count; i++)<br>{<br>printf("%d ", output[i]);<br>fprintf(files, "%d ", output[i]);<br>}<br>printf("\n");<br>fprintf(files, "\n");<br><br>printf("*\n");<br>fprintf(files, "*\n");<br><br>fclose(files);<br>} <br><br><br>int main()<br>{ <br>char path[20] = "hw1.inp"; <br>int data[2][20] = {0};<br>int LineCount[2] = { 0};<br>int output[40] = {0};<br>char *save;<br>char *s;<br>char buf[512];<br>int count = 0;<br>int h_count = 0;<br>char delim[] = " \t\n"; /* 문자열을 나누는 기준 (공백, 텝, 엔트) */<br>int i = 0;<br>int j = 0;<br>char tmp_set;<br><br>FILE *files;<br>files = fopen(path, "r+") ;<br><br>if (files == NULL)<br>{<br>printf(" FILE OPEN ERROR! \n");<br>return 0;<br>}<br><br>for (i = 0; i < 2; i++)<br>{<br>for (j = 0; j < 20; j++)<br>{<br><br>fscanf(files, "%d", &data[i][j]);<br>fscanf(files, "%c", &tmp_set);<br><br>if (tmp_set == '\n')<br>break;<br>if (tmp_set == '*')<br>break;<br>}<br><br>LineCount[i] =j+1;<br>}<br><br><br><br>for (i = 0; i < 2 ;i++)<br>shell_sort(data[i], LineCount[i], i);<br><br>shell_add(data[0], LineCount[0], data[1], LineCount[1], output);<br><br>fclose(files); <br><br>return 0;<br>}<br>-- 소스는 다음과 같고</div> <div>hw1.inp </div> <div>1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 </div> <div>*</div> <div>일때.</div> <div> </div> <div>10 20</div> <div>9 19</div> <div>8 18</div> <div>7 17</div> <div>6 16</div> <div>5 15</div> <div>4 14</div> <div>3 13</div> <div>2 12</div> <div>1 11</div> <div>5 10 15 20</div> <div>4 9 14 19</div> <div>3 8 13 18</div> <div>2 7 12 17</div> <div>1 6 11 16</div> <div>1 3 5 7 9 11 13 15 17 19</div> <div>2 4 6 8 10 12 14 16 18 20</div> <div>1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20</div> <div>0 -858993460 1 ~ 20</div> <div>*</div> <div> </div> <div>이렇게 나옵니다. 뭐가 문제인지 짚어주시면 감사하겠습니다.</div>
댓글 분란 또는 분쟁 때문에 전체 댓글이 블라인드 처리되었습니다.