일지
알고리즘...10
niamdank
2021. 6. 16. 10:44
선택 정렬_계속
위 알고리즘을 실제 구현하고 과정을 살펴보면 다음과 같다.
SelectionSort.hpp
#pragma once
#include "../Common.hpp"
void SelectionSort(int arr[], int n)
{
Common::PrintArray(arr, n);
for (int i = 1; i < n; i++)
{
int biggest{ n - i };
for (int j = 0; j < n - i; j++)
{
if (arr[biggest] < arr[j])
{
biggest = j;
}
}
Common::Swap(arr, biggest, n - i);
Common::PrintArray(arr, n);
}
}
main.cpp
#include "Sort/SelectionSort.hpp"
int main()
{
int arr[10] = { 2, 4, 6, 8, 10, 9, 7, 5, 3, 1 };
SelectionSort(arr, 10);
}
실행결과
2 4 6 8 10 9 7 5 3 1
2 4 6 8 1 9 7 5 3 10
2 4 6 8 1 3 7 5 9 10
2 4 6 5 1 3 7 8 9 10
2 4 6 5 1 3 7 8 9 10
2 4 3 5 1 6 7 8 9 10
2 4 3 1 5 6 7 8 9 10
2 1 3 4 5 6 7 8 9 10
2 1 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
Algorithm/SelectionSort.hpp at main · NadanKim/Algorithm · GitHub
NadanKim/Algorithm
알고리즘 학습 및 예제 코드 작성을 위한 저장소. Contribute to NadanKim/Algorithm development by creating an account on GitHub.
github.com