Thursday, September 25, 2008

Verify the string whether it is palindrome(c#)

Some times we need to verify a string whether it is palindrome, here is a method with stack theory to cope with it.
Let's go!

using System.Collections;//

namespace Palindrome

{

class CStack

{

private int p_index;

private ArrayList list;
//initiate the arraylist

public CStack()

{

list = new ArrayList();

p_index = -1;

}
//return the arraylist length as count number property

public int count { get { return list.Count; } }


//add items into arraylist

public void push(Object item) {

list.Add(item);

p_index++;

}
//retrieve the top data and remove it from arraylist

public object pop() {

object obj = list[p_index];

list.RemoveAt(p_index);

p_index--;

return obj;

}
//remove all data from arraylist

public void clear() {

list.Clear();

p_index = -1;

}
//display all elements of arraylist

public object peek() {

return list[p_index];

}

}
class Program

{

static void Main(string[] args)

{

CStack alist = new CStack();

string ch; Console.WriteLine("input you word....");

string word = Console.ReadLine();

bool isPalindrome = true;
for (int x = 0; x < pos =" 0;"> 0)

{

ch = alist.pop().ToString();

if (ch != word.Substring(pos, 1))

{

isPalindrome = false;

break;

}

pos++;

}

//show the result

if (isPalindrome)

{

Console.WriteLine(word + " is a palindrome");

}

else

{

Console.WriteLine(word+" is not a palindrome");

}

Console.WriteLine();

}

}

}

Basic searching algorithm

Searching a data set for a value is a ubiquitous computational operation. The simplest method of searching a data set is to start at the beginning and search for the item until either the item is found ot the end of data set is reached. This searching method works best when the data set is relatively small and unordered.
If the data set is ordered, the binary searching algorithm is the better choice. Binary search works by continually subdividing the data set until the item being searched for is found. You can write the binary searching algorithm using both iterative and recursive codes. The Array class in C# includes a built-in binary search method, which should be used whenever a binary search is called for.
ok, let's save the words and goto code!


namespace BasicSearchAlgorithm
{
class CArray
{
//binary seach algorithm
public int binSearch(int[] arr,int value)
{
int upperBound, lowerBound, mid;
upperBound = arr.Length - 1;
lowerBound = 0;
while (lowerBound <= upperBound)
{
mid = (lowerBound + upperBound)/2;
if (arr[mid] == value)
{
return mid;
}
else if (value < arr[mid])
{
upperBound = mid - 1;
}
else
{
lowerBound = mid + 1;
}
}
return -1;
}
}
class Program
{
static void Main(string[] args)
{
int[] arr = new int[10] { 12, 5, 21, 89, 63, 45, 72, 50, 43, 16 };
//Insertion Sort
int inner, temp;
int upper = arr.Length - 1;
for (int i = 1; i <= upper; i++)
{
temp = arr[i];
inner = i;
while (inner > 0 && arr[inner - 1] >= temp)
{
arr[inner] = arr[inner - 1];
inner -= 1;
}
arr[inner] = temp;
}
foreach (int i in arr)
{
Console.Write(i+", ");
}
Console.WriteLine();
//end insertion sort
int seek = 43;
CArray myNum = new CArray();
int position = myNum.binSearch(arr, seek);
if (position > -1)
{
Console.WriteLine(seek+" is found item");
}
else
{
Console.WriteLine("the item "+seek+" is not found");
}
}
}
}

that's a code statement for binary search algorithm. since the sequential search is easier, so we won't write this time.

Sorting Algorithm with three popular method(c#)

some times we need to sort the array, it's general in our coding life. so there're three popular method i got. if you want, you can create a random array with Random class, i have mentioned below.
ok, let's go!

namespace SortingAlgorithm
{
public class CArray
{
private int[] arr;
private int upper;
private int numElements;
public CArray(int size)
{
arr = new int[size];
upper = size - 1;
numElements = 0;
}
public void Insert(int item)
{
arr[numElements] = item;
numElements++;
}
public void DisplayElements()
{
for (int i = 0; i <= upper; i++)
{
Console.Write(arr[i]+", ");
}
Console.WriteLine();
}
public void Clear()
{
for (int i = 0; i <= upper; i++)
{
arr[i] = 0;
numElements = 0;
}
}
//Bubble Sort
public void BubbleSort()
{
int temp;
//record how many times in this sort
int SortTimes = 0;
for (int i = upper; i >= 1; i--)
{
for (int j = 0; j <= i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
SortTimes++;
}
}
Console.WriteLine(SortTimes);
}
//Selection Sort
public void SelectionSort()
{
int min;
int temp;
//record how many times in this sort
int SortTimes = 0;
for (int i = 0; i <= upper; i++)
{
min = i;
for (int j = i + 1; j <= upper; j++)
{
if (arr[min] > arr[j])
{
min = j;
}
SortTimes++;
}
temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
Console.WriteLine(SortTimes);
}
//Insertion Sort
public void InsertionSort()
{
int inner, temp;
//record how many times in this sort
int SortTimes = 0;
for (int i = 1; i <= upper; i++)
{
temp = arr[i];
inner = i;
while (inner > 0 && arr[inner - 1] >= temp)
{
arr[inner] = arr[inner - 1];
inner -= 1;
SortTimes++;
}
arr[inner] = temp;
}
Console.WriteLine(SortTimes);
}
}
class Program
{
static void Main(string[] args)
{
CArray nums = new CArray(10);
Random rnd = new Random(100);
for (int i = 0; i < 10; i++)
{
//create random number
nums.Insert((int)(rnd.NextDouble()*100));
}
Console.WriteLine("before sorting...");
nums.DisplayElements();
//Console.WriteLine("during bubble sorting...");
//nums.BubbleSort();
//Console.WriteLine("during selection sorting...");
//nums.SelectionSort();
Console.WriteLine("during Insertion sorting...");
nums.InsertionSort();
Console.WriteLine("after sorting...");
nums.DisplayElements();
}
}
}