Mercurial > hg > Members > nobuyasu > CbC
view Huffman/test-quicksort.c @ 24:5354e0f8f557 draft
add unbalance_binary_tree.c
author | Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 13 Aug 2012 03:59:25 +0900 |
parents | f426762609d4 |
children |
line wrap: on
line source
#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; }