문자열의 삽입정렬과 선택정렬 오류

prupru의 이미지

void selection_sort(char SWL[100][100], int n) { //n은 단어개수
int i, j, least;
char temp[100];

for (i = 0; i < n - 1; i++)
{
least = i;
for (j = i + 1; j < n; j++)
{
if (SWL[j][0] < SWL[least][0])
least = j;

else if (SWL[j][0] == SWL[least][0])
{
int len1 = strlen(SWL[j]);
int len2 = strlen(SWL[least]);
int short_l = 0;
if (len1 < len2)
short_l = len1;
else
short_l = len2;

for (int k = 1; k < short_l; k++)
{
if (SWL[j][k] < SWL[least][k])
{
least = j;
break;
}
}
}

strcpy(temp, SWL[i]);
strcpy(SWL[i], SWL[least]);
strcpy(SWL[least], temp);

}
}

}

void insertion_sort(char SWL[100][100], int n) {
int i, j;
char key[100];

for (i = 1; i < n; i++) {
strcpy(key, SWL[i]);

j = i - 1;
if (SWL[j][0] > key[0])
{
for (j = i - 1; j >= 0 && SWL[j][0] > key[0]; j--)
{
strcpy(SWL[j + 1], SWL[j]);
}
}

else if (SWL[j][0] == key[0])
{
int len1 = strlen(SWL[j]);
int len2 = strlen(key);
int short_l = 0;
if (len1 < len2)
short_l = len1;
else
short_l = len2;

for (int k = 1; k < short_l; k++)
{
if (SWL[j][k] > key[k]) {
for (j = i - 1; j >= 0 && SWL[j][k] > key[k]; j--)
{
strcpy(SWL[j + 1], SWL[j]);
}
break;
}
}
}

strcpy(SWL[j + 1], key);
}

}

제가 이렇게 코드를 짰는데 {{study},{here},{abstract},{abc},{time},{fin}}을 abc순서대로 정렬을 시키려고 하는데 자꾸 이상한 값이 나옵니다. 어떻게 고쳐야 할까요?

익명 사용자의 이미지

이상한 값이 나오는 게 아니라, 프로그램이 작성된 그대로 정상적인 값을 출력하는 것이죠.
컴퓨터는 귀하의 의도 따위와 상관없이 시키는 대로만 동작합니다.
사소한 것처럼 보일지도 모르겠습니다만, 좀 더 책임감 있는 태도로 접근하는 게 좋겠지요.

본론으로 돌아와서, 정렬이 의도대로 되지 않는 이유는 일단 근본적으로 문자열 abc순 비교 코드가 잘못 작성되었기 때문입니다.

그 부분을 따로 떼서 최소 3글자 이상의 문자열들을 이것저것 넣어 테스트해보세요. 어디에 문제가 있는지 금방 드러날 겁니다.

prupru의 이미지

감사합니다 strcmp()로 해결을 했지만 저 코드도 한 번 써보고 싶었습니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.