컴맹학자 2022. 9. 6. 00:55
728x90

Selection Sort
특징
1. 조건에 따라 앞에서든 뒤에서든 천천히 1칸씩 정렬이 된다.
2. 최악, 최대, 평균 구분 없이 실행 시간은 같다


Bubble Sort
특징
1. x와 x+1를 2개씩 비교해서 뒤에서 부터 정렬
2. 최악, 최대, 평균 구분 없이 실행 시간은 같다
3. Swap 과정이 많기 때문에 느리다



Insertion Sort
특징 
1. 비교할 x와 한개씩 증가하면서 비교할 y의 통해서 정렬
2. x, y값 비교해서 Swap이 발생하면 교환 하는 자리를 비우고 한칸씩 우측으로 밀어버린다. 
3. 반복 돌릴수록 정렬이 자동적으로 되기 때문에 위에 2개 정렬 보단 빠르다

 


#pragma once

#define SIZE 7

class SlowSort
{
public:
	void SelectSort();
	
	void BubbleSort();
	
	void InsertSort();

	void Swap(int& x, int& y);
	void Print(int data[]);

private:
	int Selectdata[SIZE] = { 50, 80, 20, 10, 60, 70, 60 };
	int Bubbledata[SIZE] = { 50, 90, 20, 77, 100, 70, 60 };
	int Insertdata[SIZE] = { 50, 22, 29, 18, 6, 70, 99 };
};

SlowSort.h

 

#include "Global.h"
#include "SlowSort.h"

void SlowSort::SelectSort()
{
	int max = SIZE;
	for (int i = 0; i < max-1; i++)	{
		for (int j = i + 1; j < max; j++)		{
			if (Selectdata[i] > Selectdata[j])
				Swap(Selectdata[i], Selectdata[j]);
		}
	}
	Print(Selectdata);
}


void SlowSort::BubbleSort()
{
	int max = SIZE;
	for (int i = max -1; i > 0 ; i--){
		for (int j = 0; j < i; j++){
			if (Bubbledata[j] > Bubbledata[j+1])
				Swap(Bubbledata[j], Bubbledata[j+1]);
		}
	}

	Print(Selectdata);
}

void SlowSort::InsertSort()
{
	int max = SIZE;
	int key;
	int j;
	for (int i = 1; i < max; i++) {
		key = Insertdata[i];

		//조건이 2개 
		// 1. j >= 0
		// 2. key가 비교보다 작은 경우만 
		for (j = i - 1; j >= 0 && Insertdata[j] > key; j--)
			Insertdata[j + 1] = Insertdata[j];

		Insertdata[j+1] = key;
	}
    //https://www.youtube.com/watch?v=EdIKIf9mHk0

	Print(Insertdata);
}

void SlowSort::Swap(int& x, int& y)
{
	int temp = x;
	x = y;
	y = temp;
}

void SlowSort::Print(int data[])
{
	for (int i = 0; i < SIZE; i++)
		cout << data[i] << " / ";
	cout << endl;
}

SlowSort.cpp