[c언어] 선택 정렬
2020. 12. 21. 15:33ㆍComputer Science/C
0이상 100미만의 난수를 발생시켜 이를 정렬하여 출력하는 프로그램을 작성한다.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void selectionSort(int list[], int size);
int main(void)
{
int i;
int sort[10];
srand(time(NULL));
for (i = 0;i < 10;i++)
sort[i] = rand() % 100;
printf("발생된 난수: \t");
for (i = 0;i < 10;i++)
printf("%d ", sort[i]);
printf("\n");
selectionSort(sort, 10);
return 0;
}
void selectionSort(int list[], int size)
{
int i, j, indexMin, temp;
printf("정렬 후: \t\t");
for (i = 0;i < size-1;i++)
{
indexMin = i;
for (j = i + 1;j < size;j++)
{
if (list[j] < list[indexMin])
{
indexMin = j;
}
}
temp = list[indexMin];
list[indexMin] = list[i];
list[i] = temp;
}
for (i = 0;i < 10;i++)
printf("%d ", list[i]);
printf("\n");
}
결과
'Computer Science > C' 카테고리의 다른 글
[c언어] 2차원 배열 각 행, 각 열의 합 (0) | 2020.12.21 |
---|---|
[c언어] 2차원 배열 각 열의 합 (0) | 2020.12.21 |
[c언어] 집합 구현 (0) | 2020.12.21 |
[c언어] 다중집합 (0) | 2020.12.21 |
[c언어] 문자열 속 숫자 합 계산 (아스키코드) (0) | 2020.12.21 |