Tuesday, October 12, 2010

Sorting Algorithms Compared

Time
Sort Average Best Worst Space Stability Remarks
Bubble
sort
O(n^2) O(n^2) O(n^2) Constant Stable Always use a modified bubble sort
Modified
Bubble sort
O(n^2) O(n) O(n^2) Constant Stable Stops after reaching a sorted array
Selection
Sort
O(n^2) O(n^2) O(n^2) Constant Stable Even a perfectly sorted input requires scanning the entire array
Insertion
Sort
O(n^2) O(n) O(n^2) Constant Stable In the best case (already sorted), every insert requires constant time
Heap
Sort
O(n*log(n)) O(n*log(n)) O(n*log(n)) Constant Instable By using input array as storage for the heap, it is possible to
achieve constant space
Merge
Sort
O(n*log(n)) O(n*log(n)) O(n*log(n)) Depends Stable On arrays, merge sort requires O(n) space; on linked lists, merge
sort requires constant space
Quicksort O(n*log(n)) O(n*log(n)) O(n^2) Constant Stable Randomly picking a pivot value (or shuffling the array prior to
sorting) can help avoid worst case scenarios such as a perfectly
sorted array.

Monday, October 11, 2010

GridView, Columns, BoundField, TemplateField, ItemTemplate

GridView class displays the values of a data source in a table where each column represents a field and each row represents a record. The GridView control enables you to select, sort, and edit these items.
When the AutoGenerateEditButton property is set to true, a column (represented by a CommandField object) with an Edit button for each data row is automatically added to the GridView control. Clicking an Edit button for a row puts that row in edit mode. When a row is in edit mode, each column field in the row that is not read-only displays the appropriate input control, such as a TextBox control, for the field's data type. This allows the user to modify the field's value.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="SalesOrderID,SalesDetailID" DataSourceID="DataSource1">

BoundField: Displays the value of a field in a data source. This is the default column type of the GridView control. To specify the field to display in a BoundField object, set the DataField property to the field's name.
Use the DataFormatString property to specify a custom display format for the values that are displayed in the BoundField object. If the DataFormatString property is not set, the field's value is displayed without any special formatting.


<columns>
 <asp:boundfield datafield="CustomerID" readonly="true" headertext="CustID"/>
 <asp:boundfield datafield="CompanyName" convertemptystringtonull="true"
   headertext="Customer Name"/>
<asp:BoundField DataField="ListPrice" HeaderText="ListPrice" 
   SortExpression="ListPrice" DataFormatString="{0:C}" />
</columns>


TemplateField: Displays user-defined content for each item in the GridView control according to a specified template. This column field type enables you to create a custom column field.



<Columns>   
<asp:TemplateField>
     <ItemTemplate>
            <img src='<%# Eval("pic_square") %>' alt="" />
     </ItemTemplate>
</asp:TemplateField>
       <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                 <%# Eval("name") %>
            </ItemTemplate>
      </asp:TemplateField>
<asp:TemplateField>
     <ItemTemplate>
         <asp:LinkButton ID="lnbSend" runat="server" CommandArgument='<%# Eval("uid") %>' OnCommand="lnbSend_Command">Send Message</asp:LinkButton>
     </ItemTemplate>
</asp:TemplateField>
</Columns>

Why use TemplateField instead of BoundField?

The GridView is composed of a set of fields that indicate what properties from the DataSource are to be included in the rendered output along with how the data will be displayed. The simplest field type is the BoundField, which displays a data value as text. Other field types display the data using alternate HTML elements. The CheckBoxField, for example, renders as a check box whose checked state depends on the value of a specified data field; the ImageField renders an image whose image source is based upon a specified data field. Hyperlinks and buttons whose state depends on an underlying data-field value can be rendered using the HyperLinkField and ButtonField field types, respectively.
While the CheckBoxField, ImageField, HyperLinkField, and ButtonField field types allow for an alternate view of the data, they still are fairly limited with respect to formatting. A CheckBoxField can only display a single check box, whereas an ImageField can display only a single image. What if a particular field must display some text, a check box, and an image, all based upon different data-field values? Or what if we wanted to display the data using a Web control other than the CheckBox, Image, HyperLink, or Button? Furthermore, the BoundField limits its display to a single data field. What if we wanted to show two or more data-field values in a single GridView column?
To accommodate this level of flexibility, the GridView offers the TemplateField, which renders using a template. A template can include a mix of static HTML, Web controls, and data-binding syntax. Furthermore, the TemplateField has a variety of templates that can be used to customize the rendering for different situations. For example, the ItemTemplate is used by default to render the cell for each row, but the EditItemTemplate template can be used to customize the interface when editing data.
To summarize here are the applications of TemplateFields
1. Combining two or more data-field values into one column
2. Expressing a data-field value using a Web control instead of text. Example: Displaying hire date using calendar control by setting the VisibleDate and SelectedDate properties to hiredate data field.
3. Used in displaying metadata about the GridView's underlying data. In addition to showing the employees' hire dates, for example, we might also want to have a column that displays how many total days they've been on the job.
4. Another use of TemplateFields arises in scenarios in which the underlying data must be displayed differently in the Web page report from the format in which it's stored in the database. Imagine that the Employees table had a Gender field that stored the character M or F to indicate the sex of the employee. When displaying this information in a web page we might want to show the gender as either "Male" or "Female", as opposed to just "M" or "F".

Sunday, October 10, 2010

Multi threading and Semaphores in C#

using System;
using System.Threading;

class SemaphoreDemo
{
static Sempahore semaphore = new Semaphore(3,3);
public static void Main()
{
for(int i=0; i<10; i++)
{
new Thread(SemaphoreDemo.DoSomething).Start(i);
}
}
static void DoSomething(object id)
{
Console.WriteLine(id+" wants to access the semaphore");
semaphore.WaitOne();
Console.WriteLine(id+" has succeeded to access the semaphore");
Thread.Sleep(1000);
Console.WriteLine(id+" is about to leave the semaphore");
semaphore.Release();
}
}