#include <iostream>
using namespace std;
int partition(int* a, int low, int high)
{
int pivotIdx = (low + high) / 2;
int l = low;
int h = high;
while (l < h)
{
while(a[l] < a[pivotIdx] && l < h)
{
l++;
}
while(a[h] >= a[pivotIdx] && l < h)
{
h--;
}
if (l<h)
{
if (l == pivotIdx)
{
pivotIdx = h;
}
swap(a[l], a[h]);
}
}
swap(a[pivotIdx], a[h]);
pivotIdx = h;
return pivotIdx;
}
void quicksort(int* a, int low, int high)
{
if (low < high)
{
int pivotIdx = partition(a, low, high);
quicksort(a, low, pivotIdx-1);
quicksort(a, pivotIdx+1, high);
}
}
int main()
{
int n = 0;
cin >> n;
int* numbers = new int[n];
srand(1);
for (int i = 0; i < n; i++)
{
numbers[i] = rand()%100;
}
cout << "before : ";
for (int i = 0; i < n; i++)
{
cout << numbers[i] << " ";
}
cout << endl;
quicksort(numbers, 0, n - 1);
cout << "after : ";
for (int i = 0; i < n; i++)
{
cout << numbers[i] << " ";
}
cout << endl;
cout << count;
delete[] numbers;
return 0;
}
'프로그래밍 > C++' 카테고리의 다른 글
Function Pointer 사용하기 (0) | 2018.01.29 |
---|---|
C++ Template이란 (0) | 2018.01.14 |
List Vector Copy (0) | 2017.05.20 |
자료구조-Queue 간단한 구현 (2) | 2017.03.28 |
scanf(%d 문자 입력시 무한루프 (0) | 2017.03.20 |