## vibudh is talking

### Counting Sort – An optimised technique

In Computer Science, counting sort is a way of sorting of a collection of numbers found in a predefined range. This property is used in Counting Sort technique to make the running time linear in the number of items and the maximum key values. So it is very useful in situations where the number of items are significantly larger than the range. Counting Sort in used mostly as a subroutine in Radix Sort, which makes the sorting technique efficient even with larger values of the range.

A basic algorithm of the Counting Sort is given below that is used and discussed in most places:

/*array[] is the original array with length array.lenght
f[]  and cf[] are the frequency and cumulative frequency with length r(length of the range)
s[] is the sorted array
*/

for( int i = 0; i < array.length; i++)
f[array[i]]++;                                        //Calculating the Frequency for each element

cf[0] = f[0];                                            //Calculating the Cumulative Frequency for each element
for(int i = 1; i < r-1; i++)
cf[i] = f[i] + cf[i-1];

for( int i = n-1; i >= 0; i–)
{ s[cf[a[i]]] = a[i];
cf[a[i]]–;
}

We can see that in the algorithm discussed worldwide there is use of 2 extra arrays for calculating the frequency and cumulative frequency for the elements. Now there is a simpler method for the Counting Sort that we are going to discuss below:

/*array[] is the original array with length array.lenght
f[] is the frequency with length r(length of the range)
s[] is the sorted array
*/

for( int i = 0; i < array.length; i++)
f[array[i]]++;                                        //Calculating the Frequency for each element

int count =0;                                        //variable for the value of sorted elements
for( int i = 0; i<r; i++)                      //putting the value one by one
while ( f[i] > 0 )
{s[count] = i;
f[i]–;
count++;
}

I consider this algorithm better for Counting sort method as it is a simpler method with lesser steps and does not uses an extra array for storing the cumulative frequency of the array.

### Inserting Mouse Pointer in your C++ program (for TC)

The C++ code that we are going to discuss is a simple part of a program that can be used to insert a mouse pointer in your C++ program, however the code is self sufficient and will work perfectly in any standard C++ compiler. This part of the program when inserted in your C++ program will make your project user friendly and at the same time give you an edge over other programs by enhancing the execution. It is advisable to include this part of the program before you start the project. Any relevant query will be most welcome. The source code for inserting the mouse pointer is as follows with relevant comment lines.

```
/*relevant header files for the program */
#include
#include
#include
#include
#include

int intmouse();
/*function to show mouse pointer*/
void showmouseptr();

/*gets the current position of the mouse */
void getmousepos(int *, int *, int*);

/*restricts the mouse in the graphical area */
void restrictmouseptr(int x,int y,int x1,int y1);

union REGS i,o,a,b;
int intmouse()
{
i.x.ax=0;
int86(0x33,&i,&o);
return(o.x.ax);
}

void showmouseptr()
{
i.x.ax=1;
int86(0x33,&i,&o);
}

void restrictmouseptr(int x,int y,int x1,int y1)
{
i.x.ax=7;
i.x.cx=x;
i.x.dx=x1;
int86(0x33,&i,&o);
i.x.ax=8;
i.x.cx=y;
i.x.dx=y1;
int86(0x33,&i,&o);
}

void getmousepos(int *p,int *x,int *y)
{
i.x.ax=3;

int86(0x33,&i,&o);
*p=o.x.bx;
*x=o.x.cx;
*y=o.x.dx;
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"\\tc\\bgi");
int x=getmaxx();
int y=getmaxy();
if(intmouse()==0)
exit(1);
showmouseptr();
cleardevice();
setcolor(WHITE);
setbkcolor(BLACK);
int button,n,m;
outtextxy(x-100,y-50,"Exit...");
getmousepos(&button,&n,&m); /*n=x-coordinate& m=y-coordinate*/
int z=1;
restrictmouseptr(1,1,x-1,y-1);

while(z)
{
getmousepos(&button,&n,&m);
/*this will keep in track with the current position of the mouse*/

/*displaying the current position of the mouse */
gotoxy(1,1);
cout<<"n ="<<n<=532 && n<=592) && (m>=429 && m<=444))
{
exit(0);
button=0;
z=0;
}
}

closegraph();
restorecrtmode();
getch();
closegraph();
restorecrtmode();
}```

Now we will be discussing how this programme actually works and what does each function do. initgraph() is used to initialize the graph grid. showmouseptr() is used to show the mouse pointer in the output screen. cleardevice() is simply used to clear the scree. getmouseposition(&button,&n,&m) is used to get the present status of the mouse button and its present coordinates. restrictmouseptr() is used to prevent the mouse pointer to go out of bouonds, so that is does not go beyond the display screen. Here as you can see in the main() we are using a while loop, that stays true, and has infinite loop. This is important because we need the dynamic execution of the programme as long as the user does not press Exit… to get out of the programme. This while loop is also the place where other dynamic components of the program will be executed, as we discuss in some other projects that we take on. Hope this programme helps you in making your programme more interactive and user friendly.