"C언어 선택정렬 구현"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
(다른 사용자 한 명의 중간 판 5개는 보이지 않습니다)
2번째 줄: 2번째 줄:
;C언어 선택정렬 구현
;C언어 선택정렬 구현


<source lang='c'>
<syntaxhighlight lang='c'>
#include <stdio.h>
#include <stdio.h>
#define ARRAYSIZE(A) sizeof(A) / sizeof((A)[0])
void selection_sort(int a[], int size) {
void selection_sort(int a[], int size) {
     int i, j, minidx, temp;
     int i, j, minidx, temp;
12번째 줄: 11번째 줄:
             if(a[minidx] > a[j]) minidx = j;
             if(a[minidx] > a[j]) minidx = j;
         }
         }
         temp = a[minidx];
         temp=a[minidx]; a[minidx]=a[i]; a[i]=temp;
        a[minidx] = a[i];
        a[i] = temp;
     }
     }
}
}
int main() {
int main() {
int arr[] = {9,1,22,4,0,-1,1,22,100,10};
int arr[] = {9,1,22,4,0,-1,1,22,100,10};
int size = ARRAYSIZE(arr);
int size = sizeof(arr)/sizeof(int);
selection_sort(arr, size);
selection_sort(arr, size);
for(int i=0; i<size; i++) printf("%d ", arr[i]);
for(int i=0; i<size; i++) printf("%d ", arr[i]);
// -1 0 1 1 4 9 10 22 22 100  
// -1 0 1 1 4 9 10 22 22 100  
}
}
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==
* [[선택정렬]]
* [[선택정렬 구현]]
* [[선택정렬 구현]]


[[분류: C 정렬]]
[[분류: C 정렬]]
[[분류: 선택정렬]]
[[분류: 선택정렬]]

2020년 11월 2일 (월) 02:41 기준 최신판

1 개요[ | ]

C언어 선택정렬 구현
#include <stdio.h>
void selection_sort(int a[], int size) {
    int i, j, minidx, temp;
    for(i=0; i<size; i++) {
        minidx = i;
        for(j=i+1; j<size; j++) {
            if(a[minidx] > a[j]) minidx = j;
        }
        temp=a[minidx]; a[minidx]=a[i]; a[i]=temp;
    }
}
int main() {
	int arr[] = {9,1,22,4,0,-1,1,22,100,10};
	int size = sizeof(arr)/sizeof(int);
	selection_sort(arr, size);
	for(int i=0; i<size; i++) printf("%d ", arr[i]);
	// -1 0 1 1 4 9 10 22 22 100 
}

2 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}