can someone use a bubblesort function on my project ?

i arranged the sequence for the program and the teacher doesnt allow arrange sequence and then running a for loop to run it , she insisted on bubblesort . therefore anyone out there pls change my program for me , by the way i am using turbo C






Months S$
Jan 78
Feb 67
March 82
April 55
May 73
June 69
July 76
August 81
September 59
October 64
November 77
December 61

Write a sending and receiving programs such that when ‘1’ is pressed at the sending PC, the receiving PC will receive it and display the phone bills in descending order and in odd months only . when ‘2’ is pressed at the sending pc , the receiving PC will display the phone bills in ascending order and in even months only . When esc key is pressed at both PC, both programs will quit.
Use array and pointer to write both programs. The communication parameters are 7 bits character. 1 stop bit, even parity, 9600 baud rate, com 2 port.







SENDING PROGRAME

#include<bios.h>
#include<conio.h>
#include<stdio.h>

#define ESC 0x1b
#define COM2 1
#define SETTING (0x02|0x00|0x18|0xE0)

void main()
{
int tx,status,rx;
clrscr();
bioscom (0, SETTING,COM2);
printf("\nType numbers to transmit\n");
printf("\nPress <ESC> to quit communication\n");



while(1)
{

tx=getch();

if(tx==ESC)
{
break;
}
else if(tx==0x31)
{
printf("command 1 is selected\n");
bioscom(1,tx,0);
}

else if(tx==0x32)
{
printf("command 2 is selected\n");
bioscom(1,tx,0);
}



bioscom(1,0,COM2);
status = bioscom(3,0,COM2);

if((status & 0x100)!=0)
{
rx=bioscom(2,0,COM2);

if((rx & 0xE00)!=0)
printf("Error in data");

else
{
rx = rx & 0xff;

if(rx==ESC)
{
break;
}
}
}
}
}










RECEIVE PROGRAM



#include<bios.h>
#include<conio.h>
#include<stdio.h>

#define ESC 0x1b
#define ENTER 0x0D
#define DATA_RDY 0x100
#define COM2 1
#define SETTING (0x02 | 0x00 | 0x18 | 0xE0)


void main()
{
int rx,k;

char oddmonth[7][20]={"Mar","jan","Nov","Jul","May","Sep"};
char evenmonth[7][20]={"Apr","Dec","Oct","Feb","Jun","Aug"};
int oddbill[7] = {82,78,77,76,73,59};
int evenbill[7] ={55,61,64,67,69,81};


int *poddbill;
int *pevenbill;

poddbill=oddbill;
pevenbill=evenbill;

clrscr();
printf("\nPress (ESC) to quite communication\n");
bioscom(0,SETTING,COM2);
while(1)
{
bioscom(1,0,COM2);
if(bioscom(3,0,COM2)&DATA_RDY)
{
rx=bioscom(2,0,COM2);
if((rx&0xe00)!=0 )
{
printf("Warning !Error in received data");
}
else
rx=rx&0xFF;

printf("months\tS$\n");
if(rx==0x31)
{


for(k=0;k<6;k++)
printf("%s\t%d\n",oddmonth[k],*(poddbill+k));


}

if(rx==0x32)



for(k=0;k<6;k++)
printf("%s\t%d\n",evenmonth[k],*(pevenbill+k));



}
if (kbhit())
{
if(getch()==ESC)
{
break;

}
}
}
}







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <algorithm>

template <typename Type>
bubbleSort(Type* array, size_t size)
{
    for (int i = 0; i < size - 1; ++i)
    {
        for(int j = i + 1; j < size; ++j)
        {
            if(arr[i] > [arr[j])
            {
                std::swap(arr[i], arr[j]);
            }
        }
    }
}
hi denis where do i put that part to ?
The my presented function must be situated on the top of your source file(or you can create new header file and add function to one). And then where you need to sort you just call it;
 
bubbleSort(myData, sizeOfMeData);

where:
myData is your array that should be sorted
sizeOfMeData is size of your array(quantity of elements in myData
closed account (1yR4jE8b)
@Denis

That's not a bubble sort, that's an almost properly implemented Selection Sort.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
template <class T>
void bubbleSort(T* data, const int len)
{
	for(int i = 0; i < len - 1; ++i)
	{
		for(int j = 0; j < len - 1; ++j)
		{
			if(data[j] > data[j + 1])
			{
                                //element swap
				T* temp = new T(data[j]);
				data[j] = data[j + 1];
				data[j + 1] = *temp;
				delete temp;
			}
		}
	}
}


This is a basic bubble sort, obviously there are other optimizations but this is the barebones algorithm.
I can argue with you, because our algorithm the same.

one element compare with next and they change locations.
closed account (1yR4jE8b)
I can argue with you too, because our algorithms are different.

Just let me say that I would rather use your algorithm than mine, I'm just saying that your sort isn't a bubble sort.

Bubble sort compares every element with the element that is directly beside it (which mine does), yours does not. Your compares the element at position i with an increasingly farther element j.

It chooses element i, (which is fixed) for ever iteration over the loop for j. Your algorithm is a Selection Sort, however Selection Sort typically saves the smallest value over the iteration and
does the swap at the end of the nested loop instead of every time it encounters a smaller value.
Topic archived. No new replies allowed.