Shalvin Interests

Thursday, September 15, 2011

Insertion Sort in C#





using System;

public class InsertionSort
{
   private int[] data; 
   private static Random generator = new Random();

   public InsertionSort( int size )
   {
      data = new int[ size ]; 

      for ( int i = 0; i < size; i++ )
         data[ i ] = generator.Next( 10, 100 );
   } 

   public void Sort()
   {
      int insert;

      for ( int next = 1; next < data.Length; next++ )
      {
           insert = data[ next ]; 

           int moveItem = next; 

         // search for place to put current element
         while ( moveItem > 0 && data[ moveItem - 1 ] > insert )
         {
            // shift element right one slot
            data[ moveItem ] = data[ moveItem - 1 ];
            moveItem--;
         } 

         data[ moveItem ] = insert; // place inserted element
         PrintPass( next, moveItem ); 
      } 
   } 

   public void PrintPass( int pass, int index )
   {
      Console.Write( "after pass {0}: ", pass );

      for ( int i = 0; i < index; i++ )
         Console.Write( data[ i ] + "  " );

      Console.Write( data[ index ] + "* " ); // indicate swap

      for ( int i = index + 1; i < data.Length; i++ )
         Console.Write( data[ i ] + "  " );
     
      Console.Write( "\n              " ); 

      // indicate amount of array that is sorted
      for( int i = 0; i <= pass; i++ )
         Console.Write( "--  " );
      Console.WriteLine( "\n" ); 
   } 

   public override string ToString()
   {
      string temporary = "";

      foreach ( int element in data )
         temporary += element + "  ";
     
      temporary += "\n"; // add newline character
      return temporary;
   } 
} 


using System;

public class InsertionSortTest
{
   public static void Main( string[] args )
   {
      InsertionSort sortArray = new InsertionSort( 10 );
      
      Console.WriteLine( "Unsorted array:" );
      Console.WriteLine( sortArray ); 
      sortArray.Sort(); 

      Console.WriteLine( "Sorted array:" );
      Console.WriteLine( sortArray );
      Console.ReadLine();
   } 
} 

Selection Sort in C#

using System;

public class SelectionSort
{
   private int[] data; 
   private static Random generator = new Random();

   public SelectionSort( int size )
   {
      data = new int[ size ]; 

       for ( int i = 0; i < size; i++ )
         data[ i ] = generator.Next( 10, 100 );
   } 

    public void Sort()
   {
      int smallest; 

      for ( int i = 0; i < data.Length - 1; i++ )
      {
         smallest = i; 

         // loop to find index of smallest element
         for ( int index = i + 1; index < data.Length; index++ )
            if ( data[ index ] < data[ smallest ] )
               smallest = index;

         Swap( i, smallest ); // swap smallest element into position
         PrintPass( i + 1, smallest ); // output pass of algorithm
      } 
   } 

    public void Swap( int first, int second )
   {
      int temporary = data[ first ]; 
      data[ first ] = data[ second ]; 
      data[ second ] = temporary; 
   } 
 
   public void PrintPass( int pass, int index )
   {
      Console.Write( "after pass {0}: ", pass );

      // output elements through the selected item
      for ( int i = 0; i < index; i++ )
         Console.Write( data[ i ] + "  " );

      Console.Write( data[ index ] + "* " ); // indicate swap

      // finish outputting array
      for ( int i = index + 1; i < data.Length; i++ )
         Console.Write( data[ i ] + "  " );
     
      Console.Write( "\n              " ); // for alignment

      // indicate amount of array that is sorted
      for( int j = 0; j < pass; j++ )
         Console.Write( "--  " );
      Console.WriteLine( "\n" ); // skip a line in output
   } 

   public override string ToString()
   {
      string temporary = "";

      foreach ( int element in data )
         temporary += element + "  ";

      temporary += "\n"; 
      return temporary;
   } 
} 



using System;

public class SelectionSortTest
{
   public static void Main( string[] args )
   {
      SelectionSort sortArray = new SelectionSort( 10 );
      
      Console.WriteLine( "Unsorted array:" );
      Console.WriteLine( sortArray ); 

      sortArray.Sort(); 

      Console.WriteLine( "Sorted array:" );
      Console.WriteLine( sortArray ); 
      Console.ReadLine();
   } 
} 

Binary Search in C#


using System;

public class BinaryArray
{
   private int[] data; 
   private static Random generator = new Random();

   public BinaryArray( int size )
   {
      data = new int[ size ];

       for ( int i = 0; i < size; i++ )
         data[ i ] = generator.Next( 10, 100 );

      Array.Sort( data );
   } 

   public int BinarySearch( int searchElement )
   {
      int low = 0; 
      int high = data.Length - 1; 
      int middle = ( low + high + 1 ) / 2;
      int location = -1; 

      do 
      {
         Console.Write( RemainingElements( low, high ) );

         for ( int i = 0; i < middle; i++ )
            Console.Write( "   " );

         Console.WriteLine( " * " ); 

         if ( searchElement == data[ middle ] )
            location = middle; 

         else if ( searchElement < data[ middle ] )
            high = middle - 1; 
         else 
            low = middle + 1; 

         middle = ( low + high + 1 ) / 2; 
      } while ( ( low <= high ) && ( location == -1 ) );

      return location; 
   }

