quicksort함수보다 다른것땜에 시간을 더 썼네여;

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <Windows.h>
#include <conio.h>

const int MAX_SIZE = 15;

void SetColor(int back, int font)
{
	SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE), (back*16+font));
	//	2번째 인자는 0x49
	// 4는 백그라운드 컬러
	// 9는 글자색
}

void gotoxy(int x,int y)
{
	COORD cur;
	cur.X=x;
	cur.Y=y;
	
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),cur);
}

COORD getCursorPosition(void)
{
    CONSOLE_SCREEN_BUFFER_INFO buffInfo;
    GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),&buffInfo);
    //현재콘솔스크린버퍼에서 정보읽어와서 buffInfo에 저장
    return   buffInfo.dwCursorPosition;
	//CONSOLE_SCREEN_BUFFER_INFO구조체형변수 buffInfo에 속한 멤버변수이고  
	//COORD형인dwCursorPosition을 리턴   (x,y좌표값이 들어있다)
}


void draw(int arr[], int size, int pivot, int low, int high)
{
	system("cls");
	::SetColor(0, 15);
	for(int i=0; i<size; ++i)
	{
		if( i == pivot )
		{
			::SetColor(1, 15);
		}
		else if( i == low )
		{
			::SetColor(2, 15);
		}
		else if( i == high )
		{
			::SetColor(3, 15);
		}
		else
			::SetColor(0, 15);

		printf("%2d: ", arr[i]);
		for(int j=0; j<arr[i]; ++j)
		{
			printf("■");
		}
		
		COORD cur = getCursorPosition();
		if( i == pivot )
		{
			gotoxy(40, cur.Y);
			printf("pivot");
		}
		else if( i == low )
		{
			gotoxy(40, cur.Y);
			printf("low");
		}
		else if( i == high )
		{
			gotoxy(40, cur.Y);
			printf("high");
		}

		printf("\n");
	}
	Sleep(500);
	::SetColor(0, 15);
}

void swap(int *a, int *b)
{
	static int temp = 0;
	temp = *a;
	*a = *b;
	*b = temp;
}

void quicksort(int arr[], int _low, int _high)
{
	if( _low >= _high )
		return;

	int pivot = _low;
	int low = pivot+1;
	int high = _high-1;

	while(true)
	{
		while( arr[pivot] >= arr[low] )	++low;
		while( arr[pivot] < arr[high] ) --high;

		if( low >= high )
		{
			swap(&arr[pivot], &arr[high]);
			draw(arr, MAX_SIZE, pivot, low, high);
			break;
		}
		
		swap(&arr[low], &arr[high]);
		draw(arr, MAX_SIZE, pivot, low, high);
	}
	quicksort(arr, _low, high);
	quicksort(arr, high+1, _high);
}

int main(int argc, char **argv)
{
	srand(time(0));
	int arr[MAX_SIZE] = { 0, };
	for(int i=0; i<MAX_SIZE;)
	{
		arr[i] = rand()%100;
		if( arr[i] <= MAX_SIZE )
			++i;
	}

	quicksort(arr, 0, MAX_SIZE);


	return 0;
}
신고
by Ergate 2011.07.24 15:52

티스토리 툴바