#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

Write a c program to implement Quicksort for arrays of length 16, as described in the...
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 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. 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...
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 this assignment you will investigate three different variants of algorithms that we have discussed in the class to solve the selection problem. Version 1. “Simultaneous minimum and maximum” selection algorithm. Version 2. Randomized selection algorithm. Version 3. “Median of...
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 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.
Write a complete C++ program that analyzes the following sorts by keeping track of how long they take to sort arrays of longS of size 5000, 10000, and 100000, as well as the number of swaps and comparisons they take in doing so: Insertion sort Selection sort Quick sort shell sort(using gaps of n/2, n/4, n/8, ..., 1) for 20, the gaps would be 10, 5, 2, and 1 shell sort(using gaps based on 2^k + 1, but starting at...
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 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...
******ONLY C++****** Objectives: • Appropriate use of arrays • Use of Sorting functions • Analysis of data using conditional structures • Organize code into usable functions Problem: Poker is an easy game to learn and a hard game to win. Write a program that will generate five card hands from a shuffled deck and then keep track of what type of hand it is, providing a count and a percentage at the end. Details: Program Flow: 1. Ask the user...