Skip to content

Commit

Permalink
퀵 정렬
Browse files Browse the repository at this point in the history
  • Loading branch information
developeSHG committed Aug 14, 2023
1 parent 2a95574 commit 2260b9e
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions Algorithm/Algorithm/Sorting_3.cpp
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);
}

0 comments on commit 2260b9e

Please sign in to comment.