This is question about C++ (bubblesort code) This question have wrong C++ code w
ID: 3663225 • Letter: T
Question
This is question about C++ (bubblesort code)
This question have wrong C++ code
what do I chang or fix to make bubblesort??(please explain me )
#ifndef __TICKS_H__
#define __TICKS_H__
// Function prototype for the "tick()" function
unsigned long long tick();
#endif
// Prototype for the "check" function to verify array properly sorted
#ifndef __CHECKIT_H__
#define __CHECKIT_H__
// Check array "data" is sorted properly up to data[size-1]
// Returns the count of errors (should be zero)
int CheckIt(int data[], int size);
#endif
#include <fcntl.h>
#include <iomanip>
#include <iostream>
#include <stdlib.h>
#include <sys/time.h>
#include "ticks.h"
#include "check-it.h"
using namespace std;
void BubbleSort(int data[], int size)
{ // Sort the array "data" of length "size" using bubblesort algorithm
// YOUR CODE HERE
}
int main()
{
// use the /dev/random device to get a random seed
// get a random seed for the random number generator
int dr = open("/dev/random", O_RDONLY);
if (dr < 0)
{
cout << "Can't open /dev/random, exiting" << endl;
exit(0);
}
int drValue;
read(dr, (char*)&drValue, sizeof(drValue));
// cout << "drValue " << drValue << endl;
srand48(drValue);
for (int size = 100; size <= 100000; size *= 10) // 100 to 100000 by 10
{
// Data is the array to sort, filled in with random values
int data[size];
// Randomize the data array
for (int i = 0; i < size; ++i)
{
data[i] = drand48() * 100000;
}
// Measure the execution time
unsigned long long startTicks = tick();
// Now call the bubble sort function
BubbleSort(data, size);
// Read the ending tick count
unsigned long long endTicks = tick();
// Check results
int errorCount = CheckIt(data, size);
if (errorCount == 0)
{
cout << "Congrats! sorted correctly size " << size << endl;
cout << "Execution time is " << setw(12) << right
<< endTicks - startTicks << " ticks" << endl;
}
else
{
cout <<"Oops, " << errorCount << " errors found " << endl;
}
}
}
tick.h code check-it.h code#ifndef __TICKS_H__
#define __TICKS_H__
// Function prototype for the "tick()" function
unsigned long long tick();
#endif
// Prototype for the "check" function to verify array properly sorted
#ifndef __CHECKIT_H__
#define __CHECKIT_H__
// Check array "data" is sorted properly up to data[size-1]
// Returns the count of errors (should be zero)
int CheckIt(int data[], int size);
#endif
Explanation / Answer
#include <fcntl.h>
#include <iomanip>
#include <iostream>
#include <stdlib.h>
#include <sys/time.h>
#include "ticks.h"
#include "check-it.h"
using namespace std;
void bubble_sort (int data[], int n)
{
for (int i = 0; i < n; ++i)
for (int j = 0; j < n - i - 1; ++j)
if (data[j] > data[j + 1])
{
int temp = data[j];
data[j] = data[j + 1];
data[j + 1] = temp;
}
}
int main()
{
// use the /dev/random device to get a random seed
// get a random seed for the random number generator
int dr = open("/dev/random", O_RDONLY);
if (dr < 0)
{
cout << "Can't open /dev/random, exiting" << endl;
exit(0);
}
int drValue;
read(dr, (char*)&drValue, sizeof(drValue));
// cout << "drValue " << drValue << endl;
srand48(drValue);
for (int size = 100; size <= 100000; size *= 10) // 100 to 100000 by 10
{
// Data is the array to sort, filled in with random values
int data[size];
// Randomize the data array
for (int i = 0; i < size; ++i)
{
data[i] = drand48() * 100000;
}
// Measure the execution time
unsigned long long startTicks = tick();
// Now call the bubble sort function
BubbleSort(data, size);
// Read the ending tick count
unsigned long long endTicks = tick();
// Check results
int errorCount = CheckIt(data, size);
if (errorCount == 0)
{
cout << "Congrats! sorted correctly size " << size << endl;
cout << "Execution time is " << setw(12) << right
<< endTicks - startTicks << " ticks" << endl;
}
else
{
cout <<"Oops, " << errorCount << " errors found " << endl;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.