You cannot copy content of this page.

Aug 20,2012
Leave a comment
By admin

**Deletion from a position in an array:**

Deletion of a value is straightforward, since the elements from the given position are overwritten with the subsequent elements upto the last element, last element is initialized with zero and variable n indicating total number of elements is decremented by one.

/* Deletion from a position */

#include <stdio.h>

#define MAX 10

int main(){

int arr[MAX],i,n,pos;

printf(“nEnter total numbers?”);

scanf(“%d”,&n);

for(i=0;i<n;i++){

printf(“nEnter number?”);

scanf(“%d”,&arr[i]);

}

printf(“nEnter the position from where to delete element?”);

scanf(“%d”,&pos);

for(i=pos;i<n;i++)

arr[i] = arr[i+1];

arr[i] = 0;

n–;

printf(“nArray elements after deletion:”);

for(i=0;i<n;i++)

printf(“%dt”,arr[i]);

}

**Sorted insertion in an Array:**

Whenever a new value is inserted, appropriate place is located to insert this value. After locating that place, shifting of elements is required from that position to the last element by one position to the right. Then the located position may be assigned the new element to be inserted. The ‘C’ program for the same is given below:

/* Program for sorted insertion */

#include <stdio.h>

#define MAX 10

int main(){

int arr[MAX]={0},n,i,j,k,num,ct=0;

printf(“nEnter total numbers?”);

scanf(“%d”,&n);

for(i=0;i<n;i++){

printf(“nEnter number to insert?”);

scanf(“%d”,&num);

for(j=0;j<ct && arr[j]<=num;j++);

for(k=n-1;k>=j;k–)

arr[k+1] = arr[k];

arr[j] = num;

ct++;

}

printf(“nArray elements are: “);

for(i=0;i<n;i++)

printf(“%dt”,arr[i]);

}

**Insert a value at a given position in an array:**

For inserting a value at a given position, the elements are shifted from the last element by one position to the right, and the element at the specified position is overwritten with the new value to be inserted.

/* Insertion at a position in an array */

#include <stdio.h>

#define MAX 10

int main(){

int arr[MAX],n,i,pos,val;

printf(“nEnter total numbers?”);

scanf(“%d”,&n);

for(i=0;i<n;i++){

printf(“nEnter number?”);

scanf(“%d”,&arr[i]);

}

printf(“nEnter the value to insert?”);

scanf(“%d”,&val);

printf(“nEnter the position where to insert?”);

scanf(“%d”,&pos);

for(i=n-1;i>=pos;i–)

arr[i+1] = arr[i];

arr[pos] = val;

printf(“nArray elements after insertion:”);

for(i=0;i<n+1;i++)

printf(“%dt”,arr[i]);

}

**Binary Search:**

Binary Search involves reducing the search range to half by dividing the range into half of its original size. Binary Search operates upon sorted array. It compares the element at the mid of this range with the value to be searched, if the value is smaller than the mid value, then the value is looked up in the range from first element to mid, otherwise the new search range becomes mid to last element. This process continues until the required element is located or lower bound becomes greater than upper bound. Efficiency of Binary Search is O(log2n) in average and worst case and is O(1) in best case. The ‘C’ program to perform Binary Search is given below:

/* Binary Search */

#include <stdio.h>

#define MAX 10

int main(){

int arr[MAX],i,n,val;

int lb,ub,mid;

printf(“nEnter total numbers?”);

scanf(“%d”,&n);

for(i=0;i<n;i++){

printf(“nEnter number?”);

scanf(“%d”,&arr[i]);

}

printf(“nEnter the value to search?”);

scanf(“%d”,&val);

lb=0;ub=n-1;

while(lb<=ub){

mid=(lb+ub)/2;

if(val<arr[mid])

ub = mid-1;

else if(val>arr[mid])

lb = mid+1;

else {

printf(“nNumber found…!”);

return;

}

}

printf(“nNumber not found…!”);

}

**Linear Search:**

Linear search involves searching the element from a given set of elements beginning from the first element upto the last element or upto its first occurrence if it is available in the given set. The efficiency of Linear Search is O(n). The program to perform linear search is given below:

/* Linear Search */

#include <stdio.h>

#define MAX 10

int main(){

int arr[MAX],n,i,num,val;

printf(“nEnter total numbers?”);

scanf(“%d”,&n);

for(i=0;i<n;i++){

printf(“nEnter number?”);

scanf(“%d”,&arr[i]);

}

printf(“nEnter the value to search?”);

scanf(“%d”,&val);

for(i=0;i<n;i++){

if(arr[i]==val){

printf(“nNumber found.”);

return;

}

}

printf(“nNumber not found.”);

}