Question

In this lab we are going to complete a profile of two sorting algorithms by running...

In this lab we are going to complete a profile of two sorting algorithms by running some tests to collect empirical data.

1. First we need to be able to generate some random integers. You can do this by including the following library :

#include 

Now first run the following to generate a seed :

srand (time(NULL))

You can then generate a random number using the function

rand()

2. We will use two sort algorithms - Selection Sort and Bubble Sort running on a vector v with n elements.

//selection sort
for (i = 0; i < n-1; i++)
    for (j = i+1; j < n; j++)
        if (v[i] > v[j])
            swap(v[i], v[j])
//bubble sort
for (i = n-1; i > 0; i--)
    for (j = 0; j < i; j++)
        if (v[j] > v[j+1])
            swap(v[j], v[j+1])

You should write two functions - one for each algorithm - to accept the vector and then sort the vector.

3. Test each function by creating a vector loaded with random numbers and running your sort functions. Make sure the functions are correctly sorting the vectors.

4. Next we want to time the functions. To do so we need a library :

#include-

Declare two variables :

clock_t start, finish;

And you can get the time used by the function with :

start=clock();
selsort(v);
finish=clock();
cout << "cpu time= " << finish-start;
You can see a full program here. 

THE FULL PROGRAM:

#include 
#include 
#include 
#include 

using namespace std;

void selsort(vector& v)
{
        for (int i = 0; i < v.size(); i++)
          for (int j = i+1; j < v.size(); j++)
              if (v[i] > v[j])
              { int t =v[i];
                v[i]=v[j];
                v[j]=t;
              }
}

int main()
{
        clock_t start, finish;
        srand (time(NULL));
        vector v;
        int n=0;
        cout << "Enter size of input :" ;
        cin >> n;
       
for (int i=0; i<n; i++)
                v.push_back(rand());
        start=clock();
        selsort(v);
        finish=clock();
        cout << "time= " << finish-start;
}

5. For each sort algorithm run 5 tests for input lengths 1000, 2000, 3000, ...,10000 and record the median result. You should therefore have 10 time samples for each algorithm

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

If you have any doubts, please give me comment...

#include <iostream>

#include <vector>

#include <cstdlib>

#include <ctime>

using namespace std;

void selsort(vector<int> &v) {

for (int i = 0; i < v.size(); i++)

for (int j = i + 1; j < v.size(); j++)

if (v[i] > v[j]) {

int t = v[i];

v[i] = v[j];

v[j] = t;

}

}

int main() {

clock_t start, finish;

srand(time(NULL));

vector<int> v;

int n = 0;

cout << "Enter size of input :";

cin >> n;

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

v.push_back(rand());

start = clock();

selsort(v);

finish = clock();

cout << "time= " << finish - start<<endl;

}

Add a comment
Know the answer?
Add Answer to:
In this lab we are going to complete a profile of two sorting algorithms by running...
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
  • Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace...

    Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace std; void combine(int *a, int low, int high, int mid) {        int i, j, k, c[100000];        i = low;        k = low;        j = mid + 1;        while (i <= mid && j <= high)        {               if (a[i] < a[j])               {                      c[k] = a[i];                      k++;                      i++;               }               else               {                     ...

  • Lab 10A Measure the program execution time One easy way to measure the program execution time is ...

    Lab 10A Measure the program execution time One easy way to measure the program execution time is encapsulate the useful timing functionality of C++ chrono library, This is illustrated inhttps://www.learncpp.com/cpp-tutorial/8-16-timing-your-code/ The example usingChronoTimer.cpp (Github/m10) is an example program using this chrono Timer class object to measure the Sorting on on an integer array of 10000 random integers based on the Bubble Sort vs. the C++ built-in Sort (an https://www.quora.com/Which-sorting-algorithm-does-STL-standard-template-library-use-in-c++. ) Please measure the performance of sorting the same array used...

  • Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative perfo...

    Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative performance of the algorithms for a variety of data sets. Need Help With this Sorting Algorithm task for C++ Base Code for sorting.cpp is given. The header file is not included in this. Help would be much appreciated as I have not started on this due to personal reasons #include <cstdlib> #include <iostream> #include <getopt.h> using namespace std; long compares; // for counting...

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

  • Hello, I want to check if my C++ code is correct and follows the requeriments described...

    Hello, I want to check if my C++ code is correct and follows the requeriments described thanks. Requeriments: Assignment Sorting Benchmark each of the sorting methods listed below. Insertion Sort Bubble Sort Selection Sort Heap Sort. Quick Sort. Merge Sort. Benchmark each of the above sorting methods for data sizes of 10000, 20000, 30000, 40000 and 50000. Display the results in a table as shown below. The table should have rows and columns. However, the rows and columns need not...

  • Add binary_search() (page 462) to your program. Modify main() to prompt the user for a number...

    Add binary_search() (page 462) to your program. Modify main() to prompt the user for a number to search (until ^D) and display the position of the number in the sorted vector. Try your program for the following user input: 1 15 18 40 30 50 ^D The output should be: -1 2 -1 7 5 -1 int binary_search(vector<int> v, int from, int to, int value) { if (from > to) return -1; int mid = (from + to) / 2;...

  • I want to compare the runtimes and swap operations times among quick Sort, selection Sort and...

    I want to compare the runtimes and swap operations times among quick Sort, selection Sort and shell Sort here is my code: But when I create a 1000,000 size array, I can't get the result of the operations times and runtime. what's wrong with my code? and I also want to copy the array. Because I want to use same array for three sort. And for the shell Sort, I haven't learn it in my class. Can anyone help me...

  • Need help adjusting this code in C. I'm not sure this one part of the code...

    Need help adjusting this code in C. I'm not sure this one part of the code is correct, it needs to generate n random addresses between 0 and (2^32)-1. So I set it to "address = rand() % 232;" but I'm not sure if that is correct. Can you please fix thanks. #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { unsigned int address; unsigned int pg_num; unsigned int offset; unsigned int i; unsigned int n = 1000000; double time_taken;...

  • This is an assignment for my algorithm class which I have written the code partially and...

    This is an assignment for my algorithm class which I have written the code partially and only need to complete it by adding a time function that calculates the average running time. We could use any programming language we want so I am using C++. I am including the instruction and my partial code below. Thank you! Implement linearSearch(a,key) and binarySearch( a,key)functions. Part A.In this part we will calculate theaverage-case running time of each function.1.Request the user to enter a...

  • The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also...

    The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also please explain why the seg fault is happening. Thank you #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // @Name loadMusicFile // @Brief Load the music database // 'size' is the size of the database. char** loadMusicFile(const char* fileName, int size){ FILE *myFile = fopen(fileName,"r"); // Allocate memory for each character-string pointer char** database = malloc(sizeof(char*)*size); unsigned int song=0; for(song =0; song < size;...

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