Question

Write a c program to implement Quicksort for arrays of length 16, as described in the textbook, taking always as the pivot the last element of the corresponding subarray. Define a variable COUNTER in your program that keeps track of the total number of comparisons among the elements in the array needed to sort the array Run your program 100 times using as inputs 100 arrays of real numbers in the interval [0, 11 generated randomly. Find the sample average (Counter1+Counter 2+Counter100)/100

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>

void quicksort(double *,int,int, int *);

int partition(double *,int,int, int *);

int main()

{

    int n = 16;

    int total_comparison = 0;

    int i;

   

    for( i = 0 ; i < 100 ; i++ )

    {

        double arr[n];

        int comparison = 0;

        int i;

       

        for( i = 0 ; i < 16 ; i++ )

            // generate random number between o to 1

            arr[i] = (double)( rand() % 32767 ) / 32767.0;

           

        quicksort(arr, 0, n - 1, &comparison);

       

        total_comparison += comparison;

    }

   

    // compute average

    double avg = (double)total_comparison / 100.0;

           

    printf("Average : %lf", avg);

           

    return 0;

}

// function to sort using quick sort

void quicksort(double *arr,int p,int r, int *comparison)

{

    int q;

   

    if(p<r)

    {

        // create a partition

        q = partition(arr,p,r, comparison);

       

        // sort the left subarray

        quicksort(arr,p,q-1, comparison);

       

        // sort the right subarray

        quicksort(arr,q+1,r, comparison);

    }

}

int partition(double *arr,int p,int r, int *comparison)

{  

    // set pivot

    int pivot = arr[r];

   

    // set the lindex of lower element

    int j, i = p - 1;

    for (j = p; j <= r - 1; j++ )

    {

        (*comparison)++;

       

        // a comparison ahead

        if (arr[j] <= pivot)

        {

            // increment smaller element index

            i++;

           

            // swap the contents of i and j position

            double temp=arr[i];

            arr[i]=arr[j];

            arr[j]=temp;

        }

    }

   

    double temp = arr[i + 1];

    arr[i+1] = arr[r];

    arr[r] = temp;

   

    return i + 1;

}

Sample Output

erage : 120.00000 0

Add a comment
Know the answer?
Add Answer to:
Write a c program to implement Quicksort for arrays of length 16, as described in the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Implement quicksort and bucket sort. Use the code in your book to help; the partition in...

    Implement quicksort and bucket sort. Use the code in your book to help; the partition in quicksort is tricky. Make sure your implementations are correct — it is easy to gain some confidence in the correctness of your code by writing a program which creates arrays filled with random numbers, sorts them, and then checks that they are sorted. Then time your code (using clock() or similar methods) on both methods for arrays filled with random integers of the following...

  • C programming Strictly - Write a program to sort an array of integers via arrays of...

    C programming Strictly - Write a program to sort an array of integers via arrays of pointers to those integers as shown in the figure. Problem 1 (68 points): Write a program to sort an array of integers via arrays of pointers to those integers as shown in the figure. The code should contain the following functions: 1)to randomly generate an array of integer numbers; 2) to allocate memory and build the arrays of pointers as shown in the figure...

  • 1. Write a Java program to implement Counting Sort and write a driver to test it....

    1. Write a Java program to implement Counting Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 2. Write a Java program to implement Bucket Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 3. In...

  • Write a C++ program that simulates a lottery game. Your program should use functions and arrays....

    Write a C++ program that simulates a lottery game. Your program should use functions and arrays. Define two global constants: - ARRAY_SIZE that stores the number of drawn numbers (for example 5) -MAX_RANGE that stores the highest value of the numbers ( for example 9 ) The program will use an array of five integers named lottery, and should generate a random number in the range of 0 through 9 for each element of the array. The user should enter...

  • Write a C program that inputs 5 elements into each of 2 integer arrays. Add corresponding...

    Write a C program that inputs 5 elements into each of 2 integer arrays. Add corresponding array elements, that array1 [0] + array2[0], etc. Save the sum into a third array called sumArray[]. Display the sum array. Submit your and the input and output of the complete execution.

  • Implement and compare sorting algorithms. The task is to sort a list of integers using 5...

    Implement and compare sorting algorithms. The task is to sort a list of integers using 5 sorting algorithms: selection sort insertion sort merge sort heap sort quicksort Your program should include 5 separate sorting methods, though it is fine for them to call some common methods (like "swap") if needed. Each sorting method should also count the number of comparison operations and assignment operations on the array elements during the sorting process. In the main program, two types of array...

  • ** C++ LANGUAGE ** Write a function that will implement each Fibonacci number with the help...

    ** C++ LANGUAGE ** Write a function that will implement each Fibonacci number with the help of an integer array of size 100 (elements of this array will be digits of the Fibonacci number). When the function is called to find , it will calculate all Fibonacci numbers from to using the basic formula . To add two Fibonacci numbers, the function will add elements of two arrays corresponding to and and store their sums in the array corresponding to...

  • Using MATLAB 15. Write a program that: a. b. c. Accept two numbers n and m...

    Using MATLAB 15. Write a program that: a. b. c. Accept two numbers n and m in the range of 1-6 Define three arrays A, B and Cof size n by m. (n-# of rows, m-H of columns) Fill the elements of array A by A(ij)-itj i-5 d. Fill the elements of array B by B(ij)ij e. Fill the elements of array C with the average of the corresponding elements of A f. g. h. and B Print the results...

  • Write in C++ (Bubble Sort) implement the bubble sort algorithm - another simple yet inefficient s...

    Write in C++ (Bubble Sort) implement the bubble sort algorithm - another simple yet inefficient sorting technique. its called bubble sort or sinking sort because smaller values gradually "bubble" their way to the top of the array like air bubbles rising in water, while the larger values sink to the bottom of the array. the technique uses nested loops to make several passes through the array. each pass compares successive pairs of elements. if a pair is in increasing order,...

  • Write a program in Java to implement the max-priority queue using max-heap data structure. Implement the...

    Write a program in Java to implement the max-priority queue using max-heap data structure. Implement the max-heap data structure using an integer array of 10 cells. (Do not use Java in-built PriorityQueue class.) [In a max-heap, the root node and the intermediate node vales are always greater than their children.] First, take 10 integer values from the user and insert them in the max-priority queue. Then print the elements of the queue. After that, delete two elements from the queue...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT