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
Mumz [18]
4 years ago
14

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

e 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. Example of the median of the first, last and middle elements: 1 2 3 4 5 6 7 8 0 (median of 1, 5, 0 is 1) 8 0 1 2 3 4 5 6 7 (median of 8, 3, 7 is 7) To calculate the CPU time, use the header , and clock_t type. Depends on the CPU of your computer, your number would not be the same as in the sample output below.
Computers and Technology
2 answers:
Stels [109]4 years ago
8 0

Answer:

Check the explanation

Explanation:

#include<iostream.h>

#include<algorithm.h>

#include<climits.h>

#include<bits/stdc++.h>

#include<cstring.h>

using namespace std;

int partition(int arr[], int l, int r, int k);

int kthSmallest(int arr[], int l, int r, int k);

void quickSort(int arr[], int l, int h)

{

if (l < h)

{

// Find size of current subarray

int n = h-l+1;

 

// Find median of arr[].

int med = kthSmallest(arr, l, h, n/2);

 

// Partition the array around median

int p = partition(arr, l, h, med);

 

// Recur for left and right of partition

quickSort(arr, l, p - 1);

quickSort(arr, p + 1, h);

}

int findMedian(int arr[], int n)

{

sort(arr, arr+n); // Sort the array

return arr[n/2]; // Return middle element

}

int kthSmallest(int arr[], int l, int r, int k)

{

// If k is smaller than number of elements in array

if (k > 0 && k <= r - l + 1)

{

int n = r-l+1; // Number of elements in arr[l..r]

 

// Divide arr[] in groups of size 5, calculate median

// of every group and store it in median[] array.

int i, median[(n+4)/5]; // There will be floor((n+4)/5) groups;

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

median[i] = findMedian(arr+l+i*5, 5);

if (i*5 < n) //For last group with less than 5 elements

{

median[i] = findMedian(arr+l+i*5, n%5);

i++;

}

int medOfMed = (i == 1)? median[i-1]:

kthSmallest(median, 0, i-1, i/2);

int pos = partition(arr, l, r, medOfMed);

if (pos-l == k-1)

return arr[pos];

if (pos-l > k-1) // If position is more, recur for left

return kthSmallest(arr, l, pos-1, k);

return kthSmallest(arr, pos+1, r, k-pos+l-1);

}

return INT_MAX;

}

void swap(int *a, int *b)

{

int temp = *a;

*a = *b;

*b = temp;

}

int partition(int arr[], int l, int r, int x)

{

// Search for x in arr[l..r] and move it to end

int i;

for (i=l; i<r; i++)

if (arr[i] == x)

break;

swap(&arr[i], &arr[r]);

 

// Standard partition algorithm

i = l;

for (int j = l; j <= r - 1; j++)

{

if (arr[j] <= x)

{

swap(&arr[i], &arr[j]);

i++;

}

}

swap(&arr[i], &arr[r]);

return i;

}

 

/* Function to print an array */

void printArray(int arr[], int size)

{

int i;

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

cout << arr[i] << " ";

cout << endl;

}

 

// Driver program to test above functions

int main()

{

float a;

clock_t time_req;

int arr[] = {1000, 10, 7, 8, 9, 30, 900, 1, 5, 6, 20};

int n = sizeof(arr)/sizeof(arr[0]);

quickSort(arr, 0, n-1);

cout << "Sorted array is\n";

printArray(arr, n);

time_req = clock();

for(int i=0; i<200000; i++)

{

a = log(i*i*i*i);

}

time_req = clock()- time_req;

cout << "Processor time taken for multiplication: "

<< (float)time_req/CLOCKS_PER_SEC << " seconds" << endl;

 

// Using pow function

time_req = clock();

for(int i=0; i<200000; i++)

{

a = log(pow(i, 4));

}

time_req = clock() - time_req;

cout << "Processor time taken in pow function: "

<< (float)time_req/CLOCKS_PER_S

return 0;

}

..................................................................................................................................................................................................................................................................................................................................

OR

.......................

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

 

// Swap utility

void swap(long int* a, long int* b)

{

int tmp = *a;

*a = *b;

*b = tmp;

}

 

// Bubble sort

void bubbleSort(long int a[], long int n)

{

for (long int i = 0; i < n - 1; i++) {

for (long int j = 0; j < n - 1 - i; j++) {

if (a[j] > a[j + 1]) {

swap(&a[j], &a[j + 1]);

}

}

}

}

 

// Insertion sort

void insertionSort(long int arr[], long int n)

{

long int i, key, j;

for (i = 1; i < n; i++) {

key = arr[i];

j = i - 1;

 

// Move elements of arr[0..i-1], that are

// greater than key, to one position ahead

// of their current position

while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

j = j - 1;

}

arr[j + 1] = key;

}

}

 

