Showing posts with label c code. Show all posts
Showing posts with label c code. Show all posts

Thursday, November 12, 2015

c code for Sorting And Searching Algorithm

BUBBLE SORT


#include<stdio.h>
#include<string.h>
int main()
{
int a[50],temp=0,i,j,n;
printf("how many number u want to sort=");
scanf("%d",&n);
printf("enter array of number\n");

for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]>=a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
return 0;
}


HEAP SORT


#include<stdio.h>
void manage(int *, int);
void heapsort(int *, int, int);

int main()
{
int arr[20];
int i,j,size,tmp,k;
printf("Enter the number of elements to sort : ");
scanf("%d",&size);
printf("Enter the element :\n");
for(i=1; i<=size; i++)
{
scanf("%d",&arr[i]);
manage(arr,i);
}

j=size;
for(i=1; i<=j; i++)
{
tmp=arr[1];
arr[1]=arr[size];
arr[size]=tmp;
size--;
heapsort(arr,1,size);
}
printf("sorted elements:\n");
size=j;
for(i=1; i<=size; i++)
printf("%d\n",arr[i]);
return 0;
}


void manage(int *arr, int i)
{
int tmp;
tmp=arr[i];
while((i>1)&&(arr[i/2]<tmp))
{
//arr[i]=arr[i/2];
//i=i/2;
k=arr[i/2];
arr[i/2]=arr[i];
arr[i]=k;
}
//arr[i]=tmp;
}


void heapsort(int *arr, int i, int size)
{
int tmp,j;
tmp=arr[i];
j=i*2;
while(j<=size)
{
if((j<size)&&(arr[j]<arr[j+1]))
j++;
if(arr[j]<arr[j/2])
break;
arr[j/2]=arr[j];
j=j*2;
}
arr[j/2]=tmp;
}

LINEAR SEARCH

#include<stdio.h>
int main()
{
int a[10],i,m,c=0,pos=0;
printf("enter array of number\n");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
printf("enter number to be search=");
scanf("%d",&m);
for(i=0;i<5;i++)
{
if(m==a[i])
{
pos=i;
c=1;
break;
}
}

if(c==0)
printf("number is not found\n");
else
printf("number is found at pos=%d\n",pos+1);

return 0;
}


QUICK SORT

#include<stdio.h>
void quicksort(int, int);
int x[20];

int main()
{
int size, i;
printf("Enter size of the array: ");
scanf("%d", &size);
printf("enter the array:\n");

for(i=0; i<size; i++)
{
scanf("%d", &x[i]);
}

quicksort(0, size-1);

printf("Sorted elements:\n ");
for(i=0; i<size; i++)
printf("%d\n", x[i]);
return 0;
}

void quicksort(int first, int last)
{
int p, j, temp, i;
if(first < last)
{
p = first;
i = first+1;
j = last;

while(i<j)
{
while(x[i] <= x[p] && i<last)
i++;

while(x[j] > x[p])
j--;
if(i<j)
{
temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}

temp = x[p];
x[p] = x[j];
x[j] = temp;
quicksort(first, j-1);
quicksort(j+1, last);

}
}

MERGE SORT

#include<stdio.h>
void merge_sort(int,int);
void merge(int,int,int);
int a[10];

int main()
{
int i,n,key,ans;
printf("Enter number(Max 10) of elements in the list : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter element %d : ",i+1);
scanf("%d",&a[i]);
}
merge_sort(0,n-1);
printf("\nMerge sorted list");
for(i=0;i<n;i++)
{
printf("\nElement %d : %d",i+1,a[i]);
}
return 0;
}

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,high,mid);
}
return;
}

void merge(int low, int high, int mid)
{
int i, j, k, c[10];
i = low;
k = low;
j = mid + 1;
while (i <= mid && j <= high)
{
if (a[i] < a[j])
{
c[k] = a[i];
k++;
i++;
}
else
{
c[k] = a[j];
k++;
j++;
}
}
while (i <= mid)
{
c[k] = a[i];
k++;
i++;
}
while (j <= high)
{
c[k] = a[j];
k++;
j++;
}
for (i = low; i < k; i++)
{
a[i] = c[i];
}
}


BINARY SEARCH

