Thursday, July 29, 2010

Accessing files on internet Internet—Downloading Files

 Accessing files on internet Internet—Downloading Files


Downloading Files


Two methods are available for downloading a file using WebClient. The method you choose depends on how you want to process the file's contents. If you simply want to save the file to disk, then you use the DownloadFile() method. This method takes two parameters: the URI of the file and a location (path and file name) to save the requested data:


WebClient Client = new WebClient();
Client.DownloadFile("http://www.reuters.com/", "ReutersHomepage.htm");

More commonly, your application will want to process the data retrieved from the Web site. To do this, you use the OpenRead() method, which returns a Stream reference that you can then use to retrieve the data into memory:


WebClient Client = new WebClient();
Stream strm = Client.OpenRead("http://www.reuters.com/");

ASP.NET - Shell Sort

 Shell Sort


The shell sort is a "diminishing increment sort", better known as a "comb sort" to the unwashed programming masses. The algorithm makes multiple passes through the list, and each time sorts a number of equally sized sets using the Insertion Sort. The size of the set to be sorted gets larger with each pass through the list, until the set consists of the entire list. (Note that as the size of the set increases, the number of sets to be sorted decreases.)


The items contained in each set are not contiguous - rather, if there are i sets then a set is composed of every i-th element. For example, if there are 3 sets then the first set would contain the elements located at positions 1, 4, 7 and so on. The second set would contain the elements located at positions 2, 5, 8, and so on; while the third set would contain the items located at positions 3, 6, 9, and so on.


The size of the sets used for each iteration has a major impact on the efficiency of the sort.


The shell sort is more than 5 times faster than the bubble sort and a little over twice as fast as the Insertion Sort, its closest competitor.


The shell sort is still significantly slower than the Merge Sort, Heap Sort, and Quick Sorts, but its relatively simple algorithm makes it a good choice for sorting lists of less than 5000 items unless speed is hyper-critical. It's also an excellent choice for repetitive sorting of smaller lists.






Shell Sort Routine Code


// array of integers to hold values
private int[] a = new int[100];
 
// number of elements in array
private int x;
 
// Shell Sort Algorithm
public void sortArray()
{
int i, j, increment, temp;
 
increment = 3;
 
while( increment > 0 )
{
for( i=0; i < x; i++ )
{
j = i;
temp = a[i];
 
while( (j >= increment) && (a[j-increment] > temp) )
{
a[j] = a[j - increment];
j = j - increment;
}
 
a[j] = temp;
}
 
if( increment/2 != 0 )
{
increment = increment/2;
}
else if( increment == 1 )
{
increment = 0;
}
else
{
increment = 1;
}
}
}




ASP.NET - -Selection Sort

 Selection Sort


The selection sort works by selecting the smallest unsorted item remaining in the list, and then swapping it with the item in the next position to be filled.


This is a performance improvement over the bubble sort, but the Insertion Sort is over twice as fast as the Bubble Sort and is just as easy to implement as the selection sort. In short, there really isn't any reason to use the selection sort - use the Insertion Sort instead.


If you really want to use the selection sort for some reason, try to avoid sorting lists of more than a 1000 items with it or repetitively sorting lists of more than a couple hundred items.






Selection Sort Routine Code


// array of integers to hold values
private int[] a = new int[100];
 
// number of elements in array
private int x;
 
