1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
scoray [572]
3 years ago
7

g Write a program to sort an array of 100,000 random elements using quicksort as follows: Sort the arrays using pivot as the mid

dle element of the array Sort the arrays using pivot as the median of the first, last, and middle elements of the array Sort the arrays using pivot as the middle element of the array. However,, when the size of any sub-list reduces to less than 20, sort the sub-list using insertion sort. Sort the array using pivot as the median of the first, last and middle elements of the array. When the size of any sub-list reduces to less than 20, sort the sub-list using insertion sort. Calculate and display the CPU time for each of the preceding four steps.
Computers and Technology
1 answer:
shtirl [24]3 years ago
7 0

Answer:

header.h->function bodies and header files.

#include<iostream>

#include<cstdlib>

#include<ctime>

using namespace std;

/* Partitioning the array on the basis of piv value. */

int Partition(int a[], int bot, int top,string opt)

{

int piv, ind=bot, i, swp;

/*Finding the piv value according to string opt*/

if(opt=="Type1 sort" || opt=="Type3 sort")

{

piv=(top+bot)/2;

}

else if(opt=="Type2 sort" || opt=="Type4 sort")

{

piv=(top+bot)/2;

if((a[top]>=a[piv] && a[top]<=a[bot]) || (a[top]>=a[bot] && a[top]<=a[piv]))

piv=top;

else if((a[bot]>=a[piv] && a[bot]<=a[top]) || (a[bot]>=a[top] && a[bot]<=a[piv]))

piv=bot;

}

swp=a[piv];

a[piv]=a[top];

a[top]=swp;

piv=top;

/*Getting ind of the piv.*/

for(i=bot; i < top; i++)

{

if(a[i] < a[piv])

{

swp=a[i];

a[i]=a[ind];

a[ind]=swp;

ind++;

}

}

swp=a[piv];

a[piv]=a[ind];

a[ind]=swp;

return ind;

}

void QuickSort(int a[], int bot, int top, string opt)

{

int pindex;

if((opt=="Type3 sort" || opt=="Type4 sort") && top-bot<19)

{

/*then insertion sort*/

int swp,ind;

for(int i=bot+1;i<=top;i++){

swp=a[i];

ind=i;

for(int j=i-1;j>=bot;j--){

if(swp<a[j]){

a[j+1]=a[j];

ind=j;

}

else

break;

}

a[ind]=swp;

}

}

else if(bot < top)

{

/* Partitioning the array*/

pindex =Partition(a, bot, top,opt);

/* Recursively implementing QuickSort.*/

QuickSort(a, bot, pindex-1,opt);

QuickSort(a, pindex+1, top,opt);

}

return ;

}

main.cpp->main driver file

#include "header.h"

int main()

{

int n=100000, i;

/*creating randomized array of 100000 numbers between 0 and 100001*/

int arr[n];

int b[n];

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

arr[i]=(rand() % 100000) + 1;

clock_t t1,t2;

t1=clock();

QuickSort(arr, 0, n-1,"Type1 sort");

t2=clock();

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

arr[i]=b[i];

cout<<"Quick sort time, with pivot middle element:"<<(t2 - t1)*1000/ ( CLOCKS_PER_SEC );

cout<<"\n";

t1=clock();

QuickSort(arr, 0, n-1,"Type2 sort");

t2=clock();

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

arr[i]=b[i];

cout<<"Quick sort time, with pivot median element:"<<(t2 - t1)*1000/ ( CLOCKS_PER_SEC );

cout<<"\n";

t1=clock();

QuickSort(arr, 0, n-1,"Type3 sort");

t2=clock();

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

arr[i]=b[i];

cout<<"Quick sort time and insertion sort time, with pivot middle element:"<<(t2 - t1)*1000/ ( CLOCKS_PER_SEC );

cout<<"\n";

t1=clock();

QuickSort(arr, 0, n-1,"Type4 sort");

t2=clock();

cout<<"Quick sort time and insertion sort time, with pivot median element:"<<(t2 - t1)*1000/ ( CLOCKS_PER_SEC );

return 0;

}

Explanation:

Change the value of n in the main file for different array size. Output is in the same format as mentioned, time is shown in milliseconds.

You might be interested in
How do i know when someone answered my questions and where can i check what they wrote?
maksim [4K]

Answer:

U can go to your profile, then go to your question to see what they have written.

Explanation: Hope this helps

3 0
3 years ago
Whats the answer to this question?
Naddika [18.5K]
Eat a a dong dong edi35(
6 0
3 years ago
Write pseudo code that performs the following: Ask a user to enter a number. If the
CaHeK987 [17]
Answer:
BEGIN
INPUT N
IF N>0 AND N<10 THEN
OUTPUT "blue"
ELSE
IF N>10 AND N<20 THEN
OUTPUT "red"
ELSE
IF N>20 AND N<30 THEN
OUTPUT "green"
ELSE
OUTPUT "It is not a correct color option"
ENDIF
END.

Explanation:

3 0
1 year ago
In the early days of photography, cameras were limited to professional photographers because of the knowledge needed to work the
ki77a [65]
True  ------------------------------------------
8 0
3 years ago
Read 2 more answers
write a c++ program that reads from the user a positive integer (in a decimal representation), and prints its binary (base 2) re
Vesna [10]

Answer:

The program to this question can be described as follows:

Program:

#include <iostream> //defining header file

using namespace std;

int main() //defining main method

{

int x1,rem,i=1; //defining integer variable

long Num=0;// defining long variable

cout << "Input a decimal number: "; // print message

cin >> x1; //input value by user

while (x1!=0) //loop for calculate binary number

{

//calculating binary value    

rem= x1%2;

x1=x1/2;

Num =Num +rem*i;//holding calculate value

i=i*10;

}

cout <<Num;//print value

return 0;

}

Output:

Input a decimal number: 76

1001100

Explanation:

In the above code, four variable "x1, rem, i, and Num" is declared, in which three "x1, rem, and i" is an integer variable, and one "Num" is a long variable.

  • In the x1 variable, we take input by the user and use the while loop to calculate its binary number.
  • In the loop, first, we check x1 variable value is not equal to 0, inside we calculate it binary number that store in long "Num" variable, after calculating its binary number the print method "cout" is used to prints its value.  
5 0
3 years ago
Other questions:
  • What type of data visual would you use to illustrate trends over time? Gantt chart Bar graph Line chart Scatter diagrams
    5·1 answer
  • Your motherboard supports dual channeling and you currently have two slots used in channel a on the board; each module holds 1 g
    15·1 answer
  • What does this image represent?
    9·2 answers
  • Some data files should be totally hidden from view, while others should have ____ so users can view, but not change, the data.
    11·1 answer
  • Solve(-8/3)+7/5 please answer​
    5·1 answer
  • Reading (BCK FORM 2C IT 2020-2021)
    12·2 answers
  • Help its in binary and im a small brain for that
    7·1 answer
  • Please help I need help with web technology I forgot to post the question in the last one. here it is tho.
    9·1 answer
  • Name 3 things that you use daily that are considered computers?
    6·1 answer
  • Examine the following output:
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!