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
2. As you have learned, ironically, a large part of sound production involves visual perception. How easy or difficult did you f
Alex787 [66]

Answer: it is very easy to work with programs such as audacity, they are real game changers. Also, they are very helpful for editing and recording audio. They could make audacity’s auto tune more beginner friendly

3 0
2 years ago
Lukas entered the date 9-17-2013 in an Excel workbook. He wants the date to appears as “Tuesday, September 17, 2013.” Instead of
Sphinxa [80]
<span>The answer is highlight cells, select the Insert tab, click on the number, select Date from the category box, highlight the correct format, and click OK.</span>
8 0
3 years ago
When you sustain program implementation by staying true to the original design, it is termed A. Goals and objectives B. Program
Naya [18.7K]

Answer:

Program fidelity

Explanation:

7 0
2 years ago
What is your understanding about the subject “digital image processing”?
N76 [4]

Answer:

Here's your answer mate.....☺️☺️

Explanation:

In computer science, digital image processing is the use of a digital computer to process digital images through an algorithm. ... It allows a much wider range of algorithms to be applied to the input data and can avoid problems such as the build-up of noise and distortion during processing.

<em><u>Hope it helps </u></em>☺️

5 0
3 years ago
Martha has a large data list and wants to sort it quickly and efficiently. Which algorithm should Martha use
Elena-2011 [213]

Answer:

quicksort

Explanation:

There are many types of asymptotically efficient sorting algorithms that can be used but one of the more commonly used for large data lists would be quicksort. This is a sorting algorithm that focuses on choosing a value from the list and working around that value in order to sort the data piece by piece. For larger data sets this method is widely used due to its speed and efficiency which is exactly what Martha needs in this scenario.

4 0
3 years ago
Other questions:
  • Sean is white hat hacker, who is demonstrating a network level session hijack attacks and as part of it, he has injected malicio
    6·1 answer
  • A medium is a:
    13·2 answers
  • What are the most popular/up-and-coming social media applications?
    10·2 answers
  • Students are studying the effects of beach pollution by counting populations of seagulls at two different beach locations. One l
    7·1 answer
  • What are the core scripting and coding technolies used to build ai platforms?
    14·1 answer
  • An element in a web page that connects to a different location in the same page or a different page is a _____.
    8·1 answer
  • The first step to accurate coding is to identify the ___________ in the diagnostic statement.
    7·1 answer
  • Letter only ^_____^!
    8·1 answer
  • A state government is attempting to reduce the digital divide. Which of the following activities has the greatest potential to c
    14·1 answer
  • Jill is configuring the AutoArchive feature in Outlook 2016. What is the default setting in relation to when AutoArchive
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!