Question:

Functions with sorting algorithm Assignment help!?

by  |  earlier

0 LIKES UnLike

hi guyz my teacher told me to apply this given examples(below) into a bubble sorting algorithm using a call function...i think he wants us to use call function on a bubble sorting algorithm...i also included the bubble sort below the given example.,,

i dont really understand his example -_-"..plz help me out

Merge Sort

#include <iostream.h>

int a[50];

void merge(int,int,int);

void merge_sort(int low,int high)

{

int mid;

if(low<high)

{ mid=(low+high)/2;

merge_sort(low,mid);

merge_sort(mid+1,high);

merge(low,mid,high);

}

}

void merge(int low,int mid,int high)

{

int h,i,j,b[50],k;

h=low;

i=low;

j=mid+1;

while((h<=mid)&&(j<=high))

{

if(a[h]<=a[j])

{

b[i]=a[h];

h++;

}

else

{

b[i]=a[j];

j++;

}

i++;

}

if(h>mid)

{

for(k=j;k<=high;k++)

{

b[i]=a[k];

i++;

}

}

else

{

for(k=h;k<=mid;k++)

{

b[i]=a[k];

i++;

}

}

for(k=low;k<=high;k++)

a[k]=b[k];

}

void main()

{

int num,i;

cout<<"*******************************...

cout<<" MERGE SORT PROGRAM "<<endl;

cout<<"*******************************...

cout<<endl<<endl;

cout<<"Please Enter THE NUMBER OF ELEMENTS you want to sort [THEN PRESS ENTER]:"<<endl;

cin>>num;

cout<<endl;

cout<<"Now, Please Enter the ( "<< num <<" ) numbers (ELEMENTS) [THEN PRESS ENTER]:"<<endl;

for(i=1;i<=num;i++)

{

cin>>a[i] ;

}

merge_sort(1,num);

cout<<endl;

cout<<"So, the sorted list (using MERGE SORT) will be :"<<endl;

cout<<endl<<endl;

for(i=1;i<=num;i++)

cout<<a[i]<<" ";

cout<<endl<<endl<<endl<<endl;

}

--------------------------------------...

// Bubble Sort Function for Descending Order

void bubble_sort(apvector <int> &array)

{

int i, j, flag = 1; // set flag to 1 to begin initial pass

int temp; // holding variable

int arrayLength = array.length( );

for(i = 1; (i <= arrayLength) && flag; i++)

{

flag = 0;

for (j=0; j < (arrayLength -1); j++)

{

if (array[j+1] > array[j]) // ascending order simply changes to <

{

temp = array[j]; // swap elements

array[j] = array[j+1];

array[j+1] = temp;

flag = 1; // indicates that a swap occurred.

}

}

}

return; //arrays are passed to functions by address; nothing is returned

}

 Tags:

   Report

1 ANSWERS


  1. I&#039;ll have a go (nobody else has).  You might get more answers if you take all those empty lines out of the code.  It makes it hard work to read.

    From what I can tell (and I haven&#039;t followed it entirely), the code you have does an insertion sort (or something like that).  Its get&#039;s the items from one list and puts them in another list by doing a binary search to find the correct place.

    It looks like all you have to do is change the line &#039;merge_sort(1, num)&#039; in your main function with a line that calls the &#039;bubble_sort&#039; function.  You have to make sure the parameters are ready for the bubble_sort function.

    Calling a function happens when you put it&#039;s name in the code. The line &#039;merge_sort(1, num)&#039; calls the merge_sort function.  The computer runs the code in the function and then returns to where you called it from.

    A binary search is a bit like a hi-lo game.  You have a number that fits in a list. You find the number in the middle of the list, if it&#039;s higher, check it against the middle number of the top half of the list.  If it&#039;s lower check it against the middle of the bottom half.  You keep dividing into two, until you&#039;re checking against one item.  Then you know where it should be.

    Using this method on a list of 256 items, you&#039;d only have to check 8 times.

    A bubble sort is a very simple way of sorting things. Starting with the first item, check if its higher than the item above, if its is swap them over.  Carry on checking and swapping each item until you get to the top.  Keep doing this until you&#039;ve checked the whole list and didn&#039;t have to swap anything, that means the list is in order and you can stop.

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.