Question

Analyze two sorts in C++, the first being a bubble sort and the second being a...

Analyze two sorts in C++, the first being a bubble sort and the second being a quick sort. Create a project for each sort. Use 4 data set sizes. Results will be based on execution time for each sort. The size should have a regular increment. For example, using a regular increment of 10,000, you might have sizes of 10,000, 20,000, 30,000, and 40,000. Choose your data size based upon the ability to obtain meaning results.

Need: Two screen shots of your program displaying the sorted array for each sort. The display of each value should include the index and value of each element in the array. The output must also display the runtime.

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

Part 1

#include <iostream>

#include <ctime>

#include <cstdlib>

using namespace std;

void swap(int *xp, int *yp)

{

int temp = *xp;

*xp = *yp;

*yp = temp;

}

void bubbleSort(int arr[], int n)

{

int i, j;

for (i = 0; i < n-1; i++)

for (j = 0; j < n-i-1; j++)

if (arr[j] > arr[j+1])

swap(&arr[j], &arr[j+1]);

}

void printArray(int arr[], int size)

{

int i;

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

cout << i << ": " << arr[i] << endl;

cout << endl;

}

void generateRandomArray(int *arr, int size) {

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

arr[i] = rand() % 100;

}

}

int main() {

srand(time(0));

for (int i = 10000; i<=40000; i += 10000) {

int arr[i];

generateRandomArray(arr, i);

clock_t tStart = clock();

bubbleSort(arr, i);

clock_t tEnd = clock();

cout << "The sorted array is: " << endl;

printArray(arr, i);

cout << "Sorting took " << (double)(clock() - tStart)/CLOCKS_PER_SEC << " seconds" << endl;

}

}

Part 2

#include <iostream>

#include <ctime>

#include <cstdlib>

using namespace std;

void swap(int *xp, int *yp)

{

int temp = *xp;

*xp = *yp;

*yp = temp;

}

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

{

int pivot = arr[high];

int i = (low - 1);

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

{

if (arr[j] < pivot)

{

i++;

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

}

}

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

return (i + 1);

}

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

{

if (low < high)

{

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

quickSort(arr, low, pi - 1);

quickSort(arr, pi + 1, high);

}

}

void printArray(int arr[], int size)

{

int i;

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

cout << i << ": " << arr[i] << endl;

cout << endl;

}

void generateRandomArray(int *arr, int size) {

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

arr[i] = rand() % 100;

}

}

int main() {

srand(time(0));

for (int i = 10000; i<=40000; i += 10000) {

int arr[i];

generateRandomArray(arr, i);

clock_t tStart = clock();

quickSort(arr, 0, i-1);

clock_t tEnd = clock();

cout << "The sorted array is: " << endl;

printArray(arr, i);

cout << "Sorting took " << (double)(clock() - tStart)/CLOCKS_PER_SEC << " seconds" << endl;

}

}

Add a comment
Know the answer?
Add Answer to:
Analyze two sorts in C++, the first being a bubble sort and the second being a...
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
  • Project 2 Sorting, CPU Time, and Big O Analysis Note: you will be using Microsoft Visual...

    Project 2 Sorting, CPU Time, and Big O Analysis Note: you will be using Microsoft Visual Studios 2019 as the compiler. This is an individual assignment. You will have lab time to work on this assignment as well as homework time. Each person will analyze two sorts. The first sort will be either bubble sort or insertion sort. The second sort will be either quick sort or merge sort. Create two projects, one for each sort in the given choices....

  • Write a program in Java that obtains the execution time of selection sort, insertion sort, bubble...

    Write a program in Java that obtains the execution time of selection sort, insertion sort, bubble sort, merge sort, quick sort, and radix sort. Your program should test all sort methods for input sizes of 10000, 20000, 30000, 40000, 50000, and 60000. The selection sort, bubble sort, and radix sort should also be tested for input sizes 100000 and 200000. Your program should create the data that is sorted from randomly generated integers and should output the results in a...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • 6 6. Merge Bubble Sort: a) How does the merge bubble sort break the array into...

    6 6. Merge Bubble Sort: a) How does the merge bubble sort break the array into sublists? b) What does it need to use for each sublist and then what does it do with all of the sublists? c) What is the Big-O notation for this sort? 7. Merge Sort: a) How does the merge sort use recursion to break the array into sublists? b) What happens to each of these sublists to get the final sorted list? c) What...

  • In C++ language, implement a class that can sort an array of numbers using all three...

    In C++ language, implement a class that can sort an array of numbers using all three algorithms we have seen in this course, but each method updates a “counter” value every time it accesses the array. Have it print this at the end of the sorting process. Store the array values in an “original” array so you don’t have to re-type it for different sorts (since each sort alters the array), and have the sort modify a copy. Note: IF...

  • In C++ language, implement a class that can sort an array of numbers using all three...

    In C++ language, implement a class that can sort an array of numbers using all three algorithms we have seen in this course, but each method updates a “counter” value every time it accesses the array. Have it print this at the end of the sorting process. Store the array values in an “original” array so you don’t have to re-type it for different sorts (since each sort alters the array), and have the sort modify a copy. Note: IF...

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

  • This program should test the running time of these algorithms: Selection Sort Insertion Sort Bubble Sort...

    This program should test the running time of these algorithms: Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort Heap Sort You have access to the implementation of all of these sorting algorithms, and you may use what is provided in your text directly. Be sure to cite the source of these implementations in the header of your program. Please maintain the property that these sorting algorithms sort arrays in ascending order. For this homework, you will write a...

  • Write a program that compares the execution speed of two different sorting algorithms: bubble sort and...

    Write a program that compares the execution speed of two different sorting algorithms: bubble sort and selection sort. It should do this via functions you must write with the following prototypes: void setRandomValues(int[], int[], int length); This function sets two integer arrays with identical random values. The first two arguments are two integer arrays of the same length, and the third argument is the length of those arrays. The function then sets each element in the first and second argument...

  • //bubble sort implementation// Fig 6.15 with simplified convention for the for loop. #define _CRT...

    //bubble sort implementation// Fig 6.15 with simplified convention for the for loop. #define _CRT_SECURE_NO_WARNINGS //for windows os only #include <stdio.h> #include <stdlib.h> // Fig. 6.15: fig06_15.c // Sorting an array's values into ascending order. #include <stdio.h> #define SIZE 10 // function main begins program execution int main(void) {    // initialize a    int a[SIZE] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };    printf("Data items in original order");    // output original array    for (int i = 0;...

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