Question

In: Computer Science

I actually would like you to take a look at my code and point it out...

I actually would like you to take a look at my code and point it out if there is anything wrong with it. I think there might be because there are some values that are the same in different arrays.

QUESTION: C++ Use the random number generator in class Random to store a list of 1000 random integer values in an array. Create 3 arrays using this method. Apply each of the insertion, bubble, selection and shell sort algorithms to each array. Determine the number of comparisons and exchanges for each sort algorithm for each array. create table to print out the results.

CODE:

#include<iostream>
#include<time.h>
#include<stdlib.h>

using namespace std;

int insertioncompare = 0;
int insertionexchange = 0;

int bubblecompare = 0;
int bubbleexchange = 0;

int selectioncompare = 0;
int selectionexchange = 0;

int shellcompare = 0;
int shellexchange = 0;

//insertion sort
void insertionSort(int size, int A[])
{
int temp;
int cmp = 0;
int x, y;
for (int x = 1; x < size; x++)
{
temp = A[x];
y = x - 1;
insertioncompare++;
while (y >= 0 && A[y] > temp)
{
insertioncompare++;
insertionexchange++;
A[y + 1] = A[y];
y = y - 1;
}
A[y + 1] = temp;
}
}

//bubble sort
void bubbleSort(int size, int A[])
{
for (int i = 0; i < size; ++i)
{
for (int j = i + 1; j < size; ++j)
{
bubblecompare++;
if (A[i] > A[j])
{
bubbleexchange++;
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
}
}

//selection sort
void selectionSort(int size, int A[])
{
int min;
for (int i = 0; i < size - 1; i++)
{
min = i;
for (int j = i + 1; j < size; j++)
{
selectioncompare++;
if (A[j] < A[min])
min = j;
}
selectionexchange++;
int temp = A[min];
A[min] = A[i];
A[i] = temp;
}
}

//Shell Sort
void shellSort(int size, int A[])
{

for (int space = size / 2; space > 0; space /= 2)
{
for (int i = space; i < size; i += 1)
{
int temp = A[i];
int j;
shellcompare++;
for (j = i; j >= space && A[j - space] > temp; j -= space)
{
shellcompare++;
shellexchange++;
A[j] = A[j - space];
}
A[j] = temp;
}
}
}


int main()
{
int Isort[1000];
int Bsort[1000];
int Ssort[1000];
int ShellSort[1000];
//creating random array
for (int i = 0; i < 1000; ++i)
{
Isort[i] = rand();
}
//creating random arry for bubble sort
for (int j = 0; j < 1000; ++j)
{
Bsort[j] = Isort[j];
}
//creating random array for selection sort
for (int j = 0; j < 1000; ++j)
{
Ssort[j] = Isort[j];
}
//creating random aray for shell sort
for (int j = 0; j < 1000; ++j)
{
ShellSort[j] = Isort[j];
}

insertionSort(1000, Isort);
bubbleSort(1000, Bsort);
selectionSort(1000, Ssort);
shellSort(1000, ShellSort);
  
cout << "Array 1:";
cout << "\nInsertion sort: \nCompare = " << insertioncompare << " Exchange = " << insertionexchange;
cout << "\nBubble sort: \nCompare = " << bubblecompare << " Exchange = " << bubbleexchange;
cout << "\nSelection sort: \nCompare = " << selectioncompare << " Exchange = " << selectionexchange;
cout << "\nShell sort: \nCompare = " << shellcompare << " Exchange = " << shellexchange;
cout << endl << endl;

insertionSort(1000, Isort);
bubbleSort(1000, Bsort);
selectionSort(1000, Ssort);
shellSort(1000, ShellSort);

cout << "Array 2:";
cout << "\nInsertion sort: \nCompare = " << insertioncompare << " Exchange = " << insertionexchange;
cout << "\nBubble sort: \nCompare = " << bubblecompare << " Exchange = " << bubbleexchange;
cout << "\nSelection sort: \nCompare = " << selectioncompare << " Exchange = " << selectionexchange;
cout << "\nShell sort: \nCompare = " << shellcompare << " Exchange = " << shellexchange;
cout << endl << endl;

insertionSort(1000, Isort);
bubbleSort(1000, Bsort);
selectionSort(1000, Ssort);
shellSort(1000, ShellSort);

cout << "Array 3:";
cout << "\nInsertion sort: \nCompare = " << insertioncompare << " Exchange = " << insertionexchange;
cout << "\nBubble sort: \nCompare = " << bubblecompare << " Exchange = " << bubbleexchange;
cout << "\nSelection sort: \nCompare = " << selectioncompare << " Exchange = " << selectionexchange;
cout << "\nShell sort: \nCompare = " << shellcompare << " Exchange = " << shellexchange;
cout << endl;
}

OUTPUT:

Array 1:
Insertion sort:
Compare = 249144 Exchange = 248145
Bubble sort:
Compare = 499500 Exchange = 246686
Selection sort:
Compare = 499500 Exchange = 999
Shell sort:
Compare = 16332 Exchange = 8326

Array 2:
Insertion sort:
Compare = 250143 Exchange = 248145
Bubble sort:
Compare = 999000 Exchange = 246686
Selection sort:
Compare = 999000 Exchange = 1998
Shell sort:
Compare = 24338 Exchange = 8326

Array 3:
Insertion sort:
Compare = 251142 Exchange = 248145
Bubble sort:
Compare = 1498500 Exchange = 246686
Selection sort:
Compare = 1498500 Exchange = 2997
Shell sort:
Compare = 32344 Exchange = 8326

Solutions

Expert Solution

The values are same because the random function you are using is infact not really random. When you use rand function like you are not really getting random values. If you print these values then you will see the sequence generated each time is same. So what we do is we seed the rand function with initial value which should be different each time so that a set of random values is generated each time. Now since seed should also be random then we use a time function for this. So before using the rand function we seed the rand function. So the program will look something like this:

   srand(time(0)); // Seeding the initial value for rand function.

// Creating random array.
   for (int i = 0; i < 1000; ++i) {
       Isort[i] = rand();
   }

Hope this helps. If you have any queries or suggestions regarding the answers please leave them in the comments section so I can update and improve the answer. Thank you.


Related Solutions

I would like you to take a look at a company website of your choice. The...
I would like you to take a look at a company website of your choice. The company must be publicly traded. Explore their website to learn more about the company. After researching the company answer the 4 questions listed below. Be sure to provide specific details in your post that you found on the website for the company you chose. Be sure to provide a link to the website of the company you chose to use for this assignment. 1....
I would like to compare my current code with that of someone else to be sure...
I would like to compare my current code with that of someone else to be sure I did this assignment correctly. My main concern is with Problems 2 and 4. I sometimes struggle with testing the methods I have created. If you can, please answer all of the questions so that I may reference them if I am still lost on Problems 2 and 4. Each problem requires the one before it and so seeing the entire assignment as opposed...
With this assignment I would like for you to take a topic in economics and analyze...
With this assignment I would like for you to take a topic in economics and analyze it using the concepts and ideas developed in the course. You can follow the format for writing a paper on Wal-Mart, or you can choose to analyze any other applicable topic that interests you using the paper guidelines provided (under 'Assignments'/'Writing Assignment'; I've provided examples of papers and topics composed by previous instructors and students in this course). If you have questions and/or problems...
How would I add an automatic please fill out this info to my complete code. Something...
How would I add an automatic please fill out this info to my complete code. Something similar to this code. <!doctype html> <html> <head> <meta charset="UTF-8"> <title>Untitled Document</title> <script>    function phonenumber(inputtxt){           var phoneno = (\(\d{3}\)|\d{3})[-\s]\d{3}-\d{4};       if(inputtxt.value.match(phoneno)){        document.getElementById("yourEntry").innerHTML = document.getElementById("myphone").value;    }    //if(!inputtxt.value.match(phoneno)){        // alert("Does not Work!")        //}    } </script> </head> <body> <form name="form1" action="#"> <input type="text" name="myphone" id="myphone" value="" pattern= "(\(\d{3}\)|\d{3})[-\s]\d{3}-\d{4}" placeholder="(555) 555-1212" required /> <input...
I would like to know if my answers are correct: •Suppose you conducted a survey, and...
I would like to know if my answers are correct: •Suppose you conducted a survey, and you calculated a correlation between two of the responses of your survey. You got an r value of 10. Which of the following could explain this? choose a a.You made a mistake in your calculation.There is no way to get an r value of 10. b.The numbers you put in for x and y aren't appropriate for this statistical technique. c.The variables x and...
I have an unexpected indent with my python code. please find out whats wrong with my...
I have an unexpected indent with my python code. please find out whats wrong with my code and run it to show that it works here is the code : def main(): lis = inputData() customerType = convertAcct2String(lis[0]) bushCost = getBushCost(lis[0],int(lis[1],10)) flowerCost = getFlowerBedCost(int(lis[2],10),int(lis[3],10)) fertiCost = getFertilCost(int(lis[4],10)) totalCost = calculateBill(bushCost,fertiCost,flowerCost) printReciept(customerType,totalCost,bushCost,fertiCost,flowerCost) def inputData(): account, number_of_bushes,flower_bed_length,flower_bed_width,lawn_square_footage = input("Please enter values").split() return [account, number_of_bushes,flower_bed_length,flower_bed_width,lawn_square_footage] def convertAcct2String(accountType): if accountType== "P": return "Preferred" elif accountType == "R": return "Regular" elif accountType == "N": return...
In which folder of XAMPP would I put in my PHP code
In which folder of XAMPP would I put in my PHP code
If you are not currently employed, look at a company you would like to work for...
If you are not currently employed, look at a company you would like to work for and look at their retirement program. Next, what concept do you find most challenging about retirement?
How would I make it so that when I run my code it does not ask...
How would I make it so that when I run my code it does not ask for input (not having to enter after the statement and enter 0 for example) after ROXY (Forever ROXY Enterprises) appears? Like it would tell me the second statement right away along with the Roxy phrase. This is in C++. My code: #include / #include using std::endl; int main() {    void readAndConvert();    unsigned int stockSymbol;    unsigned int confNum;    std::cout << "ROXY...
Q. I would like you to use a fixed point method to solve the positive real...
Q. I would like you to use a fixed point method to solve the positive real quadratic root of 4 by solving h(x) = x^4 − 4 = 0. The standard method manipulates h(x) = 0 into g(x) = x so that the iterative scheme becomes xn+1 = g(xn). The iterative scheme will converge to the required solution if the root is in the interval defined by |g '(x)| < 1 (i) We begin by adding x to both sides...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT