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

15번째 줄: 15번째 줄:
}
}
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) {

2018년 8월 27일 (월) 17:45 판

1 개요

C++ 기수정렬 구현
#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 }}