Question

In: Computer Science

Create the following functions for an array in C++. Test with size 10, 10,000 and 100,000....

  • Create the following functions for an array in C++. Test with size 10, 10,000 and 100,000. Time each sort.

    • Merge sort

    • Insertion Sort

    • Selection Sort        

    • Bubble Sort

    • Quick Sort

  • PLEASE DO IT IN C++

Solutions

Expert Solution

C++ code using DEV-C++

============================================================================================

//bubble,selection,insertion sort
//quick,merge sort


#include <iostream>
#include <ctime>
#include <cstdlib>
#include<iomanip>
#include<math.h>
using namespace std;
int *ins_el;
int *bubble_el;
int *sel_el;
int *quick_el;
int *merge_el;

void insertionSort(int n)
{
int i, j;
int temp;
for (i = 1; i <= n; i++)
{
temp = ins_el[i];
j = i - 1;
while (j > 0 && ins_el[j] > temp)
{
ins_el[j + 1] = ins_el[j];
j = j - 1;
}
ins_el[j + 1] = temp;
}
}

void bubbleSort(int n)
{
int i, j;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (bubble_el[j] > bubble_el[j + 1])
{
int t = bubble_el[j];
bubble_el[j] = bubble_el[j + 1];
bubble_el[j + 1] = t;
}
}
}
}

void selectionSort(int n)
{
int i, j;
for (i = 0; i < n; i++)
{
for (j = i; j < n; j++)
{
if (sel_el[i] > sel_el[j])
{
int temp = sel_el[i];
sel_el[i] = sel_el[j];
sel_el[j] = temp;
}
}
}
}

int partition(int low, int high)
{
int pivot = quick_el[high];
int i = (low - 1), j;
for (j = low; j <= high - 1; j++)
{
if (quick_el[j] <= pivot)
{
i++;
int temp = quick_el[i];
quick_el[i] = quick_el[j];
quick_el[j] = temp;
}
}
int temp = quick_el[i + 1];
quick_el[i + 1] = quick_el[high];
quick_el[high] = temp;
return (i + 1);
}

void quickSort(int low, int high)
{
if (low < high)
{
int pi = partition(low, high);
quickSort(low, pi - 1);
quickSort(pi + 1, high);
}
}

void merge(int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
/* create temp arrays */
int *L = new int[n1];
int *R = new int[n2];
/* Copy data to temp arrays L[] and R[] */
for (i = 0; i < n1; i++)
L[i] = merge_el[l + i];
for (j = 0; j < n2; j++)
R[j] = merge_el[m + 1 + j];
/* Merge the temp arrays back into arr[l..r]*/
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
merge_el[k] = L[i];
i++;
}
else
{
merge_el[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
merge_el[k] = L[i];
i++;
k++;
}
while (j < n2)
{
merge_el[k] = R[j];
j++;
k++;
}
}

void mergeSort(int l, int r)
{
if (l < r)
{
int m = l + (r - l) / 2;
// Sort first and second halves
mergeSort(l, m);
mergeSort(m + 1, r);
merge(l, m, r);
}
}
int main()
{
srand(time(NULL));
cout<<left<<setw(20)<<"Data Size"
<<setw(15)<<"Merge Sort"
<<setw(15)<<"Quick Sort"
<<setw(15)<<"Bubble Sort"
<<setw(15)<<"Selection Sort"
<<setw(15)<<"Insertion Sort"
<<endl;
int rand_val;


// Loop for 100000 and 1010000
for (int i = 100000; i <=1010000; i += 910000)
{
ins_el = new int[i];
bubble_el = new int[i];
sel_el = new int[i];
quick_el = new int[i];
merge_el = new int[i];

for (int j = 0; j < i; j++)
{
rand_val = rand() % 100;
ins_el[j] = rand_val;
bubble_el[j] = rand_val;
quick_el[j] = rand_val;
merge_el[j] = rand_val;
}
cout<<"step1 ";

clock_t start, finish;


start = clock(); //time in milliseconds
mergeSort(0, i - 1);
finish = clock(); //time in milliseconds
double merge_duration = (double)((finish - start) / (double)CLOCKS_PER_SEC); //time in secs.

cout<<"mergesort done ";

start = clock(); //time in milliseconds
quickSort(0, i - 1);
finish = clock(); //time in milliseconds
double quick_duration = (double)((finish - start) / (double)CLOCKS_PER_SEC); //time in secs.


start = clock(); //time in milliseconds
bubbleSort(i);
finish = clock(); //time in milliseconds
//the constant CLOCKS_PER_SEC below is equal to 1000
double bubble_duration = (double)((finish - start) / (double)CLOCKS_PER_SEC); //time in secs.

start = clock(); //time in milliseconds
selectionSort(i);
finish = clock(); //time in milliseconds
//the constant CLOCKS_PER_SEC below is equal to 1000
double selection_duration = (double)((finish - start) / (double)CLOCKS_PER_SEC); //time in secs.


start = clock(); //time in milliseconds
insertionSort(i);
finish = clock(); //time in milliseconds
//the constant CLOCKS_PER_SEC below is equal to 1000
double insertion_duration = (double)((finish - start) / (double)CLOCKS_PER_SEC); //time in secs.

cout<<left<<setw(20)<<i
<<setw(15)<<merge_duration
<<setw(15)<<quick_duration
<<setw(15)<<bubble_duration
<<setw(15)<<selection_duration
<<setw(15)<<insertion_duration
<<endl;

}

system("pause");
return 0;

}

