-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2a95574
commit 2260b9e
Showing
1 changed file
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <list> | ||
#include <stack> | ||
#include <queue> | ||
using namespace std; | ||
#include <thread> | ||
// 오늘의 주제 : 정렬 | ||
|
||
// C# 자료구조/알고리즘 | ||
// -> A* OpenList (PQ) | ||
// -> C# List = C++ vector | ||
|
||
// PQ O(logN) | ||
// Red-Black Tree O(logN) | ||
// Sorting ? | ||
|
||
|
||
// [5][1][3][7][9][2][4][6][8] | ||
// p | ||
// high low | ||
|
||
int Partition(vector<int>& v, int left, int right) | ||
{ | ||
int pivot = v[left]; | ||
int low = left + 1; | ||
int high = right; | ||
|
||
// O(N) | ||
while (low <= high) | ||
{ | ||
while (low <= right && pivot >= v[low]) | ||
low++; | ||
|
||
while (high >= left + 1 && pivot <= v[high]) | ||
high--; | ||
|
||
if (low < high) | ||
swap(v[low], v[high]); | ||
} | ||
|
||
swap(v[left], v[high]); | ||
return high; | ||
} | ||
|
||
// O(N^2) < 최악 | ||
// O(NlogN) < 평균 | ||
void QuickSort(vector<int>& v, int left, int right) | ||
{ | ||
if (left > right) | ||
return; | ||
|
||
int pivot = Partition(v, left, right); | ||
QuickSort(v, left, pivot - 1); | ||
QuickSort(v, pivot + 1, right); | ||
} | ||
|
||
int main() | ||
{ | ||
vector<int> v; | ||
|
||
srand(time(0)); | ||
|
||
for (int i = 0; i < 50; i++) | ||
{ | ||
int randValue = rand() % 100; | ||
v.push_back(randValue); | ||
} | ||
|
||
//BubbleSort(v); | ||
//SelectionSort(v); | ||
//InsertionSort(v); | ||
//HeapSort(v); | ||
//MergeSort(v, 0, v.size() - 1); | ||
QuickSort(v, 0, v.size() - 1); | ||
} |