#include<stdio.h>
//int c=0;
int binary(int a[10],int key)
{
int mid,high,low;
for(low=0,high=9;low<=high;)
{
mid=(low+high)/2;
if(key==a[mid])
{
return mid;
}
else if(key<a[mid])
{
high=mid-1;
}
else
{
low=mid+1;
}
}
return 0;
}

int main()
{
int a[10],i,key,ans;
printf("enter value\n");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
printf("enter key value=");
scanf("%d",&key);
ans=binary(a,key);
printf("your found key at pos=%d\n",ans);
return 0;
}

SHELL SORT

#include<stdio.h>
int main()
{
int arr[30];
int i,j,k,tmp,num;
printf("Enter total no. of elements : \n");
scanf("%d", &num);
printf("enter the number:\n");
for(k=0; k<num; k++)
{
scanf("%d",&arr[k]);
}
for(i=num/2; i>0; i=i/2)
{
for(j=i; j<num; j++)
{
for(k=j-i; k>=0; k=k-i)
{
if(arr[k+i]>=arr[k])
break;
else
{
tmp=arr[k];
arr[k]=arr[k+i];
arr[k+i]=tmp;
}
}
}
}
printf("sorted number:\n");
for(k=0; k<num; k++)
printf("%d\n",arr[k]);

return 0;
}


REDIX SORT

#include<stdio.h>
int radix_sort(int array[], int n);
int main()
{

int array[100],n,i;
printf("Enter the number of elements to be sorted:\n ");
scanf("%d",&n);
printf("Enter the elements to be sorted: \n");

for(i = 0 ; i < n ; i++ )
{
scanf("%d",&array[i]);
}
radix_sort(array,n);

printf("sorted element\n");

for(i = 0; i < n; i++)
{
printf("%d\n", array[i]);
}
return 0;

}

radix_sort(int arr[], int n)
{
int bucket[10][5],buck[10],b[10];
int i,j,k,l,num,div,large,passes;
div=1;
num=0;
large=arr[0];
for(i=0 ; i<n ; i++)
{
if(arr[i] > large)
{
large = arr[i];
}

while(large > 0)
{
num++;
large = large/10;
}

for(passes=0 ; passes<num ; passes++)
{
for(k=0 ; k<10 ; k++)
{
buck[k] = 0;
}

for(i=0 ; i<n ;i++)
{
l = ((arr[i]/div)%10);
bucket[l][buck[l]++] = arr[i];
}

i=0;
for(k=0 ; k<10 ; k++)
{
for(j=0 ; j<buck[k] ; j++)
{
arr[i++] = bucket[k][j];
}
}
div*=10;
}
}
}


BUCKET SORT


#include<stdio.h>
void Bucket_Sort(int array[], int n)
{
int i, j;
int count[n];
for(i=0; i < n; i++)
{
count[i] = 0;
}

for(i=0; i < n; i++)
{
(count[array[i]])++;
}

for(i=0,j=0; i < n; i++)
{
for(; count[i]>0;(count[i])--)
{
array[j++] = i;
}
}
}
int main()
{
int array[100];
int num;
int i;
printf("Enter How many Numbers : ");
scanf("%d",&num);
printf("Enter the %d elements to be sorted:\n",num);

for(i = 0; i < num; i++ )
{
scanf("%d",&array[i]);
}
printf("\nThe array of elements before sorting : \n");

for (i = 0;i < num;i++)
{
printf("%d ", array[i]);
}
printf("\nThe array of elements after sorting : \n");
Bucket_Sort(array, num);

for (i=0;i<num;i++)
{
printf("%d ", array[i]);
}
printf("\n");
return 0;
}


SELECTION SORT

#include<stdio.h>
int main()
{
int i, j, n,arr[50],temp=0;
printf("How many number want to sort? ");
scanf("%d", &n);

printf("Enter the number: \n");
for(i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}

for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
printf("\nThe sorted number\n");

for(i=0; i<n; i++)
{
printf("%d\n", arr[i]);
}
return 0;
}


INSERTION SORT

#include<stdio.h>
int main()
{
int a[10],temp=0,i,j,n;
printf("how many nu. u want to sort=\n");
scanf("%d",&n);
printf("enter array of number\n");

for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}

for(i=0;i<n;i++)
{
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}

printf("sorted array is below\n");

for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
return 0;
}
Read more »

storage class in c


Storage class in C

Topics
  • Automatic variables
  • External variables
  • Static variables
  • Register variables
  • Scopes and longevity of above types of variables.

Few terms

  • Scope: the scope of a variable determines over what part(s) of the program a variable is actually available for use(active).
  • Longevity: it refers to the period during which a variables retains a given value during execution of a program(alive)
  • Local(internal) variables: are those which are declared within a particular function.
  • Global(external) variables: are those which are declared outside any function.

Automatic variables

  • Are declare inside a function in which they are to be utilized.
  • Are declared using a keyword auto           eg. auto int number;
  • Are created when the function is called and destroyed automatically when the function is exited.
  • This variable are therefore private(local) to the function in which they are declared.
  • Variables declared inside a function without storage class specification is, by default, an automatic variable.


Example program

int main()
{ int m=1000;
function2();
printf(%d\n”,m);
}
function1()
{
int m = 10;
printf(%d\n”,m);
}
function2()
{ int m = 100;
function1();
printf(%d\n”,m);
}

Few observation about auto variables

  • Any variable local to main will normally live throughout the whole program, although it is active only in main.
  • During recursion, the nested variables are unique auto variables.
  • Automatic variables can also be defined within blocks. In that case they are meaningful only inside the blocks where they are declared.
  • If automatic variables are not initialized they will contain garbage.
External Variables
  • These variables are declared outside any function. 
  • These variables are active and alive throughout the entire program.
  • Also known as global variables and default value is zero.
  • Unlike local variables they are accessed by any function in the program.
  • In case local variable and global variable have the same name, the local variable will have precedence over the global one.
  • Sometimes the keyword extern used to declare these variable.
  • It is visible only from the point of declaration to the end of the program.


External variable (examples)

int number;
float length=7.5;
main()
{ . . .
. . .
}
funtion1()
{. . .
. . .
}
funtion1()
{. . .
. . .
}

Global variable example

int x;
int main()
{
x=10;
printf(“x=%d\n”,x);
printf(“x=%d\n”,fun1());
printf(“x=%d\n”,fun2());
printf(“x=%d\n”,fun3());
}
int fun1()
{ x=x+10;
return(x);
}
int fun2()
{ int x
x=1;
return(x);
}

External declaration

int main()
{

y=5;
. . .
. . .
}
int y;

func1()
{
y=y+1
}

External declaration(examples)

int main()
{
extern int y;
. . .
. . .
}
func1()
{
extern int y;
. . .
. . .
}
int y;

Multifile Programs and extern variables

int main()
{
extern int m;
int i
. . .
. . .
}
function1()
{
int j;
. . .
. . .
}


Multifile Programs and extern variables

int m;
int main()
{
int i;
. . .
. . .
}
function1()
{
int j;
. . .
. . .
}


Static Variables


  • The value of static variables persists until the end of the program.
  • It is declared using the keyword static like
  •                    static int x;
  •                    static float y;
  • It may be of external or internal type depending on the place of there declaration.
  • Static variables are initialized only once, when the program is compiled.

Internal static variable


  • Are those which are declared inside a function.
  • Scope of Internal static variables extend upto the end of the program in which they are defined.
  • Internal static variables are almost same as auto variable except they remain in existence (alive) throughout the remainder of the program.
  • Internal static variables can be used to retain values between function calls.
  • Examples (internal static)
  • Internal static variable can be used to count the number of calls made to function. eg.

int main()
{
int I;
for(i =1; i<=3; i++)
stat();
}
void stat()
{
static int x=0;
x = x+1;
printf(“x = %d\n”,x);
}

External static variables


  • An external static variable is declared outside of all functions and is available to all the functions in the program.
  • An external static variable seems similar simple external variable but their difference is that static external variable is available only within the file where it is defined while simple external variable can be accessed by other files.
Static function
  • Static declaration can also be used to control the scope of a function.
  • If you want a particular function to be accessible only to the functions in the file in which it is defined and not to any function in other files, declare the function to be static. eg.

          static int power(int x inty)
                {
                  .   .   .
                  .   .   .
                }                     


Register Variable


  • These variables are stored in one of the machine’s register and are declared using register keyword. 
  •             eg. register int count;
  • Since register access are much faster than a memory access keeping frequently accessed variables in the register lead to faster execution of program.
  • Since only few variable can be placed in the register, it is important to carefully select the variables for this purpose. However, C will automatically convert register variables into non register variables  once the limit is reached.
  • Don’t try to declare a global variable as register. Because the register will be occupied during the lifetime of the program.
Read more »