Mercurial > hg > Members > nobuyasu > CbC
changeset 18:f426762609d4 draft
add test-quicksort
author | Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 31 Jul 2012 06:27:17 +0900 |
parents | 2ec14e6d8303 |
children | a21df07434f2 |
files | Huffman/test-huffman.c Huffman/test-quicksort.c |
diffstat | 2 files changed, 135 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/Huffman/test-huffman.c Tue Jul 31 04:55:48 2012 +0900 +++ b/Huffman/test-huffman.c Tue Jul 31 06:27:17 2012 +0900 @@ -1,16 +1,80 @@ #include <stdio.h> #include <stdlib.h> +#define N 256 +#define READ_BUF_SIZE 1024 +#define INPUT(fp, buf, result, max_size) result = fread(buf, 1, max_size, fp) +int parent[2*N]; +int l_node[2*N], r_node[2*N]; + +void q_sort(int* numbers[], int left, int right) +{ + int pivot, l_hold, r_hold; + int *bp; + l_hold = left; + r_hold = right; + pivot = *numbers[left]; + bp = numbers[left]; + while (left < right) + { + while ((*numbers[right] >= pivot) && (left < right)) + right--; + if (left != right) + { + numbers[left] = numbers[right]; + left++; + } + while ((*numbers[left] <= pivot) && (left < right)) + left++; + if (left != right) + { + numbers[right] = numbers[left]; + right--; + } + } + numbers[left] = bp; + pivot = left; + left = l_hold; + right = r_hold; + if (left < pivot) + q_sort(numbers, left, pivot-1); + if (right > pivot) + q_sort(numbers, pivot+1, right); +} +void quick_sort(int* numbers[], int array_size) +{ + q_sort(numbers, 0, array_size - 1); +} + + +int encode(char *buf, size_t n_chars) +{ + int alp[N]; + + int i; + for (i=0; i<N; i++ ) alp[i] = 0; + for (i=0; i<n_chars; i++ ) { + alp[buf[i]]++; + } + + int *numbers[N]; + for (i=0; i<N; i++ ) numbers[i] = alp+i; -#define READ_BUF_SIZE 1024 + for (i=0; i<N; i++) { + + } + -#define INPUT(fp, buf, result, max_size) result = fread(buf, 1, max_size, fp) + return 0; +} -int main(int argc, char* argv[]) { + +int main(int argc, char* argv[]) +{ FILE *fp; char *filename = "./test.txt"; @@ -23,13 +87,19 @@ size_t n_chars; INPUT(fp, base, n_chars, READ_BUF_SIZE); + do { buf = base; int ret; + + + encode((char)buf, n_chars); +/* while ( (ret = (int)*buf) != '\0' ) { putc(*buf,stdout); ++buf; } +*/ if ( n_chars < READ_BUF_SIZE ) { break; }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Huffman/test-quicksort.c Tue Jul 31 06:27:17 2012 +0900 @@ -0,0 +1,62 @@ +#include <stdio.h> +void q_sort(int* numbers[], int left, int right) +{ + int pivot, l_hold, r_hold; + int *bp; + l_hold = left; + r_hold = right; + pivot = *numbers[left]; + bp = numbers[left]; + while (left < right) + { + while ((*numbers[right] >= pivot) && (left < right)) + right--; + if (left != right) + { + numbers[left] = numbers[right]; + left++; + } + while ((*numbers[left] <= pivot) && (left < right)) + left++; + if (left != right) + { + numbers[right] = numbers[left]; + right--; + } + } + numbers[left] = bp; + pivot = left; + left = l_hold; + right = r_hold; + if (left < pivot) + q_sort(numbers, left, pivot-1); + if (right > pivot) + q_sort(numbers, pivot+1, right); +} +void quick_sort(int* numbers[], int array_size) +{ + q_sort(numbers, 0, array_size - 1); +} + + + +int main() +{ + int n = 6; + int buf[] = {33, 23, 11, 44, 51, 98}; + int *numbers[n]; + + int i; + + for (i=0; i<n; i++) { + numbers[i] = buf+i; + } + + quick_sort(numbers, n); // N is size of "buf". It is defined in "test-huffman.c". + + for (i=0; i<n; i++ ) { + printf("%d ",*numbers[i]); + } + puts(""); + return 0; +}