// Selection Sort Algorithm
public void sortArray()
{
int i, j;
int min, temp;
 
for( i = 0; i < x-1; i++ )
{
min = i;
 
for( j = i+1; j < x; j++ )
{
if( a[j] < a[min] )
{
min = j;
}
}
 
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}

ASp.NET - Quick Sort

Quick Sort

The quick sort is an in-place, divide-and-conquer, massively recursive sort. As a normal person would say, it's essentially a faster in-place version of the merge sort. The quick sort algorithm is simple in theory, but very difficult to put into code.

The recursive algorithm consists of four steps:


  1. If there are one or less elements in the array to be sorted, return immediately.

  2. Pick an element in the array to serve as a "pivot" point. (Usually the left-most element in the array is used.)

  3. Split the array into two parts - one with elements larger than the pivot and the other with elements smaller than the pivot.

  4. Recursively repeat the algorithm for both halves of the original array.


The quick sort is by far the fastest of the common sorting algorithms. It is possible to write a special-purpose sorting algorithm that can beat the quick sort for some data sets, but for general-case sorting there isn't anything faster.


The quick sort is recursive, which can make it a bad choice for applications that run on machines with limited memory.






Quick Sort Routine Code


// array of integers to hold values
private int[] a = new int[100];

// number of elements in array
private int x;

// Quick Sort Algorithm
public void sortArray()
{
q_sort( 0, x-1 );
}

public void q_sort( int left, int right )
{
int pivot, l_hold, r_hold;

l_hold = left;
r_hold = right;
pivot = a[left];

while( left < right )
{
while( (a[right] >= pivot) && (left < right) )
{
right--;
}

if( left != right )
{
a[left] = a[right];
left++;
}

while( (a[left] <= pivot) && (left < right) )
{
left++;
}

if( left != right )
{
a[right] = a[left];
right--;
}
}

a[left] = pivot;
pivot = left;
left = l_hold;
right = r_hold;

if( left < pivot )
{
q_sort( left, pivot-1 );
}

if( right > pivot )
{
q_sort( pivot+1, right );
}
}




ASP.Net - Merge Sort

Merge Sort


The merge sort splits the list to be sorted into two equal halves, and places them in separate arrays. Each array is recursively sorted, and then merged back together to form the final sorted list.


Elementary implementations of the merge sort make use of three arrays - one for each half of the data set and one to store the sorted list in. The below algorithm merges the arrays in-place, so only two arrays are required. There are non-recursive versions of the merge sort, but they don't yield any significant performance enhancement over the recursive algorithm on most machines.


The merge sort is slightly faster than the Heap Sort for larger sets, but it requires twice the memory of the Heap Sort because of the second array. This additional memory requirement makes it unattractive for most purposes - the Quick Sort is a better choice most of the time and the Heap Sort is a better choice for very large sets.


Like the Quick Sort, the merge sort is recursive which can make it a bad choice for applications that run on machines with limited memory.






Merge Sort Routine Code


// array of integers to hold values
private int[] a = new int[100];
private int[] b = new int[100];

// number of elements in array
private int x;

// Merge Sort Algorithm
public void sortArray()
{
m_sort( 0, x-1 );
}

public void m_sort( int left, int right )
{
int mid;

if( right > left )
{
mid = ( right + left ) / 2;
m_sort( left, mid );
m_sort( mid+1, right );

merge( left, mid+1, right );
}
}

public void merge( int left, int mid, int right )
{
int i, left_end, num_elements, tmp_pos;

left_end = mid - 1;
tmp_pos = left;
num_elements = right - left + 1;

while( (left <= left_end) && (mid <= right) )
{
if( a[left] <= a[mid] )
{
b[tmp_pos] = a[left];
tmp_pos = tmp_pos + 1;
left = left +1;
}
else
{
b[tmp_pos] = a[mid];
tmp_pos = tmp_pos + 1;
mid = mid + 1;
}
}

while( left <= left_end )
{
b[tmp_pos] = a[left];
left = left + 1;
tmp_pos = tmp_pos + 1;
}

while( mid <= right )
{
b[tmp_pos] = a[mid];
mid = mid + 1;
tmp_pos = tmp_pos + 1;
}

for( i = 0; i < num_elements; i++ )
{
a[right] = b[right];
right = right - 1;
}
}

ASP.NET-Insertion Sort

 Insertion Sort


The Insertion sort works just like its name suggests - it inserts each item into its proper place in the final list. The simplest implementation of this requires two list structures - the source list and the list into which sorted items are inserted. To save memory, most implementations use an in-place sort that works by moving the current item past the already sorted items and repeatedly swapping it with the preceding item until it is in place.


The insertion sort is a little over twice as efficient as the bubble sort.


The insertion sort is a good middle-of-the-road choice for sorting lists of a few thousand items or less. The algorithm is significantly simpler than the Shell Sort, with only a small trade-off in efficiency. At the same time, the insertion sort is over twice as fast as the Bubble Sort and almost 40% faster than the Selection Sort. The insertion sort shouldn't be used for sorting lists larger than a couple thousand items or repetitive sorting of lists larger than a couple hundred items.


Insertion Sort Routine Code


// array of integers to hold values
private int[] a = new int[100];
 
// number of elements in array
private int x;
 
// Insertion Sort Algorithm
public void sortArray()
{
int i;
int j;
int index;
 
for( i = 1; i < x; i++ )
{
index = a[i];
j = i;
 
while( (j > 0) && (a[j-1] > index) )
{
a[j] = a[j-1];
j = j - 1;
}
 
a[j] = index;
}
}

ASP.Net-Heap Sort

Heap Sort


The heap sort does not require massive recursion or multiple arrays to work. This makes it an attractive option for very large data sets of millions of items.

The heap sort works as it name suggests - it begins by building a heap out of the data set, and then removing the largest item and placing it at the end of the sorted array. After removing the largest item, it reconstructs the heap and removes the largest remaining item and places it in the next open position from the end of the sorted array. This is repeated until there are no items left in the heap and the sorted array is full. Elementary implementations require two arrays - one to hold the heap and the other to hold the sorted elements.

To do an in-place sort and save the space the second array would require, the algorithm below "cheats" by using the same array to store both the heap and the sorted array. Whenever an item is removed from the heap, it frees up a space at the end of the array that the removed item can be placed in.


Heap Sort Routine Code

// array of integers to hold values
private int[] a = new int[100];
 
// number of elements in array
private int x;
 
// Heap Sort Algorithm
public void sortArray()
{
  int i;
  int temp;
 
  for( i = (x/2)-1; i >= 0; i-- )
  {
    siftDown( i, x );
  }
 
  for( i = x-1; i >= 1; i-- )
  {
    temp = a[0];
    a[0] = a[i];
    a[i] = temp;
    siftDown( 0, i-1 );
  }
}
 
public void siftDown( int root, int bottom )
{
  bool done = false;
  int maxChild;
  int temp;
 
  while( (root*2 <= bottom) && (!done) )
  {
    if( root*2 == bottom )
      maxChild = root * 2;
    else if( a[root * 2] > a[root * 2 + 1] )
      maxChild = root * 2;
    else
      maxChild = root * 2 + 1;
 
    if( a[root] < a[maxChild] )
    {
      temp = a[root];
      a[root] = a[maxChild];
      a[maxChild] = temp;
      root = maxChild;
    }
    else
    {
      done = true;
    }
  }
}

ASP.Net Bubble Sort

The bubble sort is the slowest sort in use.


The bubble sort works by comparing each item in the list with the item next to it, and swapping them if required. The algorithm repeats this process until it makes a pass all the way through the list without swapping any items. This causes larger values to "bubble" to the end of the list while smaller values "sink" towards the beginning of the list.


The bubble sort is considered to be the most inefficient sorting algorithm.






Bubble Sort Routine Code


// array of integers to hold values
private int[] a = new int[100];

// number of elements in array
private int x;

// Bubble Sort Algorithm
public void sortArray()
{
int i;
int j;
int temp;

for( i = (x - 1); i >= 0; i-- )
{
for( j = 1; j <= i; j++ )
{
if( a[j-1] > a[j] )
{
temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
}
}
}
}

Wednesday, July 28, 2010

About Me

Software developer by profession. Acquainted with an engineering degree of B.E in Computers, one can call me the blend of moderate and tradition mix.I have developed myself as a person who can survive in all kind of situations.About Psingh Choudhary

Currently working as software-developer in a well reputated company at Jaipur since July 2007 on various web technologies like Asp.net,ado.net,sql server, photo-shop, c#, vb.net, java script,jqury,XML,web services,Ajax etc.

I have flair of creative writing and reading techno based fictions. I believe in sharing knowledge and according to me writing is the best possible medium for this so welcome to my blog and enjoy reading and sharing the same...................