SIGN UP MEMBER LOGIN:    
ARTICLE

Sorting, Searching and some other useful programs

Posted by Narayana Surapaneni Articles | C# Language September 07, 2001
Here are the some useful programs including many search algorithms, sort, palindrome, fibonacci...
Reader Level:

Introduction

Here are the some useful programs implemented using C#. Of course, most of you might have programmed them using different other programming languages. Hence I am not going to explain you what each program does. I am going show how to implement them using C# The following is the list of programs for your reference.

Linear Search Program

using System;
class linSearch
{
public static void Main()
{
int[] a= new int[100];
Console.WriteLine("Enter number of elements you want to hold in the array ?");
string s=Console.ReadLine();
int x=Int32.Parse(s);
Console.WriteLine("-------------------------");
Console.WriteLine("\n Enter array elements \n");
for(int i=0;i<x;i++)
{
string s1=Console.ReadLine();
a[i]=Int32.Parse(s1);
}
Console.WriteLine("-------------------------");
Console.WriteLine("Enter Search element\n");
string s3=Console.ReadLine();
int x2=Int32.Parse(s3);
for(int i=0;i<x;i++)
{
if(a[i]==x2)
{
Console.WriteLine("-------------------------");
Console.WriteLine("Search successful");
Console.WriteLine("Element {0} found at location {1}\n",x2,i+1);
return;
}
}
Console.WriteLine("Search unsuccessful");
}
}

Binary Search Program

using System;
class binSearch
{
public static void Main()
{
int[] a= new int[100];
Console.WriteLine("Number of elements in the array ?");
string s=Console.ReadLine();
int x=Int32.Parse(s);
Console.WriteLine("-----------------------");
Console.WriteLine(" Enter array elements ");
Console.WriteLine("-----------------------");
for(int i=0;i<x;i++)
{
string s1=Console.ReadLine();
a[i]=Int32.Parse(s1);
}
Console.WriteLine("--------------------");
Console.WriteLine("Enter Search element");
Console.WriteLine("--------------------");
string s3=Console.ReadLine();
int x2=Int32.Parse(s3);
int low=0;
int high=x-1;
while(low<=high)
{
int mid=(low+high)/2;
if(x2<a[mid])
high=mid-1;
else if(x2>a[mid])
low=mid+1;
else if(x2==a[mid])
{
Console.WriteLine("-----------------");
Console.WriteLine("Search successful");
Console.WriteLine("-----------------");
Console.WriteLine("Element {0} found at location {1}\n",x2,mid+1);
return;
}
}
Console.WriteLine("Search unsuccessful");
}
}

Selection Sort Program

using System;
class selectionSort
{
public static void Main()
{
int[] a= new int[100];
int min,pass,i;
Console.WriteLine("Number of elements in the array ?");
string s=Console.ReadLine();
int x=Int32.Parse(s);
Console.WriteLine("-----------------------");
Console.WriteLine(" array elements ");
Console.WriteLine("-----------------------");
for(int j=0;j<x;j++)
{
string s1=Console.ReadLine();
a[j]=Int32.Parse(s1);
}
for(pass=0;pass<x-1;pass++)
{
min=pass;
for(i=pass+1;i<x;i++)
{
if(a[min]>a[i])
min=i;
}
if( min!=pass)
{
int k=a[pass];
a[pass]=a[min];
a[min]=k;
}
}
Console.WriteLine("--------------------------------------------");
Console.WriteLine("Sorted elements of an array are(selection sort)");
for (int j=0;j<x;j++)
Console.WriteLine(a[j]);
}
}

Bubble Sort Program

using System;
class bubbleSort
{
public static void Main()
{
int[] a= new int[100];
Console.WriteLine("Number of elements in the array ?");
string s=Console.ReadLine();
int x=Int32.Parse(s);
Console.WriteLine("-----------------------");
Console.WriteLine(" array elements ");
Console.WriteLine("-----------------------");
for(int j=0;j<x;j++)
{
string s1=Console.ReadLine();
a[j]=Int32.Parse(s1);
}
int limit= x-1;
for(int pass=0;pass<x-1;pass++)
{
for(int j=0;j<limit-pass;j++)
{
if(a[j]>a[j+1])
{
int k=a[j];
a[j]=a[j+1];
a[j+1]=k;
}
Console.WriteLine("------------------------------------------------");
Console.WriteLine("Sorted elements of an array are(buble sort)");
for (int j=0;j<x;j++)
Console.WriteLine(a[j]);
}
}

Finding Largest and Smallest Number

using System;
class Test
{
public static void Main()
{
int n;
float large,small;
int[] a = new int[50];
Console.WriteLine("Enter the size of Array");
string s= Console.ReadLine();
n=Int32.Parse(s);
Console.WriteLine("Enter the array elements");
for(int i=0;i<n;i++)
{
string s1=Console.ReadLine();
a[i]=Int32.Parse(s1);
}
Console.Write("");
large =a[0];
small= a[0];
for(int i=1;i<n;i++)
{
if(a[i]>large)
large=a[i];
else if(a[i]<small)
small=a[i];
}
Console.WriteLine("Largest element in the array is {0}",large);
Console.WriteLine("Smallest element in the array is {0}",small);
}
}

Fibonacci Series

using System;
class FS
{
public static void Main()
{
int n;
Console.WriteLine("Number of terms to be generated ?");
string s=Console.ReadLine();
n=Int32.Parse(s);
Console.WriteLine("*******************************");
Console.WriteLine("Fibonacci seqence upto {0}",n);
Console.WriteLine("*******************************");
test1.generateFib(n);
}
}
class generateFib
{
static int f1=0;
static int f2=1;
public static void fib(int n)
{
int temp;
if(n<2)
{
f1=0;
f2=1;
}
else
{
fib(n-1);
temp=f2;
f2=f1+f2;
f1=temp;
}
Console.WriteLine(f1);
}
}

Palindrome Program

using System;
class palindrome
{
public static void Main()
{
int n,num,digit,sum=0,rev=0;
string s;
Console.WriteLine("*******************");
Console.WriteLine("Please enter a number");
Console.WriteLine("*******************");
s=Console.ReadLine();
num=Int32.Parse(s);
n=num;
do
{
digit=num%10;
sum+=digit;
rev=rev*10+digit;
num/=10;
}
while(num!=0);
Console.WriteLine("*******************************");
Console.WriteLine("Sum of the digit of the number = {0}",sum);
Console.WriteLine("************************");
Console.WriteLine("Reverse of the number = {0}",rev);
Console.WriteLine("************************");
if(n==rev)
Console.WriteLine("The number is a palindrome");
else
Console.WriteLine("The number is not a palindrome");
}
}

Generating Pascal Triangle

using System;
class pascalTriangle
{
public static void Main()
{
int binom=1,q=0,r,x;
Console.WriteLine("Enter the number of rows");
string s=Console.ReadLine();
int p =Int32.Parse(s);
Console.WriteLine(p);
Console.WriteLine("The Pascal Triangle");
while(q<p)
{
for(r=40-(3*q);r>0;--r)
Console.Write(" ");
for(x=0;x<=q;++x)
{
if((x==0)||(q==0))
binom=1;
else
binom=(binom*(q-x+1))/x;
Console.Write(binom);
}
Console.Write("\n ");
++q;
}
}
}

Login to add your contents and source code to this article
share this article :
post comment
 

:(

Posted by emani ama Aug 07, 2007

i am using microsoft visual j++ 6.0

Posted by emani ama Aug 07, 2007

withe these variable 16 8 12 21 24 11 25 thnk you :)

Posted by emani ama Aug 07, 2007
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor