Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Wednesday, August 26, 2009

Quick Sort

ကြန္ပ်ဴတာ သိပၸံ (Computer Science) ေလ့လာသူေတြနဲ႕ သတင္းနည္းပညာ (IT) ေလ့လာသူေတြအတြက္ အစီအစဥ္ဆြဲနည္း (Programming) သင္ခန္းစာေတြမွာ မပါမျဖစ္ကေတာ့ အခ်က္အလက္မ်ားကုိ စီျခင္း (Sorting) အစီအစဥ္ပါပဲ။ Sorting စီတဲ့ နည္းလမ္းစဥ္ (Algorithm) အမ်ိဳးေပါင္း ေျမာက္မ်ားစြာ ရွိတဲ့အထဲက Bubble Sort, Counting Sort, Insertion Sort, Selection Sort နဲ႕ Quick Sort တုိ႕ဟာ ထင္ရွားပါတယ္။ (စာေရးသူ အႀကိဳက္ဆံုးကေတာ့ Selection Sort ျဖစ္ၿပီး အေၾကာင္းရင္းကေတာ့ နားလည္ရလြယ္လြန္းလုိ႕ ျဖစ္ပါတယ္။) ဒါေပမဲ့ လက္ေတြ႕ဘ၀မွာ အသံုး၀င္တာကေတာ့ နားလည္ရခက္၊ ေရးရတာ လက္၀င္တဲ့ Quick Sort ေလ။ ေရးလုိက္ၾကတဲ့ အစီအစဥ္ေတြတုိင္းမွာ စီစရာ (object, data type) တစ္မ်ိဳး ေပၚလာတုိင္း Quick Sort ကုိ တစ္ခါ ထထ ေရးရမယ္ ဆုိတာကေတာ့ လြန္လြန္းပါတယ္။ ဒါေၾကာင့္ C/C++ စံသတ္မွတ္ခ်က္မွာ Array အားလံုးကို စီႏုိင္ဖုိ႕ qsort ဆုိတဲ့ function ပါေနတာ ျဖစ္ပါတယ္။ ဒီေဆာင္းပါးမွာ အဲဒီ qsort ကုိ အသံုးျပဳနည္းကို ေရးသားေပးမွာ ျဖစ္ပါတယ္။

qsort ရဲ႕ သတ္မွတ္ခ်က္မွာ အစီအစဥ္ေရးသားသူ (programmer) ဒါမွမဟုတ္ ကုဒ္ဒါကို ၿခိမ္းေျခာက္ေနတာကေတာ့ pointer ေတြပါပဲ။ ရုိးရုိး Data Pointer ေတြတင္မကပဲ Function Pointer ႀကီးပါ ပါေနေတာ့ Pointer ဆုိရင္ ေက်ာစိမ့္ေနၾကလူေတြ လန္႕ကုန္တာ မဆန္းပါဘူး။ ဒါေပမဲ့ အေက်ာသိရင္ လြယ္လြယ္ကေလးပါ။ qsort ရဲ႕ အဓိပၸါယ္ သတ္မွတ္ခ်က္ကို အရင္ၾကည့္ရေအာင္ေနာ္။

void qsort ( void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) );

ပထမဆံုး return type ကေတာ့ ဒီ function ဟာ return မျပန္ပါဘူးလုိ႕ အဓိပၸါယ္ရပါတယ္။ ဆုိလုိတာက ေပးလိုက္တဲ့ array ကုိ စီပဲ စီေပးမယ္။ ဘာ ဂဏန္းမွ တြက္ၿပီး အေျဖမထုတ္ေပးပါဘူးေပါ့။

ပထမဆံုး parameter က စီရမဲ့ array ရဲ႕ ပထမဆံုးအကြက္ရဲ႕ လိပ္စာ (address) ပါ။ အမ်ားေၾကာက္တဲ့ pointer ေပါ့။ Array အားလံုးဟာ Fix Pointer ေတြ ျဖစ္တဲ့အတြက္ စိတ္ခ်လက္ခ်ပဲ array name ကုိ ျဖည့္လုိက္လုိ႕ရပါတယ္။

ဒုတိယ parameter ကေတာ့ Array ရဲ႕ အရြယ္အစား (size) ျဖစ္ပါတယ္။ C/C++ မွာ Array ရဲ႕ size ကုိ compile-time/run-time check မလုပ္ပဲ ကုဒ္ဒါကပဲ အၿမဲ ကုိင္တြယ္ ႀကီးၾကပ္ရတာမုိ႕ ဒါကို ထည့္ေပးရတာပါ။ ႏုိ႕မုိ႕ရင္ ဘယ္ႏွခု စီရမလဲ မသိပဲ ျဖစ္ေနမယ္ေလ။ ကုိယ့္ Array မွာ Element ၁၀ ခုပါရင္ 10 လုိ႕ထည့္ေပါ့။ Variable တစ္ခုခု ထည့္လုိ႕လဲ ရပါတယ္။

တတိယ parameter ကေတာ့ စီခ်င္တဲ့ Array Element တစ္ခုခ်င္းရဲ႕ အရြယ္အစားေပါ့။ အဲဒါမွ အဲဒီ မွတ္ဥာဏ္တံုးႀကီး (memory block) ေတြကို ေရႊ႕လုိ႕ရမယ္ေလ။ ဒါလဲ မခက္ပါဘူး။ sizeof(element) လုိ႕ ေပးလုိက္ရင္ရပါတယ္။

စတုတၳနဲ႕ ေနာက္ဆံုး parameter ကေတာ့ နည္းနည္း ရွင္းျပရ လက္၀င္ပါတယ္။ ဒါဟာ Function Pointer ႀကီးျဖစ္ပါတယ္။ ကုိယ့္ဘာသာကုိယ္ သတ္မွတ္ထားတဲ့ Element ေတြကို ႏိႈင္းယွဥ္ခ်င္ရင္ ဒီ Function နဲ႕ တြက္ဆုိၿပီး ေျပာရတဲ့ သေဘာပါ။ (ေနာက္တမ်ိဳးေျပာရရင္ အဲဒီ Function ႀကီးကုိ qsort ကေန လွမ္းေခၚမွာ ျဖစ္ပါတယ္။ အဲဒီ Function ႀကီးက qsort ဆီကုိ ေရွ႕ကေကာင္က ငယ္ရင္ အႏႈတ္ကိန္း၊ တူရင္ သုည၊ ႀကီးရင္ အေပါင္းကိန္း တစ္ခုခု return ျပန္ရပါမယ္။) အဲဒီ Function ႀကီးရဲ႕ ပံုစံကလဲ

int function_name(const void* a, const void* b);

ပဲျဖစ္ရပါမယ္။ ပုိနားလည္လြယ္ေအာင္ ဥပမာေလးနဲ႕ ၾကည့္ရေအာင္ေနာ္။

ဥပမာ ၀န္ထမ္းေတြကို လစာ အမ်ားဆံုးကေန အနည္းဆံုးကို စီၿပီးသား အစီရင္ခံစာထုတ္ေပးတဲ့ အစီအစဥ္ကုိ လုိခ်င္တယ္။ တကယ္လုိ႕ လစာတူေနရင္ လုပ္သက္ရင့္တဲ့လူကုိ ေရွ႕မွာ ထားခ်င္တယ္ ဆုိပါစုိ႕။ ဒါဆုိရင္ ၀န္ထမ္းေတြရဲ႕ အခ်က္အလက္ေတြကုိ သိမ္းဖုိ႕ class တစ္ခု ေဆာက္ရပါမယ္။