// Selection sort

void selectionSort(long int arr[], long int n)

{

long int i, j, midx;

 

for (i = 0; i < n - 1; i++) {

 

// Find the minimum element in unsorted array

midx = i;

 

for (j = i + 1; j < n; j++)

if (arr[j] < arr[min_idx])

midx = j;

 

// for plotting graph with integer values

printf("%li, %li, %li, %li\n",

n,

(long int)tim1[it],

(long int)tim2[it],

(long int)tim3[it]);

 

// increases the size of array by 10000

n += 10000;

}

 

return 0;

}

Ivahew [28]4 years ago
8 0

Answer:

See explaination

Explanation:

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

// Swap utility

void swap(long int* a, long int* b)

{

int tmp = *a;

*a = *b;

*b = tmp;

}

// Bubble sort

void bubbleSort(long int a[], long int n)

{

for (long int i = 0; i < n - 1; i++) {

for (long int j = 0; j < n - 1 - i; j++) {

if (a[j] > a[j + 1]) {

swap(&a[j], &a[j + 1]);

}

}

}

}

// Insertion sort

void insertionSort(long int arr[], long int n)

{

long int i, key, j;

for (i = 1; i < n; i++) {

key = arr[i];

j = i - 1;

// Move elements of arr[0..i-1], that are

// greater than key, to one position ahead

// of their current position

while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

j = j - 1;

}

arr[j + 1] = key;

}

}

// Selection sort

void selectionSort(long int arr[], long int n)

{

long int i, j, midx;

for (i = 0; i < n - 1; i++) {

// Find the minimum element in unsorted array

midx = i;

for (j = i + 1; j < n; j++)

if (arr[j] < arr[min_idx])

midx = j;

// Swap the found minimum element

// with the first element

swap(&arr[midx], &arr[i]);

}

}

// Driver code

int main()

{

long int n = 10000;

int it = 0;

// Arrays to store time duration

// of sorting algorithms

double tim1[10], tim2[10], tim3[10];

printf("A_size, Bubble, Insertion, Selection\n");

// Performs 10 iterations

while (it++ < 10) {

long int a[n], b[n], c[n];

// generating n random numbers

// storing them in arrays a, b, c

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

long int no = rand() % n + 1;

a[i] = no;

b[i] = no;

c[i] = no;

}

// using clock_t to store time

clock_t start, end;

// Bubble sort

start = clock();

bubbleSort(a, n);

end = clock();

tim1[it] = ((double)(end - start));

// Insertion sort

start = clock();

insertionSort(b, n);

end = clock();

tim2[it] = ((double)(end - start));

// Selection sort

start = clock();

selectionSort(c, n);

end = clock();

tim3[it] = ((double)(end - start));

// type conversion to long int

// for plotting graph with integer values

printf("%li, %li, %li, %li\n",

n,

(long int)tim1[it],

(long int)tim2[it],

(long int)tim3[it]);

// increases the size of array by 10000

n += 10000;

}

return 0;

}

You might be interested in
In this lab you will write a program that simulates a mouse in a maze. The maze will have one exit location. The mouse will star
sergij07 [2.7K]

Answer:

/* C/C++ program to solve Rat in a Maze problem using  

backtracking */

#include <stdio.h>  

// Maze size  

#define N 4  

bool solveMazeUtil(int maze[N][N], int x, int y, int sol[N][N]);  

/* A utility function to print solution matrix sol[N][N] */

void printSolution(int sol[N][N])  

{  

for (int i = 0; i < N; i++) {  

 for (int j = 0; j < N; j++)  

  printf(" %d ", sol[i][j]);  

 printf("\n");  

}  

}  

/* A utility function to check if x, y is valid index for N*N maze */

bool isSafe(int maze[N][N], int x, int y)  

{  

// if (x, y outside maze) return false  

if (x >= 0 && x < N && y >= 0 && y < N && maze[x][y] == 1)  

 return true;  

return false;  

}  

