Thursday, September 17, 2009

Sorting process by anil nain from sonepat haryana


Sorting: Sorting is the process of arranging a set of similar information into an increasing or decreasing order. Sorting is one of the most intellectually pleasing category because the process is so well defined. We can use function qsort( ) for sorting but it will sort array in memory not linked lists. Finally qsort( ) is very effective in general case but not the best for all situations.

Selection Sort : A selection sort selects the element with the lowest value and exchanges it with first element. Then from the remaining n-1 elements, the element with smallest value is found and exchanged with the second element, and so forth. The exchanges continue to the last two elements. For example, if selection sort method is used on the array dcab, each pass would like this:

Initial d c a b
Pass 1 a c d b
Pass 2 a b d c
Pass 3 a b c d
n / 6 log n
Average Number of Exchanges = .


Bubble Sort : The most well known and infamous sort is Bubble Sort. Its popularity is derived from its catchy name and simplicity. However, for general purpose sorting, it is one of the worst sort sorts ever convinced.
The Bubble Sort is an exchange sort. It involves repeated comparisons and, if necessary exchange of adjacent elements. The elements are just like bubbles in a tank of water – each seeks its own level. For example, if bubble sort method is used on the array dcab, each pass is shown here:

Initial d c a b
Pass 1 a d b c
Pass 2 a b d c
Pass 3 a b c d

In analyzing any sort, it is useful to have an idea about comparisons and exchanges performed for best, average, worst case. With Bubble Sort, the no. of comparisons are always same because two for loops repeat the specified no. of times whether the list is initially ordered or not.
1 / 2(n2-n)
Average Number of Exchanges =

Where n is no. of elements to be sorted. The formula is derived from the fact that outer for loop executes n-1 times while inner one n/2 times. Multiplied together results in the preceding formula. For Bubble Sort :
Case No. of Exchanges. Remarks
Best Zero Already Sorted List
Average 1/2(n2-n) -
Worst 1/2(n2-n) -
Sorting process by anil nain from sonepat haryana

Tuesday, September 15, 2009

Binary Search by anil nain


Binary Search: If data to be searched in sorted array, In such cases we use a vastly superior method to find a match named Binary Search which uses the divide and conquer approach. To employ this method, test the middle element. If it is larger than key(element to be searched), test the middle element of the first half; otherwise test the middle element of lower half. Repeat this procedure until a match is found or there is no more elements to test.

For Example, to find number 4 in Array :
1 2 3 4 5 6 7 8 9

A binary search first tests the middle element, which is 5. Since 5 is greater than 4, the search continues in first half, or
1 2 3 4 5

The middle element is now 3. This is less than 4, so first half is discarded, and search continues with
4 5
This time the match is found.

No. of Comparisons (Worst Case) = log2 n
No. of Comparisons (Best Case) = 0
No. of Comparisons for Average Case are somewhat lower as compared to Worst Case.
by anil nain 9416077273


Friday, September 4, 2009

Grouping Data From Tables In SQL FROM anil nain 9416077273

Grouping Data From Tables In SQL
The concept of grouping
From sale_order_details table, we well create a set containing several sets of rows groups together based on a condition.
Table name: sales_order_details
S Order No.
Product Qty Ordered
Qty. Ordered
Qty. Disp
019001
P00001
10
10
019001
P03453
3
3
019001
P06734
3
3
046865
P06734
4
4
046865
P03453
10
10
046865
P03453
2
2
073965
P00001
1
1
073965
P06734
1
1
Example
Select product_no, total qty_ordered for each product;
SELECT Product_no, sum(qty_ordered) “Total Qty Ordered”
FROM slaes_order _details
GROUP BY product_no;
The following is the output displayed:
Product No. Total Qty. Ordered
------------------ ---------------------------
P00001 13
P03453 15
P06734 8

A condition cab be imposed on the group by clause, using the having clause as done in the following example:
1. Select product_no, total qty_ordered for products ’P00001’ OR ‘P03454’,
Select product_no, sum(qty_ordered)”Total Qty Ordered”
FROM sales_order_details
GROUP BY product_no
HAVING product_no=’P00001’ OR ‘P03454’,
The following output displayed:
Product No. Total Qty Ordered
---------------- -------------------------
P00001 13
P03453 15