ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 쾌속 정렬 (Quick Sort)
    언어/C, C++ 2021. 1. 27. 23:33
    #pragma once
    class QuickSort
    {
    private:
    	int* m_data;
    	int m_size;
    
    public:
    	QuickSort(int MAX = 100);
    	~QuickSort();
    
    	void Sort();
    	void QuickSorting(int first, int last);
    	int Partition(int first, int last);
    	void Swap(int& a, int& b, int& c);
    
    	void InitData(int* data);
    	int GetSize();
    	int* GetData();
    
    };
    

    QuickSort.h

     

     

     

     

    #include "QuickSort.h"
    
    QuickSort::QuickSort(int MAX)
    {
    	m_data = new int[MAX];
    	m_size = MAX;
    
    }
    
    QuickSort::~QuickSort()
    {
    	if (m_data)
    		delete[] m_data;
    
    }
    
    void QuickSort::Sort()
    {
    	int first = 0;
    	int last = GetSize() - 1;
    	QuickSorting(first, last);
    }
    
    void QuickSort::QuickSorting(int first, int last)
    {
    	if (first < last)
    	{
    		int pivotIndex = Partition(first, last);
    		QuickSorting(first, pivotIndex - 1);
    		QuickSorting(pivotIndex + 1, last);
    	}
    
    }
    
    int QuickSort::Partition(int first, int last)
    {
    	int* data = GetData();
    	int low, high;
    	int pivot, temp;
    
    	pivot = data[last];
    	low = first;
    	high = last - 1;
    
    	while (low <= high)
    	{
    		while (pivot > data[low])
    			low++;
    
    		while (pivot < data[high])
    			high--;
    
    		if (low <= high)
    			Swap(data[low], data[high], temp);
    
    	}
    
    	Swap(data[low], data[last], temp);
    
    	return low;
    }
    
    void QuickSort::Swap(int& a, int& b, int& c)
    {
    	c = a;
    	a = b;
    	b = c;
    
    }
    
    void QuickSort::InitData(int* data)
    {
    	for (int i = 0; i < m_size; i++)
    		m_data[i] = data[i];
    
    }
    
    int QuickSort::GetSize()
    {
    	return m_size;
    
    }
    
    int* QuickSort::GetData()
    {
    	return m_data;
    
    }
    

    QuickSort.cpp

    '언어 > C, C++' 카테고리의 다른 글

    [C++] Stack STL  (0) 2021.07.17
    [C++] List STL  (0) 2021.07.16
    합병 정렬 (Merge Sort)  (0) 2021.01.27
    셸 정렬 (Shell Sort)  (0) 2021.01.26
    삽입 정렬 (Insertion Sort)  (0) 2021.01.26

    댓글