/* This function solves the Maze problem using Backtracking. It mainly  

uses solveMazeUtil() to solve the problem. It returns false if no  

path is possible, otherwise return true and prints the path in the  

form of 1s. Please note that there may be more than one solutions,  

this function prints one of the feasible solutions.*/

bool solveMaze(int maze[N][N])  

{  

int sol[N][N] = { { 0, 0, 0, 0 },  

    { 0, 0, 0, 0 },  

    { 0, 0, 0, 0 },  

    { 0, 0, 0, 0 } };  

if (solveMazeUtil(maze, 0, 0, sol) == false) {  

 printf("Solution doesn't exist");  

 return false;  

}  

printSolution(sol);  

return true;  

}  

/* A recursive utility function to solve Maze problem */

bool solveMazeUtil(int maze[N][N], int x, int y, int sol[N][N])  

{  

// if (x, y is goal) return true  

if (x == N - 1 && y == N - 1) {  

 sol[x][y] = 1;  

 return true;  

}  

// Check if maze[x][y] is valid  

if (isSafe(maze, x, y) == true) {  

 // mark x, y as part of solution path  

 sol[x][y] = 1;  

 /* Move forward in x direction */

 if (solveMazeUtil(maze, x + 1, y, sol) == true)  

  return true;  

 /* If moving in x direction doesn't give solution then  

 Move down in y direction */

 if (solveMazeUtil(maze, x, y + 1, sol) == true)  

  return true;  

 /* If none of the above movements work then BACKTRACK:  

  unmark x, y as part of solution path */

 sol[x][y] = 0;  

 return false;  

}  

return false;  

}  

// driver program to test above function  

int main()  

{  

int maze[N][N] = { { 1, 0, 0, 0 },  

    { 1, 1, 0, 1 },  

    { 0, 1, 0, 0 },  

    { 1, 1, 1, 1 } };  

solveMaze(maze);  

return 0;  

}  

3 0
3 years ago
Hello join please I would be so happy if y’all did
Luda [366]

Answer:

i bet its a class raid

Explanation:

its gonna be a class raid

8 0
3 years ago
Read 2 more answers
Which statement is true?
Klio2033 [76]

Answer:

You need to import the deque methods in order to use a deque.

Explanation:

the correct option is B

You need to import the deque methods in order to use a deque.

Please mark me as brainliest

5 0
3 years ago
Read 2 more answers
Which method allows you to choose how to display the date and time?
Alja [10]

FORMAT Is the Answer Hope it helps:)

8 0
3 years ago
Which view allows the user to see the source code and the visual representation simultaneously? to see the source code and the v
Ganezh [65]

Answer:

Split view.

Explanation:

HTML is an acronym for hypertext markup language and it is a standard programming language which is used for designing, developing and creating web pages.

Generally, all HTML documents are divided into two (2) main parts; body and head. The head contains information such as version of HTML, title of a page, metadata, link to custom favicons and CSS etc. The body of the HTML document contains the contents or informations of a web page to be displayed.

Split view allows the user to see the source code and the visual representation simultaneously.

It can be used in graphic design software such as Adobe Dreamweaver to get a side-by-side view of both the source code and code layout modules or design and source code.

For example, a user with a dual screen monitor can use the split view feature to display the design on monitor while using the other to display the source code.

3 0
3 years ago
Other questions:
  • To track website behavior data with google analytics, which steps will you need to complete?
    14·1 answer
  • Which option should u select to ignore all tracked changes in a document
    8·1 answer
  • I accidentally pressed the challenge button while I was scrolling through. I didn't mean to press that challenge, instead of the
    11·2 answers
  • Which of the following statements best explains how the high-gain antenna in Voyager probes has made outer space exploration eas
    15·1 answer
  • Which is the correct APA format in books?​
    10·2 answers
  • Match the expenses to their respective categories.
    6·2 answers
  • A system of cranks and motors to create movement in a mobile was called bebop. cantilevers. pastels. kinetic sculpture.
    6·1 answer
  • In Python, which comparison operator means "greater than"?
    9·2 answers
  • I've been stuck on these two basic javascript assignments for an embarrassingly long time. The teacher refuses to help me and th
    13·1 answer
  • True or false: Securing intellectual property digitally is the only security because that's how all IP is stored.
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!