Write a computer program that prompts the user for a number, creates an array fo
ID: 645817 • Letter: W
Question
Write a computer program that prompts the user for a number, creates an array for that number of random integers, and then uses the sophisticated form of bubble sort to order the array. The program should print out the array prior to the call to the sorting algorithm and afterwards. You can write the program in C++,
Repeat 1 but use insertion sort this time.
1 and 2 are primarily intended to make sure that your algorithms work.
Once you are convinced your programs work, do the following
Write a computer program that prompts the user for two numbers, n for the number of items in the array to sort, and num_ifor a number of iterations. Then do the following:
Initiate a variable running_time to 0
Create a for loop that iterates num_i times.
In the body of the loop,
Create an array of n random integers
Get the time and set this to start-time. You will have to figure out what the appropriate command is in the programming language you are using to find them time
Use sophisticated bubble sort to sort the array
Get the time and set this to end-time
Subtract end-time from start-time and add the result to running_time
Once the program has run, note
The number of items sorted
The number of iterations
The running time
Repeat the process nine times, using 5, 25 and 50 as the size of the array, and 10, 100 and 1000 as the number of iterations.
Explanation / Answer
#include
#include
const int MAX =8;
class array
{
private:
int arr[MAX];
int count;
public:
array();
void add(int item);
void sort();
void display();
};
array ::array()
{
count=0;
for(int i=0;i arr[i]=0;
}
void array::add(int item)
{
if(count {
arr[count]=item;
count++;
}
else
cout<<" Array is full"< }
void array::sort()
{
int temp;
for(int i=0;i<=count-2;i++)
{
for(int j=0;j<=count-2-i;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
void array::display()
{
for(int i=0;i cout< cout< }
void main()
{
time_t now=time(0);
char* dt=ctime(&now);
cout<<" Date and time is: "<
array a;
a.add(25);
a.add(17);
a.add(31);
a.add(13);
a.add(2);
a.add(4);
a.add(8);
a.add(16);
a.add(9);
cout<<" BUBBLE SORT ";
cout<<" Array before sorting: "< a.display();
a.sort();
cout<<" Array after bubble sort"< a.display();
}
______________________________________________
OUTPUT
______________________________________________
Date and time is: Thu Mar 26 13:43:54 2015
BUBBLE SORT
Array before sorting:
25 17 31 13 2 4 8 16 9
Array after sorting:
2 4 8 9 13 16 17 25 31
______________________________________________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.