Quicksort Stack

Embed Size (px)

DESCRIPTION

Quicksort using a stack

Citation preview

  • Birla Institute of Technology and Science, Pilani Data Structures and Algorithms

    Lab: 3 (Tuesday, 25th Aug) Tuesday 18.08.09 QUICKSORT Max Marks: 16

    Problem Statement:

    1. Implement iterative version of the Quicksort algorithm. Use Stack ADT to maintain the sub-list information. Input should be generated randomly for a given size of array.

    2. Measure the execution time in microseconds for the above function for varying input sizes from 10 to 107. Use time.h library functions for measuring execution time. Use of time functions is demonstrated below. Compute NlogN for the different input sizes (N) and estimate the constant of proportionality c so that all the measured values are upper-bounded by cNlogN.

    3. Measure the execution time of the recursive algorithm for same sets of input data and find the ratio of improvement in execution time for each input size.

    4. Measure the maximum space utilized by both recursive and iterative Quicksort

    algorithms. For recursive algorithm maintain a counter for depth of recursion to simulate maximum growth of machine stack. For iterative algorithm, measure the maximum growth of the explicit stack.

    Measuring Execution Time:

    #include

    int main(int argc,char **argv)

    { ......

    // Declaration of timeval variables which will capture the system time

    long time_start, time_end; struct timeval tv;

    // Write all the initialization code here

    // Time measurement begins here

    gettimeofday(&tv,NULL); /*gets the current system time into variable tv */ time_start = (tv.tv_sec *1e+6) + tv.tv_usec;

    // calculating start time in seconds

  • // Write the code to be analyzed here

    gettimeofday (&tv, NULL); time_end = (tv.tv_sec *1e+6) + tv.tv_usec;

    // calculating end time in seconds

    // (time_end-time_start) gives the total execution time

    return (0);

    }

    Given:

    1. Implementation of recursive Quicksort algorithm.

    2. ADT Stack

    Deliverables:

    1. Iterative Quicksort function in quicksortfunctions.c

    2. Print comparative time analysis in following format in main.c

    Time and Space analysis of recursive Vs. iterative quicksort

    Input size

    n*logn Time (recursive)

    Const (CR)

    Time (Iterative)

    Const(CI) Ratio of improvement

    10 33.219 2.00 16.610 58.00 0.573 ..

    5010 61575.879 1182.00 52.095 123593.00 0.498

    (Sample output)

    Input size

    Space measured (recursive)

    Space measured (iterative)