2007년 02월 20일
Quick Sort 구현
/* DO NOT reproduce the code above without any permission. */
/* Contact me via email(ted78kr at yahoo.co.kr) if you wish to. */
#include <iostream>
using namespace std;
class SortUtil {
public:
static void quick_sort(int* begin, int* end);
static void disp_array(const int* begin, const int* end);
};
int main(int argc, char* argv[])
{
int a[] = {1, 28, 38, 11, 16, 8, 20, 12, 24, 19, 13, 45, 17};
SortUtil::quick_sort(&a[0], &a[sizeof(a)/sizeof(int)]);
SortUtil::disp_array(&a[0], &a[sizeof(a)/sizeof(int)]);
return 0;
}
void SortUtil::disp_array(const int* begin, const int* end)
{
for(const int* iter = begin; iter < end; ++iter) {
cout << *iter << ' ';
}
cout << endl;
}
void SortUtil::quick_sort(int* begin, int* end)
{
if((end - begin) <= 1) {
return;
}
int* pivot = end - 1;
int* s_idx = begin;
int* e_idx = end - 2;
while(true) {
while(*e_idx > *pivot) --e_idx;
while(*s_idx < *pivot) ++s_idx;
if(e_idx <= s_idx) {
break;
}
swap(*s_idx, *e_idx);
}
swap(*s_idx, *pivot);
quick_sort(begin, s_idx);
quick_sort(s_idx + 1, end);
}
/* Contact me via email(ted78kr at yahoo.co.kr) if you wish to. */
#include <iostream>
using namespace std;
class SortUtil {
public:
static void quick_sort(int* begin, int* end);
static void disp_array(const int* begin, const int* end);
};
int main(int argc, char* argv[])
{
int a[] = {1, 28, 38, 11, 16, 8, 20, 12, 24, 19, 13, 45, 17};
SortUtil::quick_sort(&a[0], &a[sizeof(a)/sizeof(int)]);
SortUtil::disp_array(&a[0], &a[sizeof(a)/sizeof(int)]);
return 0;
}
void SortUtil::disp_array(const int* begin, const int* end)
{
for(const int* iter = begin; iter < end; ++iter) {
cout << *iter << ' ';
}
cout << endl;
}
void SortUtil::quick_sort(int* begin, int* end)
{
if((end - begin) <= 1) {
return;
}
int* pivot = end - 1;
int* s_idx = begin;
int* e_idx = end - 2;
while(true) {
while(*e_idx > *pivot) --e_idx;
while(*s_idx < *pivot) ++s_idx;
if(e_idx <= s_idx) {
break;
}
swap(*s_idx, *e_idx);
}
swap(*s_idx, *pivot);
quick_sort(begin, s_idx);
quick_sort(s_idx + 1, end);
}
이 글과 관련있는 글을 자동검색한 결과입니다 [?]
- 살려주세Yo! by 루미
- [006]sizeof, _msize 두번째 시간 by sally
- 나선형 배열 출력 문제 Spiral Array by 정상혁
- 이진검색 - 재귀호출 알고리즘 by 시즈하
- std::map에서 초기화 해준다. by 샘이
# by | 2007/02/20 23:56 | C/C++ | 트랙백




☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]