Question

Task 3: Functions or classes are ok. Create an array that holds 1000 random floats between...

Task 3:

Functions or classes are ok.

Create an array that holds 1000 random floats between 1-1000.

Implement Quick Sort to sort the values in ascending order. Least → Greatest

Measure the time it takes to execute the sort in milliseconds.

Please run the sort 3 times.

Attach Photos of Source Code and Output

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

#include <iostream>

#include <cstdlib>

#include <ctime>

#include <iomanip>

#include <stdio.h>

using namespace std;

void swap(float* a, float* b)

{

float t = *a;

*a = *b;

*b = t;

}

int partition (float arr[], int low, int high)

{

float pivot = arr[high]; // pivot

int i = (low - 1); // Index of smaller element

for (int j = low; j <= high- 1; j++)

{

if (arr[j] <= pivot)

{

i++; // increment index of smaller element

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

}

}

swap(&arr[i + 1], &arr[high]);

return (i + 1);

}

void quickSort(float arr[], int low, int high)

{

if (low < high)

{

/* pi is partitioning index, arr[p] is now

at right place */

int pi = partition(arr, low, high);

// Separately sort elements before

// partition and after partition

quickSort(arr, low, pi - 1);

quickSort(arr, pi + 1, high);

}

}

/* Function to print an array */

void printArray(float arr[], int size)

{

int i;

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

printf("%.2f\n", arr[i]);

printf("\n");

}

int main() {

int size = 1000;

float arr[size];

int i=0;

int low = 1, high = 1000;

srand(time(0));

while (i < size)

{

arr[i] = (float)rand()/(float)(RAND_MAX/1000.0);

i++;

}

cout<<"\n\nArray Content before sorting: \n";

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

// cout<<std::fixed<<setprecision(2)<<arr[i]<<" ";

// }

int start_s=clock();

quickSort(arr, 0,size);

int stop_s=clock();

cout << "\nTime taken: " << (stop_s-start_s)/double(CLOCKS_PER_SEC)*1000 << endl;

cout<<"\n\nArray Content after sorting: \n";

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

// cout<<std::fixed<<setprecision(2)<<arr[i]<<" ";

// }

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Task 3: Functions or classes are ok. Create an array that holds 1000 random floats between...
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
  • do in C++ language thank you Task 4: Functions or classes are ok. Please read data...

    do in C++ language thank you Task 4: Functions or classes are ok. Please read data into program from descending_mostly_sorted.txt and store into an array. Implement Selection Sort to sort the values in ascending order. Least → Greatest Measure the time it takes to execute the sort in milliseconds or even nanoseconds. Please run the sort 3 times. Attach Photos of Source Code and Output Task 5: Functions or classes are ok. Please read data into program from descending_mostly_sorted.txt and...

  • Please read data into a program from ascending_mostly_sorted.txt (a text file containing integers that are mostly...

    Please read data into a program from ascending_mostly_sorted.txt (a text file containing integers that are mostly in ascending order) and store into an array. Implement Insertion Sort to sort the values in ascending order. Least → Greatest Measure the time it takes to execute the sort in milliseconds or even nanoseconds. IN c++ Please run the sort 3 times.

  • My following program has an array which holds 1000 random integers between 1-1000. Now I need...

    My following program has an array which holds 1000 random integers between 1-1000. Now I need help to create an array that holds 10,000 random integer between 1-1000 in my following program. The main goal of this program is time analysis by using bubble sort and binary search algorithms. Please do the following task; 1. Replace the 1000 random integers with 10,000 random integers After change please answer the following question 2. what will be happen, if an array holds...

  • Task 1 Main Function: Declare and fill an array with 1000 random integers between 1 -...

    Task 1 Main Function: Declare and fill an array with 1000 random integers between 1 - 1000. You will pass the array into functions that will perform operations on the array. Function 1: Create a function that will output all the integers in the array. (Make sure output is clearly formatted. Function 2: Create a Function that will sum and output all the odd numbers in the array. Function 3: Create a Function that will sum and output all the...

  • Please Use C++ Language. And Note that please donot post the answer already there Because there...

    Please Use C++ Language. And Note that please donot post the answer already there Because there is similar answer code but I need with modified Quick sort algorithm ​. Update your program from ... Create an array that holds 1000 random integers between 1-1000. Allow the user to enter an integer to search. Create and implement modified Quick sort algorithm which will sort the array before the Binary Search algorithm is executed. Create and implement a Binary Search Algorithm ....

  • PART 1  Initialize the array from the Values.txt Mostly Sorted Descending. (VALUES ARE GIVEN BELOW) (YOU CAN...

    PART 1  Initialize the array from the Values.txt Mostly Sorted Descending. (VALUES ARE GIVEN BELOW) (YOU CAN COPY AND PASTE THE ARRAY VALUES INTO YOUR PROGRAM) Task 1: Functions or classes are ok. Initialize the array from the Values.txt Mostly Sorted Descending. Implement Selection sort to sort the values in ascending order. Least → Greatest Measure the time it takes to execute the sort in milliseconds. Please run the sort 3 times. Attach Photos of Source Code and Output Task 2:...

  • Purpose: Demonstrate the ability to create and manipulate classes, data members, and member functions. This assignment...

    Purpose: Demonstrate the ability to create and manipulate classes, data members, and member functions. This assignment also aims at creating a C++ project to handle multiple files (one header file and two .cpp files) at the same time. Remember to follow documentation and variable name guidelines. Create a C++ project to implement a simplified banking system. Your bank is small, so it can have a maximum of 100 accounts. Use an array of pointers to objects for this. However, your...

  • Note: None of these functions should use cout. It is OK to use cout while debugging...

    Note: None of these functions should use cout. It is OK to use cout while debugging to check how your function is working, but your final submission should not contain cout in any function but main. Head ==== Name the source file for this section head.cpp. Write a function named "head" which takes an array of integers as an argument, and returns the first element in the array. Write a function named main which outputs the result of testing your...

  • Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Jav...

    Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Java program provided: // Student Name Today's Date import java.util.Arrays; import java.util.Random; public class SortTimer {    // Please expand method main() to meet the lab requirements.       // You have the following sorting methods available:    // insertionSort(int[] a);    // selectionSort(int[] a);    // mergeSort(int[] a);    // quickSort(int[] a);    // The array will be in sorted order after the routines are called!   ...

  • Comparison of Sorting Algorithms You are required to implement all the sorting algorithms (Bubble, Selection, Insertion...

    Comparison of Sorting Algorithms You are required to implement all the sorting algorithms (Bubble, Selection, Insertion, Quick, Merge). Take help from lecture slides and welb . You will then create arrays of different sizes as instructed below, test each algorithm on each array and record the execution times of each individual algorithm on each array. . You will report these execution times in a table and then write a report explaining the execution times of the sorting algorithms according to...

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