"C++ 기수정렬 구현"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-</source> +</syntaxhighlight>, -<source +<syntaxhighlight ))
 
(다른 사용자 한 명의 중간 판 2개는 보이지 않습니다)
1번째 줄: 1번째 줄:
==개요==
==개요==
;C++ 기수정렬 구현
;C++ 기수정렬 구현
<source lang='cpp'>
 
<syntaxhighlight lang='cpp'>
#include <iostream>
void radix_sort(int a[], int size, int base) {
    int ex, i, j, max = a[0];
    for(i=1; i<size; i++) if(a[i]>max) max=a[i];
    for(ex=1; ex<=max; ex*=base) {
    int output[size], count[base] = {0};
    for(i=0; i<size; i++) count[(a[i]/ex)%base]++;
    for(i=1; i<base; i++) count[i] += count[i-1];
    for(i=size-1; i>-1; i--) {
    j = (a[i]/ex)%base;
    output[count[j]-1] = a[i];
    count[j]--;
    }
    for(i=0; i<size; i++) a[i] = output[i];
    }
}
void radix_sort(int a[], int size) {
    radix_sort(a, size, 10);
}
int main() {
    int arr[] = {9,1,22,4,0,1,22,100,10};
    radix_sort(arr, sizeof(arr)/sizeof(int));
    for(int x: arr) std::cout << x << " ";
    // 0 1 1 4 9 10 22 22 100
}
</syntaxhighlight>
<syntaxhighlight lang='cpp'>
#include <iostream>
#include <iostream>
void radix_counting_sort(int a[], int size, int ex, int base) {
void radix_counting_sort(int a[], int size, int ex, int base) {
15번째 줄: 43번째 줄:
}
}
void radix_sort(int a[], int size, int base) {
void radix_sort(int a[], int size, int base) {
int max = a[0];
int i, max = a[0];
for(int i=1; i<size; i++) if(a[i]>max) max=a[i];
for(i=1; i<size; i++) if(a[i]>max) max=a[i];
for(int ex=1; max/ex>0; ex*=base) radix_counting_sort(a, size, ex, base);
for(i=1; i<=max; i*=base) radix_counting_sort(a, size, i, base);
}
}
void radix_sort(int a[], int size) {
void radix_sort(int a[], int size) {
28번째 줄: 56번째 줄:
// 0 1 1 4 9 10 22 22 100  
// 0 1 1 4 9 10 22 22 100  
}
}
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==

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

1 개요[ | ]

C++ 기수정렬 구현
#include <iostream>
void radix_sort(int a[], int size, int base) {
    int ex, i, j, max = a[0];
    for(i=1; i<size; i++) if(a[i]>max) max=a[i];
    for(ex=1; ex<=max; ex*=base) {
    	int output[size], count[base] = {0};
    	for(i=0; i<size; i++) count[(a[i]/ex)%base]++;
    	for(i=1; i<base; i++) count[i] += count[i-1];
    	for(i=size-1; i>-1; i--) {
    		j = (a[i]/ex)%base;
    		output[count[j]-1] = a[i];
    		count[j]--;
    	}
    	for(i=0; i<size; i++) a[i] = output[i];
    }
}
void radix_sort(int a[], int size) {
    radix_sort(a, size, 10);
}
int main() {
    int arr[] = {9,1,22,4,0,1,22,100,10};
    radix_sort(arr, sizeof(arr)/sizeof(int));
    for(int x: arr) std::cout << x << " ";
    // 0 1 1 4 9 10 22 22 100 
}
#include <iostream>
void radix_counting_sort(int a[], int size, int ex, int base) {
	int i, j, output[size], count[base] = {0};
	for(i=0; i<size; i++) count[(a[i]/ex)%base]++;
	for(i=1; i<base; i++) count[i] += count[i-1];
	for(i=size-1; i>-1; i--) {
		j = (a[i]/ex)%base;
		output[count[j]-1] = a[i];
		count[j]--;
	}
	for(i=0; i<size; i++) a[i] = output[i];
}
void radix_sort(int a[], int size, int base) {
	int i, max = a[0];
	for(i=1; i<size; i++) if(a[i]>max) max=a[i];	
	for(i=1; i<=max; i*=base) radix_counting_sort(a, size, i, base);
}
void radix_sort(int a[], int size) {
    radix_sort(a, size, 10);
}
int main() {
	int arr[] = {9,1,22,4,0,1,22,100,10};
	radix_sort(arr, sizeof(arr)/sizeof(int));
	for(int x: arr) std::cout << x << " ";
	// 0 1 1 4 9 10 22 22 100 
}

2 같이 보기[ | ]

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