From d2da61b81d8c122a994295e78d703f90a171fcc0 Mon Sep 17 00:00:00 2001 From: "zik.saleeba" Date: Fri, 29 May 2009 03:24:36 +0000 Subject: [PATCH] Added a quicksort test case git-svn-id: http://picoc.googlecode.com/svn/trunk@311 21eae674-98b7-11dd-bd71-f92a316d2d60 --- tests/25_quicksort.c | 75 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 tests/25_quicksort.c diff --git a/tests/25_quicksort.c b/tests/25_quicksort.c new file mode 100644 index 0000000..578aa14 --- /dev/null +++ b/tests/25_quicksort.c @@ -0,0 +1,75 @@ +int array[16]; + +array[0] = 62; +array[1] = 83; +array[2] = 4; +array[3] = 89; +array[4] = 36; +array[5] = 21; +array[6] = 74; +array[7] = 37; +array[8] = 65; +array[9] = 33; +array[10] = 96; +array[11] = 38; +array[12] = 53; +array[13] = 16; +array[14] = 74; +array[15] = 55; + +//Swap integer values by array indexes +void swap(int a, int b) +{ + int tmp = array[a]; + array[a] = array[b]; + array[b] = tmp; +} + +//Partition the array into two halves and return the +//index about which the array is partitioned +int partition(int left, int right) +{ + int pivotIndex = left; + int pivotValue = array[pivotIndex]; + int index = left; + int i; + + swap(pivotIndex, right); + for(i = left; i < right; i++) + { + if(array[i] < pivotValue) + { + swap(array, i, index); + index += 1; + } + } + swap(right, index); + + return index; +} + +//Quicksort the array +void quicksort(int left, int right) +{ + if(left >= right) + return; + + int index = partition(left, right); + quicksort(left, index - 1); + quicksort(index + 1, right); +} + +int i; + +for (i = 0; i < 16; i++) + printf("%d ", array[i]); + +printf("\n"); + +//quicksort(0, 15); + +for (i = 0; i < 16; i++) + printf("%d ", array[i]); + +printf("\n"); +