퀵소팅 소스에서 문제가 있는 것 같아서요.

yangam의 이미지

#include <stdio.h>

#define SWAP(x,y,t) ((t = x), (x = y), (y = t))

void quicksort(int *list, int left, int right);
void printarray(int *list, int len);

int main()
{
	int list[] = { 30, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	int len = sizeof(list) / sizeof(int) - 1;

	printarray(list, 3);
	quicksort(list, 0, 3);
	printarray(list, 3);
	return 0;
}

void quicksort(int *list, int left, int right)
{
	int pivot;
	int i, j, t;

	if (left < right) {
		pivot = list[left];
		i = left + 1;
		j = right;

		do {
			while (list[i] < pivot)
				i++;
			
			while (list[j] > pivot)
				j--;

			if (i < j)
				SWAP(list[i], list[j], t);

			printf("\ti = %d, j = %d\n", i, j);
		} while (i < j);

		SWAP(list[left], list[j], t);
		quicksort(list, left, j-1);
		quicksort(list, j+1, right);
	}
}

void printarray(int *list, int len)
{
	int i;

	for (i = 0; i <= len; i++)
		printf("%3d", list[i]);
	puts("");
}

while (list[i] < pivot)
       i++;

의도적으로 0 - 3 까지의 요소만 정렬하도록 하였습니다.
실행을 해보시면 아시겠지만, i 의 인덱스는 0 - 4 의 범위 안의
값들만 갖어야 합니다. 그런데, 그렇게 되지 않죠.

while (list[i] < pivot && i <= right)
       i++;

위의 소스코드처럼 i 의 범위를 지정해주어야 하는 것 아닌가요?

list[0] - list[i] 까지가 배열의 범위인데
정말 운이 없게도 배열 요소 범위의 바깥인
list[i+1], list[i+2], ..., list[i+n] 의 값들이
설정된 피봇의 값보다 작다면.. i 는 한없이 커지는 것 아닌가요?

질문이 이해가 가실지 모르겠네요;
작문 실력을 늘려야겠다는... ㅠㅠ

제가 제기한 문제에 대해서 설명이 부족하다 싶은 부분은
답글 달아주시면, 제가 리플을 달겠습니다.

의견 부탁드립니다.

참고로, 참고한 소스는 FUNDAMENTALS OF DATA STRUCTURES IN C 입니다.
(그러나, 책의 소스와는 좀 다릅니다.. 수정을 했기 때문에..)

익명 사용자의 이미지

문제가 없는건가요;;; 제가 잘못 생각하고 있는건가요;;;

kane의 이미지

i<right로 범위를 지정해주는 것이 맞습니다.
덤으로, j>left로 j의 범위도 지정해주는게 좋겠죠.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.