ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 버블 정렬 (Bubble Sort)
    언어/C, C++ 2021. 1. 25. 23:26
    #pragma once
    class BubbleSort
    {
    private:
    	int* m_data;
    	int m_size;
    
    public:
    	BubbleSort(int MAX = 100);
    	~BubbleSort();
    
    	void Sort();
    	void Swap(int& a, int& b, int& c);
    
    	void InitData(int* data);
    	int GetSize();
    	int* GetData();
    
    };
    

    BubbleSort.h

     

     

     

     

    #include "BubbleSort.h"
    
    BubbleSort::BubbleSort(int MAX)
    {
    	m_data = new int[MAX];
    	m_size = MAX;
    
    }
    
    BubbleSort::~BubbleSort()
    {
    	if (m_data)
    		delete[] m_data;
    
    }
    
    void BubbleSort::Sort()
    {
    	int size = GetSize();
    	int* data = GetData();
    	int temp;
    
    	bool sorted = false;
    
    	for (int pass = 1; (pass < size) && (sorted == false); ++pass)
    	{
    		sorted = true;
    
    		for (int current = 0; current < size - pass; ++current)
    		{
    			if (data[current] > data[current + 1])
    			{
    				Swap(data[current], data[current + 1], temp);
    				sorted = false;
    			}
    		}
    	}
    
    }
    
    void BubbleSort::Swap(int& a, int& b, int& c)
    {
    	c = a;
    	a = b;
    	b = c;
    
    }
    
    void BubbleSort::InitData(int* data)
    {
    	for (int i = 0; i < m_size; i++)
    		m_data[i] = data[i];
    
    }
    
    int BubbleSort::GetSize()
    {
    	return m_size;
    
    }
    
    int* BubbleSort::GetData()
    {
    	return m_data;
    
    }
    

    BubbleSort.cpp

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

    셸 정렬 (Shell Sort)  (0) 2021.01.26
    삽입 정렬 (Insertion Sort)  (0) 2021.01.26
    선택 정렬 (Selection Sort)  (0) 2021.01.25
    원형 큐 (Circular Queue)  (0) 2021.01.19
    연결 리스트 큐 (LinkedList Queue)  (0) 2021.01.19

    댓글