============================================================================================

Output

it will took lot of time to get output for 1010000 size.


Related Solutions

c++ language Create a file program that reads an int type Array size 10; the array...
c++ language Create a file program that reads an int type Array size 10; the array has already 10 numbers, but your job is to resize the array, copy old elements of array to the new one and make it user input and add an additional 5 slots in the array, and lastly do binary search based on user input. close the file.
Write C++ program to do the following: 1. Create integer array size of 10 2. Ask...
Write C++ program to do the following: 1. Create integer array size of 10 2. Ask user input the values of the array's element using for loop 3. pass the array to void function. in void function do the following: a. Find the maximum of the array. b. Compute the element average c. Find out how many numbers are above the average d. Find out and print how many numbers are below the average e. find out how many numbers...
Directions of assignment: - Create an array of words of size 10. - Prompt the User...
Directions of assignment: - Create an array of words of size 10. - Prompt the User to enter the 10 integers. Populate the array with the integers as they are entered. - You MUST use indexed addressing to traverse through the array. - Determine the maximum and the minimum values contained within the array and print them out. Can you see what I am doing wrong here, the last part (to find the min and max) I can't seem to...
Given an array of Student type and size 10, create a linked list of students by...
Given an array of Student type and size 10, create a linked list of students by linking students with an odd index first and then linking students with an even index. Write a loop to print out the students in the linked list #include<iostream> #include<string> #include<fstream> using namespace std; const int NUM = 10; struct Student{ string fName; string lName; Student * next; }; int main() {        Student stuArr[NUM];        ifstream myfile;        myfile.open("Test.txt");        for(int i = 0;...
In C++, Create an array of 10 characters. Use a loop to prompt for 10 letters...
In C++, Create an array of 10 characters. Use a loop to prompt for 10 letters (a-z|A-Z) and store each in the array. If an invalid character is entered, re-prompt until a valid char is entered. After 10 characters are stored in the array, use a loop to print out all characters in the array from the first to the last element. Then, use another loop starting at the last element to print all characters in the array in reverse...
Write a C program to Declare an integer array of size 10 with values initialized as...
Write a C program to Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Compute each item of a new array of same size derived from the above array by: adding first item (intArray[0]) of the array with 3rd, 2nd with 4th, 3rd with 5th and so on. For the last-but-one item intArray[8], add it with first item and for the last item (intArray[9])...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
c++ I need a code that will fill an array size of 1000, an array of...
c++ I need a code that will fill an array size of 1000, an array of size 2000, and an array size of 10000, with random int values. Basically like this: array1[1000] = filled all with random numbers array2[2000] = filled all with random numbers array3[10000] = filled all with random numbers C++ no need for print
IN C++ (THIS IS A REPOST) Design a class, Array, that encapsulates a fixed-size dynamic array...
IN C++ (THIS IS A REPOST) Design a class, Array, that encapsulates a fixed-size dynamic array of signed integers. Write a program that creates an Array container of size 100 and fills it with random numbers in the range [1..999]. (Use std::rand() with std::srand(1).) When building the array, if the random number is evenly divisible by 3 or 5, store it as a negative number. Within your main.cpp source code file, write a function for each of the following processes....
In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT