# Issue Determing Average Number of Comparisons for Quicksort

• February 24th, 2014, 11:49 AM
gabie1121
Issue Determing Average Number of Comparisons for Quicksort
My objective is to execute quick sort ( i was told to convert the pseudocode from the Cormen book) using arrays of increasing sizes and find the average number of comparisons for each of those sizes over 100 iterations. This is a school project and the numbers I am getting are far larger than those of my friends, so I am clearly doing something wrong. I believe it must be in the way that I am collecting and averaging my number of comparisons. I will first give the method in which most of that calculating is done, then I will include the whole program.

Code Java:

``` public static void tests(int arraySize) { long numComparisons = 0; long averageComparisons = 0; long[] numComparisonsArray = new long[100];   for(int i = 0; i<100; i++) { int[] array= genRandomArray(arraySize); numComparisons=quickSort(array,0,array.length-1); numComparisonsArray[i]=numComparisons; numComparisons = 0; }   for(int i =0; i<numComparisonsArray.length;i++) { averageComparisons += numComparisonsArray[i]; }   averageComparisons = averageComparisons/100; System.out.println("Average comparisons for "+ arraySize+" "+averageComparisons); totalComparisons[totalComparisonsIndex] = averageComparisons; totalComparisonsIndex++; averageComparisons=0; }```

I am incrementing the variable comparisonCount for each comparison done and adding that to an array of all the comparison numbers for that array size. I am then running through that array, averaging it, and placing that average into another array.

Code Java:

``` import java.util.Random; import java.io.*;   public class TestQS {   public static Random randomGenerator = new Random(System.currentTimeMillis()); public static int comparisonCount = 0; public static long[] totalComparisons = new long [90]; public static int totalComparisonsIndex = 0;   public static void main(String[] args) { for(int i = 1100;i<=10000;i=i+100) { tests(i); }   try{ PrintWriter writer = new PrintWriter("results.txt","UTF-8"); int indexNum = 1100; for (int i =0; i<totalComparisons.length;i++) { writer.println(indexNum+", "+totalComparisons[i]); indexNum=indexNum+100; } writer.close(); System.out.println("done"); } catch(IOException ex) { ex.printStackTrace(); }   }       public static void tests(int arraySize) { long numComparisons = 0; long averageComparisons = 0; long[] numComparisonsArray = new long[100];   for(int i = 0; i<100; i++) { int[] array= genRandomArray(arraySize); numComparisons=quickSort(array,0,array.length-1); numComparisonsArray[i]=numComparisons; numComparisons = 0; }   for(int i =0; i<numComparisonsArray.length;i++) { averageComparisons += numComparisonsArray[i]; }     averageComparisons = averageComparisons/100; System.out.println("Average comparisons for "+ arraySize+" "+averageComparisons); totalComparisons[totalComparisonsIndex] = averageComparisons; totalComparisonsIndex++; averageComparisons=0; }     public static int[] genRandomArray(int arraySize) { int[] array = new int[arraySize]; for(int i = 0;i<array.length;i++) { array[i] = randomGenerator.nextInt(); } return array; }   public static int partition(int[] array, int p, int r) { int x = array[r]; int i = p-1; int temp = 0; for(int j =p; j<= r-1;j++) { if( array[j] <= x) { i = i+1; temp = array[i]; array[i]= array[j]; array[j] = temp; comparisonCount++; } else { comparisonCount++; } } temp = array[i+1]; array[i+1]= array[r]; array[r]=temp; return (i+1); }   public static int quickSort(int[] array, int p, int r) { int q; if (p < r) { comparisonCount++; q = partition(array,p,r); quickSort(array,p,q-1); quickSort(array,q+1,r); } else { comparisonCount++; } return comparisonCount;   } }```

--- Update ---

SOLVED: I was simply not zeroing out one of my variables
• February 24th, 2014, 03:23 PM
GregBrannon
Re: Issue Determing Average Number of Comparisons for Quicksort
So long gone. Welcome back, even if it was only for a quick moment to stall long enough for inspiration to catch up with you.