   public string RemainingElements( int low, int high )
   {
      string temporary = "";

      for ( int i = 0; i < low; i++ )
         temporary += "   ";

      for ( int i = low; i <= high; i++ )
         temporary += data[ i ] + " ";

      temporary += "\n";
      return temporary;
   } 

   public override string ToString()
   {
      return RemainingElements( 0, data.Length - 1 );
   } 
} 


using System;

public class BinarySearchTest
{
   public static void Main( string[] args )
   {
      int searchInt; 
      int position;
   
      BinaryArray searchArray = new BinaryArray( 15 );
      Console.WriteLine( searchArray );

      Console.Write( "Please enter an integer value (-1 to quit): " );
      searchInt = Convert.ToInt32( Console.ReadLine() );
      Console.WriteLine();

      while ( searchInt != -1 )
      {
         position = searchArray.BinarySearch( searchInt );

         if ( position == -1 )
            Console.WriteLine( "The integer {0} was not found.\n",
               searchInt );
         else
            Console.WriteLine( 
               "The integer {0} was found in position {1}.\n",
               searchInt, position);

         Console.Write( "Please enter an integer value (-1 to quit): " );
         searchInt = Convert.ToInt32( Console.ReadLine() );
         Console.WriteLine();
      }
   } 
} 

Linear Search with C#


using System;

public class LinearArray
{
   private int[] data; 
   private static Random generator = new Random();

   public LinearArray( int size )
   {
      data = new int[ size ]; // create space for array

      for ( int i = 0; i < size; i++ )
         data[ i ] = generator.Next( 10, 100 );
   }

   public int LinearSearch( int searchKey )
   {
         for ( int index = 0; index < data.Length; index++ )
         if ( data[ index ] == searchKey )
            return index; // return index of integer

      return -1; // integer was not found      
   }

   public override string ToString()
   {
      string temporary = "";

      foreach ( int element in data )
         temporary += element + " ";

      temporary += "\n";
      return temporary;
   } 
} 





using System;

public class LinearSearchTest
{
   public static void Main( string[] args )
   {
      int searchInt; // search key
      int position; // location of search key in array

           LinearArray searchArray = new LinearArray( 10 );
      Console.WriteLine( searchArray ); // print array

            Console.Write( "Please enter an integer value (-1 to quit): " );
      searchInt = Convert.ToInt32( Console.ReadLine() );

      while ( searchInt != -1 )
      {
         position = searchArray.LinearSearch( searchInt );

         if ( position != -1 ) // integer was not found
            Console.WriteLine(
               "The integer {0} was found in position {1}.\n",
               searchInt, position );
         else // integer was found
            Console.WriteLine( "The integer {0} was not found.\n",
               searchInt );

            Console.Write( "Please enter an integer value (-1 to quit): " );
         searchInt = Convert.ToInt32( Console.ReadLine() );
      } // end while
   } 
} 


Tuesday, July 19, 2011

INotifyPropertyChanged Interface

A change-notification handler notifies a binding target that a change has been
made. This enables a target to automatically respond to changes. Dependency properties
already have this feature built in, but CLR properties don’t. If you want your CLR
properties to broadcast their changes, you must implement the INotifyProperty-
Changed interface.

INotifyPropertyChanged interface belongs to System.ComponentModel namespace.

using System.ComponentModel;

namespace INotifyProperyChangeShalvin
{
    public class Speaker : INotifyPropertyChanged
    {
        private string mSpeakerName;
            public string SpeakerName
            {
                get { return mSpeakerName; }
                set
                {
                    mSpeakerName = value;
                    FirePropertyChanged("SpeakerName");
                }
            }
            public event PropertyChangedEventHandler PropertyChanged;


            void FirePropertyChanged(string property)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this,
                        new PropertyChangedEventArgs(property));
                }
            }
        }
    }




private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    Speaker s = new Speaker { SpeakerName = "Shalvin" };
    LayoutRoot.DataContext = s;
}


<Grid x:Name="LayoutRoot" Background="White">
        <TextBox Height="23" HorizontalAlignment="Left" Margin="120,47,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Text="{Binding SpeakerName, Mode=TwoWay}" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="120,147,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" Text="{Binding SpeakerName, Mode=TwoWay}" />
</Grid>

Monday, July 18, 2011

Silverlight Layout Container

Silverlight Layout Containers are panels that derive from the abstract System.Windows.Controls.Panel class. The Properties of Panel class as Background and Children.

The Layout Panels are StackPanel, WrapPanel, DockPanel, Grid and Canvas.

Wednesday, July 13, 2011

Silverlight Styles

Styles is a collection of property value you can apply to an element. In a way it is equivalent to the functionality of CSS in HTML. You store style object as a resource.

<UserControl.Resources>
    <Style x:Key="SimpleStyle" TargetType="Button">
        <Setter Property="Foreground" Value="Blue" />
        <Setter Property="Background" Value="Cyan"/>
    </Style>
</UserControl.Resources>

<Grid x:Name="LayoutRoot" Background="White">
    <Button Style="{StaticResource SimpleStyle}" Content="Button" Height="23" HorizontalAlignment="Left" Margin="50,76,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="50,137,0,0" Name="button2" VerticalAlignment="Top" Width="75" />
</Grid>