Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Members have full access to the forums. Advertisements are removed for registered users.

# Thread: Issue Determing Average Number of Comparisons for Quicksort

1. ## 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.

```    public static void tests(int arraySize)
{
long numComparisons = 0;
long averageComparisons = 0;
long[] numComparisonsArray = new long;

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.

```     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 ;
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;

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  Reply With Quote

3. ## 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.  Reply With Quote

#### Tags for this Thread 