Thursday, June 16, 2011

Algorithm and Program for Merge Short

Algorithm

function merge(left,right)
    var list result
    while length(left) > 0 or length(right) > 0
        if length(left) > 0 and length(right) > 0
            if first(left) ≤ first(right)
                append first(left) to result
                left = rest(left)
            else
                append first(right) to result
                right = rest(right)
        else if length(left) > 0
            append first(left) to result
            left = rest(left)
        else if length(right) > 0
            append first(right) to result
            right = rest(right)
    end while
    return result

Program

#include <stdio.h>
#include<conio.h>
int a[50];
void merge(int,int,int);
void merge_sort(int low,int high)
{ int mid;
  if(low<high)
 {mid=(low+high)/2;
  merge_sort(low,mid);
  merge_sort(mid+1,high);
  merge(low,mid,high);
 }
}
void merge(int low,int mid,int high)
{
 int h,i,j,b[50],k;
 h=low;
 i=low;
 j=mid+1;
 while((h<=mid)&&(j<=high))
 {if(a[h]<=a[j])
  {b[i]=a[h];
   h++;
  }
  else
  {b[i]=a[j];
   j++;
  }
  i++;
 }
 if(h>mid)
 {
  for(k=j;k<=high;k++)
  {b[i]=a[k];
   i++;
  }
 }
 else
 {  for(k=h;k<=mid;k++)
       {b[i]=a[k];
        i++;
       }
 }
 for(k=low;k<=high;k++) a[k]=b[k];
}
int main()
{int num,i;
 printf("Please Enter THE NUMBER OF ELEMENTS you want to sort :\n");
 scanf("%d",&num);
 printf("\n");
 printf("Now, Please Enter the ( %d ) numbers :\n",num);
 for(i=1;i<=num;i++)
 {  scanf("%d",&a[i]); }
 merge_sort(1,num);
 printf("\n");
 printf("So, the sorted list (using MERGE SORT) will be :\n");
 printf("\n\n");
 for(i=1;i<=num;i++)
 printf("%d  ",a[i]);
 printf("\n\n\n\n");
getch();
return 0;
}

Algorithm and Program for Quick Short

Algorithm

// left is the index of the leftmost element of the array
  // right is the index of the rightmost element of the array (inclusive)
  // number of elements in subarray: right-left+1
  function partition(array, left, right, pivotIndex)
     pivotValue := array[pivotIndex]
     swap array[pivotIndex] and array[right]  // Move pivot to end
     storeIndex := left
     for i from left to right - 1 // left ≤ i < right
         if array[i] < pivotValue
             swap array[i] and array[storeIndex]
             storeIndex := storeIndex + 1
     swap array[storeIndex] and array[right]  // Move pivot to its final place
     return storeIndex

Program
#include<conio.h>
#include<stdio.h>
void quickshort(int ,int,int);
int partition(int arr[], int left, int right)
{  int i = left, j = right;
   int tmp;
   int pivot = arr[i];
   while (i <= j)
         { while (arr[i] < pivot)
                  i++;
            while (arr[j] > pivot)
                   j--;
            if (i <= j)
               { tmp = arr[i];
                  arr[i] = arr[j];
                  arr[j] = tmp;
                  i++;
                  j--;
               }
         };
   return i;
}
void quickSort(int arr[], int left, int right)
{  int index = partition(arr, left, right);
      if (left < index - 1)
      quickSort(arr, left, index - 1);
      if (index < right)
            quickSort(arr, index, right);
}
int main()
{printf("enter the no of elements in the list\n");
int n,i;
scanf("%d",&n);
int arr[n];
printf("now enter the %d elements of the list ",n);
for(i=0;i<n;i++)
    scanf("%d",&arr[i]);
quickSort(arr,0,7);
printf("\n The shorted list is ");
for(i=0;i<n;i++)
printf("  %d  ",arr[i]);
getch();
    return 0;}

Algorithm and Program for Binary Search


implementation of the algorithm

limitations :
entered list must be pre shorted;
there must be only one element in the list which is to be searched;

#include<stdio.h>
#include<conio.h>
int mid;
int BinarySearch(int A[], int key, int low,int high)
{
       if (high < low)
        return -1;
      mid =low + (high - low)/2;
       if (A[mid] > key)
          return BinarySearch(A,key, low, mid-1);
       else if (A[mid] < key)
          return BinarySearch(A, key, mid+1, high);
       else
           return mid ;
   };
  
   int main()
   {
     
      int j,low=0,high=9,key,k=0;
      int A[9];
       printf("enter the 12 elements of array");
      for(j=0;j<9;j++)
      scanf("%d",&A[j]);
       printf("\n enter the element to be searched for");
      scanf("%d",&key);
      j=BinarySearch(A,key,low,high);
      if (j>=0)
      printf("\element %d is found at position %d",key,j+1); 
      else
      printf("\n element not found");
      getch();
      return 0;
   }
BinarySearch(A[0..N-1], value, low, high) {
       if (high < low)
           return -1 // not found
       mid = low + (high - low) / 2
       if (A[mid] > value)
           return BinarySearch(A, value, low, mid-1)
       else if (A[mid] < value)
           return BinarySearch(A, value, mid+1, high)
       else
           return mid // found
   }

Program to saving data to a database

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{

  public partial class Form1 : Form
{
  public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection cn =new SqlConnection("PASTE CONNECTION STRING HERE");

cn.Open();
SqlCommand cmd = new SqlCommand("insert into emp values ('"+textBox1.Text+"', '"+textBox2.Text+"', "+textBox3.Text+")",cn); 

cmd.ExecuteNonQuery();
MessageBox.Show(" your entries are saved into the database");
}
}

connection string is found in database properties

example of a connection string


("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\manish\\Documents\\niist.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")

Program to add two numbers in c#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;using System.Drawing;
using System.Linq;using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}


private void button1_Click(object sender, EventArgs e)
{int a,b,c;
a=Convert.ToInt32(textBox1.Text);
b=Convert.ToInt32(textBox2.Text);
c=a+b;
textBox3.Text = (c.ToString());

}
}
}

Program to retrieve data from a table

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1

   public partial class Form1 : Form
{
   public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

 SqlCommand cmd = new SqlCommand(" select * from EMP where ENAME='" + textBox1.Text + "'",cn);
 SqlDataReader dr = cmd.ExecuteReader();
cn.Open();
dr.Read();
if (dr.HasRows)
{
label2.Text = dr["DESIG"].ToString();
label3.Text = dr["SAL"].ToString();
}
if (!dr.HasRows)
{
MessageBox.Show("ERROR IN RETRIEVING","Error Message"MessageBoxButtons.OKCancel ,  MessageBoxIcon.Warning,  MessageBoxDefaultButton.Button1);
}
}
}
}

connection string can be found in database properties

example of a connection string

("Data Source=.\\SQLEXPRESS;AttachDbFilename= C:\\Users\\manish\\Documents\\niist.mdf; Integrated Security=True;Connect Timeout=30;User Instance=True")SqlConnection cn = new SqlConnection("PASTE CONNECTION STRING HERE");