본문 바로가기
C++ 알고리즘/5. 정렬 알고리즘

선택 알고리즘 (시간 복잡도 O(N^2) )

by Beijing_KingGod 2019. 9. 1.

#include <iostream>
using namespace std;

int main(){
	int array[]={1,10,5,8,7,6,4,3,2,9};
	
	for(int i=0; i<sizeof(array)/sizeof(int); i++){
		int min=1000000;
		int index=0;
		for(int j=i; j<sizeof(array)/sizeof(int); j++){
			if(min>array[j]){
				min=array[j];
				index=j;
			}
		}
		
		if(index!=i){
			int temp = array[index];
			array[index]=array[i];
			array[i]=temp;
		}
	}

	for(int i=0; i<sizeof(array)/sizeof(int); i++){
		cout<<array[i]<<"  ";
	}
	cout<<endl;
	system("pause");
	return 0;
}

 

1  2  3  4  5  6  7  8  9  10

 

 

 

 

 

 

 

 

출처 :https://gmlwjd9405.github.io/2018/05/06/algorithm-selection-sort.html

 

[알고리즘] 선택 정렬(selection sort)이란 - Heee's Development Blog

Step by step goes a long way.

gmlwjd9405.github.io

http://blog.naver.com/PostView.nhn?blogId=ndb796&logNo=221226800661&categoryNo=128&parentCategoryNo=0&viewDate=¤tPage=9&postListTopCurrentPage=&from=postList&userTopListOpen=true&userTopListCount=5&userTopListManageOpen=false&userTopListCurrentPage=9

 

2. 정렬의 개요와 선택 정렬(Selection Sort)

일반적으로 알고리즘을 공부할 때 가장 먼저 풀어보는 문제는 '정렬(Sort)' 문제입니다. 왜냐하면 정렬만...

blog.naver.com

 

댓글