I/T/C2009. 11. 16. 21:05
  선택 정렬로 가장 큰 것을 선택하여 가장 마지막 것과 스와핑하는 방식입니다.

#include <stdio.h>
#include <stdlib.h>

typedef int boolean;
const boolean FALSE = 0;
const boolean TRUE = 1;

// comment와 함께 배열 내용을 출력 
void printArray(char* comment, int A[], int N);
// 정렬 알고리즘 
void selectionSort(int A[], int N);

int main(int argc, char *argv[])
{
	int A[] = {22, 37, 15, 19, 12};
	
	printf("Selection Sort\n");
	printArray("Input  : ", A, 5);
	selectionSort(A, 5);
	printArray("Output : ", A, 5);
	printf("\n");
	
	system("PAUSE");	
	return 0;
}

void selectionSort(int A[], int N)
{
	int i, j, maxIndex;
	
	for(i=N ; i>1 ; i--) {
		maxIndex = 0;
		
		// 가장 큰 값이 있는 maxIndex 구하기 
		for(j=1 ; j<i ; j++ ) {
			if(A[j]>A[maxIndex]) {
				maxIndex = j;
			}
		}
		
		// 배열의 마지막 값이 maxIndex와 일치 하지 않을 때만 교환 
		if(maxIndex!=i-1) {
			int temp = A[i-1];
			A[i-1] = A[maxIndex];
			A[maxIndex] = temp;
		}
	}
}

void printArray(char* comment, int A[], int N)
{
	int i;

	printf("%s", comment);
	for(i=0 ; i<N ; i++) {
		printf("%d\t", A[i]);
	}
	printf("\n");
}


Posted by 황타