Answered Essay: Write Program in C++

Write Program in C++

Instructions…

For this assignment you will write your own version of a merge sort and a bubble sort — sorting files of numbers.

Your programs should read a file integers into an array of integers, sort them from least to greatest, then print the sorted list out.

(note: you do not have to sort “in-place” — you can sort into a newly allocated array)

For you program to allocate a large enough array up front, you may want to have the first number in your data files be the count of how many random numbers are in the file.

Or it could be a command line parameter to your program.

Your version of these programs should include keeping count every time your program compares two items from the list.

Print the count of comparisons when the programs ends.

It is up to you how you keep the “count of comparisons” separated from the sorted output list.

On Linux, you might use stdout and stderr…

…or you might give filenames as command line args to your programs, and the only stdout form your program will be the stats on how many comparison steps were used to sort the      list.

…or you might have a command line arg with a filename specifically to **append** stats to

Expert Answer

 

1) Merge Sort

#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;

int count = 0; //Number of comparisons
int n = 0;
const int SIZE = 100;
void merge(int values[], int left_First, int left_Last, int right_First, int right_Last);
void print_array( int a[], int n);
void merge_sort(int a[], int start, int end){

if(start < end){
int mid = (start+end)/2;
merge_sort(a,start, mid);
merge_sort(a,mid+1,end);
merge(a, start,mid, mid+1, end);
}
}
void merge(int values[], int left_First, int left_Last, int right_First, int right_Last){
int temp_array[SIZE];
int index = left_First;
int saveFirst = left_First;

while((left_First <= left_Last) && ( right_First <= right_Last)){//Two sub arrays are compared for smallest element

if(values[left_First] < values[right_First]){
temp_array[index] = values[left_First]; //smallest element is assigned to temp
left_First++;
}
else
{
temp_array[index] = values[right_First];
right_First++;
}
index++;
count++; //count of comaparisons per iteration of while loop.
}

while(left_First <= left_Last){

temp_array[index] = values[left_First];
left_First++;
index++;

}
while(right_First <= right_Last){
temp_array[index] = values[right_First];
right_First++;
index++;

}

for(index = saveFirst; index <= right_Last; index++)//copies from temp array to values array
values[index] = temp_array[index];
print_array(values,n);
cout << endl;

}

void print_array( int a[], int n){
for (int i=0; i < n; i++)
cout << a[i] << ” “;
}

int main(){

clrscr();
cout << “Enter number of elements to be sorted : “;
cin >>n;

int a[SIZE];
for (int i=0; i < n; i++){
if(i==0)
cout << “Enter the first element: “;
else
cout << “Enter the next element: “;
cin >> a[i];
}

int start = 0;
int end = n-1;
merge_sort(a, start, end);
print_array(a, n);
cout << endl;
cout << “Total number of comparisons done during mergesort : “<< count << endl;
getch();
}

2) Bubble Sort

#include<iostream>

using namespace std;

int main()
{
int array[50],num,i,j,temp;
int count=0; // Number of Comparisons
cout<<“Enter number of elements to be sorted: “;
cin>>num;
cout<<“Enter the elements: “<<endl;

for(i=0;i<num;++i)
cin>>array[i];

for(i=1;i<num;++i)
{
for(j=0;j<(num-i);++j)
{
if(array[j]>array[j+1])
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;

}
count++; //count of comaparisons per iteration of inner for loop.
}
}
cout<<“Array after bubble sort:”;
for(i=0;i<num;++i)
cout<<” “<<array[i];
cout<<endl;
cout<<“Total number of comparisons done during Bubble Sort: “<<count;
return 0;
}

Buy Essay
Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

24/7 Customer Support

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

STEP 1

Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

STEP 2

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

STEP 3

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.

STEP 4

Receive your Paper

After Editing, your paper will be sent to you via email.