Question

data strutures in c++ please implement the QuickSort algorithm as a C++ function. Use it to...

data strutures in c++ please

implement the QuickSort algorithm as a C++ function. Use it to sort a large number of integers, say at least 100000. Do a timing test on the sort. Then use the build-in sort() function in to sort the same data and time this sort. Which sort is better? Do some research to see if you can determine what algorithm the built-in sort() function uses and describe this sort if it is different than QuickSort.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

from c++ 11 onwards there is an chrono library which is used to measure the time of running time of functions

#include <bits/stdc++.h>
#include<chrono>
#define SIZE 100000
using namespace std;
using namespace std::chrono;

int part(int A[],int start,int end)
{
   int i=start+1;
   int piv=A[start];//make the first element as pivot element.
   /*rearrange the array by putting elements which are less than pivot
on one side and which are greater that on other. */
   for(int j=start+1;j<=end;j++)
   {
       if(A[j]<piv)
       swap(A[i],A[j]),i++;
   }
   swap(A[start],A[i-1]);//put the pivot element in its proper place.
   return i-1;//return postion of pivot
}
void quick_sort(int A[],int start,int end)
{
   if(start<end)
   {
       //stores the position of pivot element
       int piv=part(A,start,end);
       quick_sort(A,start,piv-1);
       quick_sort(A,piv+1,end);
   }
}
void in_built_sort(int A[],int n)
{
   sort(A,A+n);
}
int main() {
   //make a array of size 100000
   int a[SIZE],b[SIZE];
   //fill the arrays with random values
   for(int i=0;i<SIZE;i++)
   {
       a[i]=rand();
       b[i]=rand();
   }
   //hih_resolution is used to calculate the running time of a function
   high_resolution_clock::time_point t1 = high_resolution_clock::now();
   quick_sort(a,0,SIZE-1);
   high_resolution_clock::time_point t2 = high_resolution_clock::now();
   //duration is running time of quick sort in ms
   auto duration = duration_cast<microseconds>( t2 - t1 ).count();
   cout<<"duration of quick sort is "<<duration<<"ms"<<endl;
   //measure the running time of inbuilt sort function
   high_resolution_clock::time_point t3 = high_resolution_clock::now();
   in_built_sort(b,SIZE);
   high_resolution_clock::time_point t4 = high_resolution_clock::now();
   duration = duration_cast<microseconds>( t4 - t3 ).count();
   cout<<"duration of inbuilt sort is "<<duration<<"ms"<<endl;
   return 0;
}

the running time of standard sort function is better

standard sort function uses intro -sort which is hybrid sort of quick sort,heap sort and insertion sort

Add a comment
Know the answer?
Add Answer to:
data strutures in c++ please implement the QuickSort algorithm as a C++ function. Use it to...
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
  • data structures c++ please implement the QuickSort algorithm as a C++ function. Use it to sort...

    data structures c++ please implement the QuickSort algorithm as a C++ function. Use it to sort a large number of integers, say at least 100000. Do a timing test on the sort. Then use the build-in sort() function in to sort the same data and time this sort. Which sort is better? Do some research to see if you can determine what algorithm the built-in sort() function uses and describe this sort if it is different than QuickSort.

  • c++ data structures please Pick any inefficient sorting algorithm you want, selection, bubble, insertion, etc., and...

    c++ data structures please Pick any inefficient sorting algorithm you want, selection, bubble, insertion, etc., and implement it as a function. Using the system clock as a timer, determine when the efficiency of the algorithm breaks down. For example, does it become slow after 1000 elements, 10000 elements, 100000 elements? Write some code to figure this out. Then use the sort() algorithm that is part of the STL <algorithm> library. How does this function compare to the function you implemented?...

  • Implement quicksort and bucket sort. Use the code in your book to help; the partition in...

    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...

  • You are going to implement Treesort algorithm in C++ to sort string data. Here are the...

    You are going to implement Treesort algorithm in C++ to sort string data. Here are the steps to complete the homework 1) Use the following class definition for binary search tree nodes. Its constructor is incomplete you should first complete the constructor. class TreeNode t public: string data; / this is the string stored in the node TreeNode left: TreeNode right; TreeNode (string element, TreeNode 1t, TreeNode rt //your code here 2) Write a function that will insert a string...

  • create a file homework_part_1.c a) Implement the function initialize_array that receives two parameters: an array of...

    create a file homework_part_1.c a) Implement the function initialize_array that receives two parameters: an array of integers and the array size. Use a for loop and an if statement to put 0s in the odd positions of the array and 5s in the even positions. You must use pointers to work with the array. Hint: review pointers as parameters. b) Implement the function print_array that receives as parameters an array of integers and the array size. Use a for statements...

  • Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over...

    Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over an array of integer numbers and size n. The function should return the index of the search key if the search key exists and return - 1 if the search key doesn't exist. [10 Points] Q2] Write a C function to implement the selection sort algorithm, to sort an array of float values and size n. The function should sort the array in ascending...

  • This assignment will test your algorithm/logic development in which you will perform the sort function with some different characteristics.

    This assignment will test your algorithm/logic development in which you will perform the sort function with some different characteristics. You will be required to ensure that your program meets the following requirements: i.    Existence of an array which can accept an unknown number of positive integersii.    ExistenceofafunctionwhichcanperformadifferentsortwheretheEvennumberswillappear first before the Odd numbers. In this sorted sequence, all the even numbers will appear first in descending order followed by the odd numbers (in the same order). You are only allowed to use one...

  • In below C++ sort.cpp 1- Implement the insertion_sort function. 2- Implement the compareSensorPtr function and the...

    In below C++ sort.cpp 1- Implement the insertion_sort function. 2- Implement the compareSensorPtr function and the code in main to create and sort the array of pointers. The places to make modifications are indicated by TODO: comments. You should not have to make modifications anywhere else. 3- what's big O and runtime for 100000 items. #include <iostream> #include <algorithm> #include <numeric> #include <vector> #include <string> #include <cstdlib> #include <cassert> using namespace std; // Set this to false to skip the...

  • This is a beginner's C++ class. Please use basic C++ knowledge or approaches provided in the...

    This is a beginner's C++ class. Please use basic C++ knowledge or approaches provided in the first 10 chapters of "starting out with >>>C++ From Control Structures through Objects 9th Edition". I will leave a thumbs up rate if the code works and done quickly. Thank you. Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in ascending order....

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