class employee{

int experience; //in year

float salary; //in kyat

/* Blar Blar Blar */

public:

bool operator < (employee &e) { If(this->salary == e.salary) return (this->experience <>salary < temp_e =" (employee*)" temp_f =" (employee*)">' ကုိ ဒီလုိေလး temp_e->my_emp_function() ဆုိၿပီးေတာ့ သံုးေပးရပါမယ္။) အဲဒီ '*' ေလးကို ထည့္ၿပီး operator '>' နဲ႕ ႏိႈင္းယွဥ္လုိ႕ရလာတဲ့ အေျဖေပၚမူတည္ၿပီး -1 သုိ႕မဟုတ္ +1 ကို qsort အတြက္ return ျပန္ေပးလုိက္ရပါမယ္။

ဒီ Function ေလးေရးလုိ႕ အခ်ိဳးေျပသြားၿပီဆုိရင္ေတာ့ qsort ကုိ စိတ္ေအးခ်မ္းသာ အသံုးျပဳႏုိင္ပါၿပီ။ (employee ရဲ႕ private member ေတြကို accessor method ေတြကေနတဆင့္ ေခၚထားတာ သတိျပဳပါ။ တကယ္တမ္း ေရးတဲ့အခါက်ေတာ့ သတိလက္လြတ္ ဒီတုိင္း ေရးမိတတ္ပါတယ္။ အဲဒီလုိေရးခ်င္ရင္ေတာ့လဲ friend လုပ္လုိက္ေပါ့ဗ်ာ၊ ေနာ။) ေအာက္မွာက main Function ပါ။

/* preprocessor Yada Yada Yada */

void main(void)

{

employee myEmployeeArray[NUM_OF_EMPLOYEE];

/* input data into myEmployeeArray */

qsort(myEmployeeArray, NUM_OF_EMPLOYEE, sizeof(employee), employee_comparer);

/* output sorted myEmployeeArray */

}

qsort ကုိ လြယ္လြယ္ေခၚၿပီး ကုဒ္ဒါအေပါင္း သက္သာၾကပါေစေၾကာင္း …


Source : ကိုေလာရွည္ blog http://opera.lawshay.com/2009/08/quick-sort-sorting.html

Read More...

Friday, August 7, 2009

Unary Operators

Not all operators in mathematics expressions are binary operators. Some are unary operators.

As you may guess, a unary operator accepts only one operand.
The positive sign and the negative sign are each unary operators. They accept only one value when they do arithmetic.
The positive sign looks a lot like the addition operator. For example, consider this expression:

+2

Is read 'positive two'. It does not really mean 'add two'.

What is happening here from an expression evaluation viewpoint is that the value of two represented by the number 2 is accepted as a single operand by the unary operator, '+', and that operation produces a value of positive two.

But, of course, the value of two represented by the number 2 is positive to start with, so, this operation is not too exciting, (no pun intended).

Obviously, '+2' and '2' each represent the same value. So, one can say that an 'invisible' positive sign can be imagined before any number, (or variable or function, for that matter).

As mentioned above, the negative, (or opposite), sign is a negative operator. Consider this expression:

-3

Technically, here the negative sign operator accepts a value of three as its operand and produces a value of negative three.

Obviously, '-3' and '3' represent different values. There are no invisible negative signs in mathematics expressions.

These two unary operations, '+' and '-', have precedence over all binary operations. To see this, let's look at a situation which is often termed a 'double negative' or stated as 'two negatives make a positive'. Consider this expression:

3 - -2

In this expression there are two operators that look like this: '-'. They are not the same. The first one, (left most), is the binary subtraction operator. The other, (right before the 2), is the unary negative sign operator.

From an expression evaluation viewpoint, things proceed as follows. The negative sign operator has precedence over the subtraction operator, so it accepts operands first. It accepts only one operand, since it is a unary operator. It accepts the value of two and produces a value of negative two. Now, the subtraction operator gets its chance to accept values. It takes the three and from it subtracts the negative two. This produces five, represented by the number 5, so we can write:

5 = 3 - -2

Now, this expression also equals five:

3 + 2

Since two expressions both equaling five equal each other we can write:

3 + 2 = 3 - -2

Although the expressions to the left and right of the equal sign above produce the same value, they each have quite a different story going on regarding the interaction of operators and operands.

So, the positive sign operator is not the addition operator, and the negative sign operator is not the subtraction operator.

But they look so much alike! Or do they?

Actually, the positive sign operator is often differentiated from the addition operator by making it a bit smaller and lifting it up a bit, as in:

+2 + +3

That would read 'positive two added to positive three'.

A similar visual difference is often used for the negative sign operator and the subtraction operator.

What was written above in the 'double negative' discussion:

3 - -2

Is much more clearly written this way:

3 - -2

That makes it very clear that the negative sign is a different operator than the subtraction operator. This last expression would be read 'three subtract negative two'.

There is really no 'double negative' here. One operation is binary subtraction, the other is unary negation. The combination of these in like expressions may evaluate to a value equal to a similar looking expression involving an addition operator, and an addition operator may look like a positive sign, but it is really not the best mathematics to explain the fact that:

3 + 2 = 3 - -2

Using terms like 'double negative' or 'two negatives make a positive'.

So, is there ever a double negative or do two negatives ever make a positive?

Well, yes, if there really are two negative sign operators present, (and not one subtraction operator and one negative sign operator). Consider this expression:

--2

Here there are two unary negative sign operators looking around, as we might say, for operands. They have equal precedence, so, which one accepts operands first?

Unlike the situation for binary operators where in situations of equal precedence evaluation proceeds from left to right, here, with unary operators of equal precedence evaluation goes from right to left.

So, the value of two is accepted by the right most negative sign producing a value of negative two, or the opposite of two. This value is handed over to the left most negative sign producing the negative of negative two, or the opposite of the opposite of two, which is positive two.

In this case, then, it is fine to talk about double negatives and say that two negatives produce a positive. Now, with complete clarity we can write:

+2 = --2

Collecting a few ideas about expression evaluation:

* There are binary and unary operators.
* Unary operators have precedence over binary operators.
* Resolutions regarding binary operations of equal precedence proceed left to right.
* Resolutions regarding unary operations of equal precedence proceed right to left.


http://id.mind.net/~zona/mmts/expressions/unaryOps.html

Read More...

Operands

In all computer languages, expressions consist of two types of components: operands and operators. Operands are the objects that are manipulated and operators are the symbols that represent specific actions. For example, in the expression

5 + x



x and 5 are operands and + is an operator. All expressions have at least one operand.

Read More...

Saturday, July 4, 2009

Thouands of thousand records

ဒီေန႕ Database ထဲက Row ေပါင္း ၇ ေသာင္းေလာက္ ကို ဘယ္လို Display ျပရင္ေကာင္းမလဲဆိုတာ စဥ္းစားမိတယ္။ အခု လုပ္ေနတဲ့ Project က row ေပါင္း ၇ ေသာင္းေက်ာ္မယ္တဲ့။

အခုစမ္းေနတာ ၂၀၀၀ ေလာက္တင္ Datagridview မွာ သိပ္အဆင္မေျပဘူး။ ေနာက္ေတာ့ web မွာလို page ေတြ ခြဲထုတ္ဖုိ႕ ဆံုးျဖတ္လုိက္တယ္။ Access မွာေတာ့ MySQL Limit မရိွဘူး။ wiki [1] အကူအညီနဲ႕ Top နဲ႕ လုပ္ရတယ္ ဆိုတာ သိသြားတယ္။
Access မွာpage 1 မွာ ၅၀ ျပျပီးသြားရင္ page 2 ကိုselect top 50 * from table where ID Not IN (select top 50 * from table)
အဲလိုေလး ျပန္ထိန္းရတယ္။ ေနာက္ကေတာ့ where နဲ႕ filter ျပန္လုပ္ရတယ္။ ပါျပီးသား ID ေတြ မဟုတ္တာဆိုျပီး ေရြးထုတ္လိုက္တဲ့ ပံုေလးေပါ့။ page ခြဲပံု idea ကိုေတာ့
http://code.mysteryzillion.org/article/separate-page-from-row-record
မွာ ဖတ္ႏိုင္ပါတယ္။ ကၽြန္ေတာ္တို႕ေတြအေနနဲ႕ record ေတြမ်ားလာေလေလ… အမ်ားၾကီးကို တစ္ခါတည္း တန္းမျပပဲ page ေလးေတြ ခြဲလိုက္ျခင္းဟာ အသံုးျပဳသူအတြက္ ေတာ္ေတာ္အဆင္ေျပေစပါတယ္။ Memory သံုးတာ ေတာ္ေတာ္သက္သာသြားတယ္။ Database သံုးတာ ေတာ္ေတာ္သက္သာသြားတယ္။ အခုေတာ့ အဆင္ေျပသြားပါျပီဗ်ာ။
http://en.wikipedia.org/wiki/Select_(SQL)#Limiting_result_rows

Related Posts
MySQL Reference Book
CSV to Mysql
Database Charset UTF-8

Read More...

Friday, May 8, 2009

stack and heap

The stack is a place in the computer memory where all the variables that are declared and initialized before runtime are stored. The heap is the section of computer memory where all the variables created or initialized at runtime are stored.

more detail download -

Read More...

Increment and Decrement Operators

++i add 1 to i
--j subtract 1 from j

These correspond to the slightly longer i += 1 and j -= 1, respectively, and also to the fully ``longhand'' forms i = i + 1 and j = j - 1.

The ++ and -- operators apply to one operand (they're unary operators). The expression ++i adds 1 to i, and stores the incremented result back in i. This means that these operators don't just compute new values; they also modify the value of some variable. (They share this property--modifying some variable--with the assignment operators; we can say that these operators all have side effects. That is, they have some effect, on the side, other than just computing a new value.)

The incremented (or decremented) result is also made available to the rest of the expression, so an expression like

k = 2 * ++i
means ``add one to i, store the result back in i, multiply it by 2, and store that result in k.'' (This is a pretty meaningless expression; our actual uses of ++ later will make more sense.)
Both the ++ and -- operators have an unusual property: they can be used in two ways, depending on whether they are written to the left or the right of the variable they're operating on. In either case, they increment or decrement the variable they're operating on; the difference concerns whether it's the old or the new value that's ``returned'' to the surrounding expression. The prefix form ++i increments i and returns the incremented value. The postfix form i++ increments i, but returns the prior, non-incremented value. Rewriting our previous example slightly, the expression

k = 2 * i++
means ``take i's old value and multiply it by 2, increment i, store the result of the multiplication in k.''
The distinction between the prefix and postfix forms of ++ and -- will probably seem strained at first, but it will make more sense once we begin using these operators in more realistic situations.

1++
or
(2+3)++

The ++ operator doesn't just mean ``add one'';
it means ``add one to a variable'' or ``make a variable's value one more than it was before.''
But (1+2) is not a variable, it's an expression;
so there's no place for ++ to store the incremented result.

Another unfortunate example is

i = i++;
which some confused programmers sometimes write, presumably because they want to be extra sure that i is incremented by 1. But i++ all by itself is sufficient to increment i by 1; the extra (explicit) assignment to i is unnecessary and in fact counterproductive, meaningless, and incorrect.
If you want to increment i (that is, add one to it, and store the result back in i), either use
i = i + 1;
or
i += 1;
or
++i;
or
i++;

Don't try to use some bizarre combination.
Did it matter whether we used ++i or i++ in this last example? Remember, the difference between the two forms is what value (either the old or the new) is passed on to the surrounding expression. If there is no surrounding expression, if the ++i or i++ appears all by itself, to increment i and do nothing else, you can use either form; it makes no difference.

for(i = 0; i < 10; ++i)
printf("%d\n", i);
and

for(i = 0; i < 10; i++)
printf("%d\n", i);

will behave exactly the same way and produce exactly the same results. (In real code, postfix increment is probably more common, though prefix definitely has its uses, too.)

http://c-faq.com/~scs/cclass/notes/sx7b.html

Read More...

Friday, February 13, 2009

Developing a Program

If you think that learning to program is simply a matter of a programming language you are very wrong.

I am going to start on the basis that you are writing your programs for a customer. He or she has problem and would like you to write a program to solve it. We shall assume that the customer knows even less about computers than we do!
Initially we are not even going to talk about the programming language, type of
computer or anything like that, we are simply going to make sure that we know what the
customer wants.

Solving the Wrong Problem
Many software projects have failed
because the problem that they solved was the wrong one. The developers of the system
quite simply did not find out what was required, but instead created what they thought
was required.
This is a kind of self discipline. Programmer’s pride themselves on their ability to
come up with solutions, so as soon as they are given a problem they immediately start
thinking of ways to solve it, this almost a reflex action. What you should do is think
"Do I really understand what the problem is?". Before you solve a problem you
should make sure that you have a watertight definition of what the problem is, which both you and the customer agree on. In the real world this is sometimes called a
Functional Design Specification or FDS. This tells you exactly what the customer
wants. Both you and the customer sign it, and the bottom line is that if you provide a
system which behaves according to the design specification the customer must pay
you. Once you have got your design specification, then you can think about ways of
solving the problem. Writing an FDS forces you to think about your problem at a very detailed level.

Specifying the Problem

Read More...