This is about C++ question (I have to find wrong codes and fix them to correct b
ID: 3663222 • Letter: T
Question
This is about C++ question (I have to find wrong codes and fix them to correct bubble sort )
what do I chang or fix below code and please explain me???
#include
#include
#include
#include
#include
#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;
}
}
}
Explanation / Answer
#include<iostream>
using namespace std;
int main()
{
int a[50],n,i,j,temp;
cout<<"Enter the size of array: ";
cin>>n;
cout<<"Enter the array elements: ";
for(i=0;i<n;++i)
cin>>a[i];
for(i=1;i<n;++i)
{
for(j=0;j<(n-i);++j)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
cout<<"Array after bubble sort:";
for(i=0;i<n;++i)
cout<<" "<<a[i];
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.