Skip to content

Instantly share code, notes, and snippets.

@starman3ch
Last active August 29, 2015 14:27
Show Gist options
  • Select an option

  • Save starman3ch/188c23b6f625f366e744 to your computer and use it in GitHub Desktop.

Select an option

Save starman3ch/188c23b6f625f366e744 to your computer and use it in GitHub Desktop.
선택 정렬 (C로 구현)
#include <stdio.h>
void SelSort(int arr[], int n)
{
int i, j;
int minIdx;
int temp;
for(i=0; i<n-1; i++)
{
minIdx = i;
for(j=i+1; j<n; j++) // 최소값 탐색
{
if(arr[j] < arr[minIdx])
minIdx = j;
}
// 교환
temp = arr[i];
arr[i] = arr[minIdx];
arr[minIdx] = temp;
}
}
int main(void)
{
int arr[4] = {3, 4, 2, 1};
int i;
SelSort(arr, sizeof(arr)/sizeof(int));
for(i=0; i<4; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
// 출처 : 윤성우 자료구조
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment