Question

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.

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

#include<iostream>

#include<cstdlib>

#include <ctime>

#include <bits/stdc++.h>

using namespace std;

void quicksort(int *,int,int);

int partition(int *,int,int);

int main()

{  

    int arr1[100000], arr2[100000];

    int i;

  

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

    {

        int n = rand();

       

        // initialize the array with random numbers

        arr1[i] = n;

        arr2[i] = n;

    }

   

    // store the starting time

    double start = clock();

   

    // sort the array using quickort() function

    quicksort(arr1, 0, 100000 - 1);

    // store the ending time

    double end = clock();

   

    // calculate time taken

    double time_taken = ( end - start ) / double( CLOCKS_PER_SEC ) * 1000;

   

    cout << "Time taken by quicksort function : " << time_taken << endl;

       

    // store the starting time

    start = clock();

       

    // sort the function using sort() function

    sort(arr2, arr2 + 100000);

       

    // store the ending time

    end = clock();

   

    // calculate time taken

    time_taken = ( end - start ) / double( CLOCKS_PER_SEC ) * 1000;

   

    cout << "\nTime taken by inbuilt sort function : " << time_taken << endl;

   

    return 0;

}

// function to sort using quick sort

void quicksort(int *arr,int p,int r)

{

    int q;

   

    if(p<r)

    {

        // create a partition

        q = partition(arr,p,r);

       

        // sort the left subarray

        quicksort(arr,p,q-1);

       

      // sort the right subarray

        quicksort(arr,q+1,r);

    }

}

int partition(int *arr,int p,int r)

{  

    // set pivot

    int pivot = arr[r];

   

    // set the lindex of lower element

    int j, i = p - 1;

    for (j = p; j <= r - 1; j++ )

    {

        if (arr[j] <= pivot)

        {

            // increment smaller element index

            i++;

           

            // swap the contents of i and j position

            int temp=arr[i];

            arr[i]=arr[j];

            arr[j]=temp;

        }

    }

   

    int temp = arr[i + 1];

    arr[i+1] = arr[r];

    arr[r] = temp;

   

    return i + 1;

}


Sample Output

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

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

  • Java - Data Structures Q. Implement one of the sorts ( Selection Sort / Insertion Sort / Shell Sort / Merge Sort ) described in Chapters 8 and 9 . Input is an array with at least 10 items. The items c...

    Java - Data Structures Q. Implement one of the sorts ( Selection Sort / Insertion Sort / Shell Sort / Merge Sort ) described in Chapters 8 and 9 . Input is an array with at least 10 items. The items can be of any type (Suggested types are integers—denoting how many patrons visited the library in the last three weeks or strings—denoting the names of books returned today). Print the original data. Print the sorted data.

  • Data Structures and Algorithm Analysis in C++ by Clifford Shaffer Devise an efficient algorithm to sort...

    Data Structures and Algorithm Analysis in C++ by Clifford Shaffer Devise an efficient algorithm to sort a set of numbers with values in the range 0 to 30, 000. There are no duplicates. Keep memory requirements to a minimum.

  • [Code in C] Help me with this. Not sure what to do. 1 Couting Sort You...

    [Code in C] Help me with this. Not sure what to do. 1 Couting Sort You may have learned some sorting algorithms - such as bubble sort ad quicksort in CS 110 and CS 210. This homework is about counting sort. Let n be the number of elements to be sorted. Bubble sort and quicksort assume tha time, which one is larger and which one is smaller. They make no assumption on the values of the elements t we can...

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

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

  • Java, Please implement the way the interface specifies. Part A:Radix Sort Implement a radix sort as...

    Java, Please implement the way the interface specifies. Part A:Radix Sort Implement a radix sort as described in the last section of Chapter 7 in your text. It should handle variable amounts of data and variable numbers of digits in the key values. Use testing to ensure radix sort works for at least three examples of different input sizes and various max digit length. I need to implement the radix sort using the below java interface. /** * <h1><LeastSignificantDigit Radix...

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