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
data strutures in c++ please implement the QuickSort algorithm as a C++ function. Use it to...
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 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?...
Algorithm 1: built-in sort The first algorithm you must implement is in builtin_sort, which must use the std::sort or qsort functions built into C++ to sort a vector of words. The function should sort the vector in-place, so it takes the vector as a non-const reference and returns void. This function may only need to be one line long. Both std::sort and qsort have time complexity O(n n). In addition, since these are widely-used library functions, developers have put a...
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...
Antiquicksort. The algorithm for sorting primitive types in Java is a variant of 3-way quicksort developed by Bentley and McIlroy. It is extremely efficient for most inputs that arise in practice, including inputs that are already sorted. However, using a clever technique described by M. D. McIlroy in A Killer Adversary for Quicksort, it is possible to construct pathological inputs that make the system sort run in quadratic time. Even worse, it is blows the function call stack. To see...
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 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 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. 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...