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
   } 
}