본문 바로가기
Computer Science/Data Structure

Quick - Sort Algorithm

by 진뚱 2019. 5. 30.
728x90

미완성 설명 필요


#include <iostream>
using namespace std;
int a[1000000];
void swap(int &x, int &y) {
	int z = x;
	x = y;
	y = z;
}
int choosePivot(int low, int high) {
	//return low + (high - low) / 2;
	return high;
}
int partition(int low, int high) {
	int pivotIndex = choosePivot(low, high);
	int pivotValue = a[pivotIndex];
	swap(a[pivotIndex], a[high]);
	int storeIndex = low;
	for (int i = low; i<high; i++) {
		if (a[i] < pivotValue) {
			swap(a[i], a[storeIndex]);
			storeIndex += 1;
		}
	}
	swap(a[storeIndex], a[high]);
	return storeIndex;
}
void quicksort(int low, int high) {
	if (low < high) {
		int pivot = partition(low, high);
		quicksort(low, pivot - 1);
		quicksort(pivot + 1, high);
	}
}
int main() {
	int n;
	cin >> n;
	for (int i = 0; i<n; i++) {
		cin >> a[i];
	}
	quicksort(0, n - 1);
	for (int i = 0; i<n; i++) {
		printf("%d\n", a[i]);
	}
	return 0;
}
728x90

'Computer Science > Data Structure' 카테고리의 다른 글

Hash  (0) 2021.02.18

댓글