Answered Essay: Write Program in C++

Write Program in C++


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

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(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
temp_array[index] = values[right_First];
count++; //count of comaparisons per iteration of while loop.

while(left_First <= left_Last){

temp_array[index] = values[left_First];

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


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


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

int main(){

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

int a[SIZE];
for (int i=0; i < n; i++){
cout << “Enter the first element: “;
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;

2) Bubble Sort


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: “;
cout<<“Enter the elements: “<<endl;



count++; //count of comaparisons per iteration of inner for loop.
cout<<“Array after bubble sort:”;
cout<<” “<<array[i];
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:

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.


Let us write that nagging essay.


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.


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.


Editing (OUR PART)

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


Receive your Paper

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