-
삽입 정렬 (Insertion Sort)언어/C, C++ 2021. 1. 26. 16:57
#pragma once class InsertionSort { private: int* m_data; int m_size; public: InsertionSort(int MAX = 100); ~InsertionSort(); void Sort(); void InitData(int* data); int GetSize(); int* GetData(); };
InsertionSort.h
#include "InsertionSort.h" InsertionSort::InsertionSort(int MAX) { m_data = new int[MAX]; m_size = MAX; } InsertionSort::~InsertionSort() { if (m_data) delete[] m_data; } void InsertionSort::Sort() { int size = GetSize(); int* data = GetData(); for (int pick = 1; pick < size; ++pick) { int current = pick; int temp = data[pick]; for (; (current > 0) && (data[current - 1] > temp); --current) data[current] = data[current - 1]; data[current] = temp; } } void InsertionSort::InitData(int* data) { for (int i = 0; i < m_size; i++) m_data[i] = data[i]; } int InsertionSort::GetSize() { return m_size; } int* InsertionSort::GetData() { return m_data; }
InsertionSort.cpp
'언어 > C, C++' 카테고리의 다른 글
합병 정렬 (Merge Sort) (0) 2021.01.27 셸 정렬 (Shell Sort) (0) 2021.01.26 버블 정렬 (Bubble Sort) (0) 2021.01.25 선택 정렬 (Selection Sort) (0) 2021.01.25 원형 큐 (Circular Queue) (0) 2021.01.19