Last active
December 19, 2015 04:49
-
-
Save karszawa/5899736 to your computer and use it in GitHub Desktop.
後輩のプログラミングの課題が厄介な感じだったのでやってみた
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 以下の要件を満たしバケットソートをする関数を作れ | |
* ・値の範囲は0~9の整数 | |
* ・関数の引数は配列のポインタと配列サイズ | |
* ・カウンタ変数を用いてはならない | |
*/ | |
#include <stdio.h> | |
#define MIN_VALUE (0) | |
#define MAX_VALUE (9) | |
#define LENGTH (MAX_VALUE - MIN_VALUE) | |
void bucket_sort(int* array, int size) | |
{ | |
int bucket[LENGTH + 1] = {0}, *it = NULL, *itt = NULL; | |
for(it = array; it != array + size; it++){ | |
bucket[(*it)]++; | |
} | |
for(it = array, itt = bucket; itt != bucket + LENGTH; itt++){ | |
for(; (*itt) != 0; (*itt)--, it++){ | |
(*it) = itt - bucket; | |
} | |
} | |
} | |
int main(void) | |
{ | |
int array[] = { 1, 2, 6, 1, 0, 2, 5, 2, 5, 2 }, size = 10; | |
int *it = NULL; | |
bucket_sort(array, size); | |
for(it = array; it != array + size; it++){ | |
printf("%d %c", (*it), (it == array + (size - 1) ? '\n' : ' ')); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment