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.

Results 1 to 2 of 2

Thread: Issue Determing Average Number of Comparisons for Quicksort

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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[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.

         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


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default 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.

Similar Threads

  1. [SOLVED] Average Rainfall Main Class Using nested for loops,input validation - Average is off
    By CyberOps in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 24th, 2014, 05:36 AM
  2. Determing if one string is a rotation of another
    By gabie1121 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 5th, 2012, 09:42 PM
  3. Program should display number of positives\negatives\total\average
    By alias22 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 21st, 2012, 03:21 PM
  4. [SOLVED] counting number of comparisons in merge sort
    By mia_tech in forum What's Wrong With My Code?
    Replies: 9
    Last Post: May 26th, 2012, 11:54 PM
  5. Find total number less than average
    By maximus20895 in forum Collections and Generics
    Replies: 2
    Last Post: December 1st, 2010, 01:46 PM

Tags for this Thread