Showing posts with label C Programing. Show all posts
Showing posts with label C Programing. Show all posts

Friday, 8 March 2019

Horspool program in c

What is Horspool algorithm..??

                                                                               The Horspool algorithms we have seen so far access every character of the text. If we start the comparison between the pattern and the current text position from the end, we can often skip some text characters completely.


Horspool program


#include<stdio.h>
#include<string.h>
#include<time.h>
#include<sys/resource.h>
#define MAX 500
int t[MAX];
void shifttable(char p[])
{
    int i,j,m;
    m=strlen(p);
    for(i=0;i<MAX;i++)
        t[i]=m;
     for(j=0;j<m-1;j++)
          t[p[j]]=m-1-j;
}
int horspool(char src[],char p[])
{
   
    int i,j,k,m,n;
    n=strlen(src);
    m=strlen(p);
    printf("nLength of text=%d",n);
    printf("n Length of pattern=%d",m);
     i=m-1;
     while(i<n)
     {
          k=0;
          while((k<m)&&(p[m-1-k]==src[i-k]))
               k++;
          if(k==m)
               return(i-m+1);
          else
               i+=t[src[i]];
     }
     return -1;
}
void main()
{
    char src[100],p[100];
    clock_t start,end=0;   
    double CPU_time_used;
    struct rusage r_usage;
    getrusage(RUSAGE_SELF,&r_usage);
    int pos;
    
    printf("Enter the text in which pattern is to be searched\n");
    gets(src);
    printf("Enter the pattern to be searched\n");
    gets(p);
    shifttable(p);
    start=clock();   
     pos=horspool(src,p);
    end=clock();
     if(pos>=0)
    printf("\n The desired pattern was found starting from position %d",pos+1);
    else
      printf("\n The pattern was not found in the given textn");
    CPU_time_used=((double)(end-start))/CLOCKS_PER_SEC;
    printf("\n TIME %lf \n",CPU_time_used);
    printf("\n Memory used %ld Kb \n",r_usage.ru_maxrss);
   
}

Horspool Output:




Click for download Source code:

Thursday, 14 February 2019

quicksort program in c



quicksort in c:

Quicksort is a sorting method it arranges the element either ascending order or descending order. Quicksort is similar to mergesort it is also divided and conquers algorithm. In this blog, I will explain the quicksort problem, quicksort program with the output.




quicksort code in c:


#include<stdio.h>
void quicksort(int number[25],int first,int last){
int i, j, pivot, temp;

if(first<last){
pivot=first;
i=first;
j=last;

while(i<j){
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}

temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);

}
}

int main(){
int i, n, number[25];

printf("Enetr the number of element ");
scanf("%d",&n);

printf("Enter %d elements: ", n);
for(i=0;i<n;i++)
scanf("%d",&number[i]);

quicksort(number,0,n-1);

printf("Order of Sorted elements: ");
for(i=0;i<n;i++)
printf(" %d",number[i]);

return 0;
}


Output:


Download Executable file: Download

Saturday, 26 January 2019

PROGRAM TO IMPLEMENT BUBBLE SORT C - using function



BUBBLE SORT C USING FUNCTION:

Executable bubble sort program download link available at the end of this page. This bubble sort c program uses function, first of all we discuss what is SORT, sort is a arranging the element either ascending order or descending order. Bubble sort is the simplest sorting algorithm it sort adjacent element until it become either ascending or descending.



Example:

(4,1,2,6)   // is a given number

(4,1,2,6)  // check  (4>1)..?  YES swap 4 and 1

(1,4,2,6)  // check (4>2)..? YES  swap 4 and 2
(1,2,4,6)  // check (4>6)..? NO don’t swap.

Element of 4,1,2,6 after sorting 1,2,4,6

Let’s See another Example for your clarity.

(3,1 7,8,10,5,2) // Consider as Given number

Pass 1:
(3,1,7,8,10,5)  // if(3>1)…?? Yes .,  Swap it.
(1,3,7,8,10,5)  // if (3>7)..? False ..
(1,3,7,8,10,5) // if(7>8)..? false..
(1,3,7,8,10,5) // (8>10)..?  False…
(1,3,7,8,10,5)//(10>5)..? Yes  Swap it
(1,3,7,8,5,10)  // 1st  completed

Pass 2:
 (1,3,7,8,5,10)   // Pass 1 result
(1,3,7,8,5,10) //(1>3)..? False
(1,3,7,8,5,10) //(3>7)..? False
(1,3,7,8,5,10) // (7>8)..? False
(1,3,7,8,5,10) // (8>5)..? Yes… Swap it
(1,3,7,5,8,10) //(8<10)..? False
(1,3,7,5,8,10) // 2nd pass result

Pass 3:

(1,3,7,5,8,10) // (1>3)..?? False
(1,3,7,5,8,10) // (3>7)….? False
(1,3,7,5,8,10) // (7>5)..?  Yes  Swap it
(1,3,5,7,8,10)  //(7>8)..? False
(1,3,5,7,8,10) // (8>10)..? False

(1,3,5,7,8,10)  // 3rd Pass Result

(3,1,7,8,10,5)  Result After Sorting is  (1,3,5,7,8,10)  



#include<stdio.h>
void bubble_sort(int [], int);

int main()

{
int array[100],n,i,j;

printf("enter the number of element to be enetered\n");
scanf("%d",&n);

printf("Enter the element\n");
for(i=0;i<n;i++)
scanf("%d",&array[i]);

bubble_sort(array,n);

printf("sorted list in ascending order");
for(i=0;i<n;i++)
printf("%d\t",array[i]);
return 0;
}

void bubble_sort(int array[], int n)
{
int i,j,k;
for(i=0;i<n-1;i++)
 {
  for(j=0;j<n-i-1;j++)
    {
     if(array[j] > array[j+1])
        {
 //swapping

k = array[j];
array[j] = array[j+1];
array[j+1]=k;
}
}
}
}


Output:

 

Executable C file Link:

Download


Tuesday, 22 January 2019

implement merge sort c | executable C file direct download link


implement merge sort

merge sort program in c  is a type of sorting program,  merge sort is a one of the sorting Technic. in this method of sorting  each and every elements are divide and merge, in that time all the elements are sorted either ascending order or descending order. merge sort program is given below, you can also download executable ".C" file in the below link.


mergesort program in c




Program:

#include<stdio.h>
void mergesort(int a[],int low,int high);
void merging(int a[],int i1,int j1,int i2,int j2);

int main()
{
int a[50],n,i;
printf("Enter the no of element\n");                                       

scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("\n sorted array is:");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
return 0;
}

void mergesort(int a[],int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merging(a,low,mid,mid+1,high);
}
}

void merging(int a[],int i1,int j1,int i2,int j2)
{
int temp[50];
int i,j,k;
i=i1; //first list
j=i2; //second list
k=0;

while(i<=j1 && j<=j2)  // elements in both list
{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}

while(i<=j1)  // copy remaining element
temp[k++]=a[i++];

while(j<=j2)
temp[k++]=a[j++];

for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j]; // copy element from temp[] to a[]
}



Output:





Download Executable C file:
Download 


If you found any error in this code, or any suggestion comment in below comment section


How to add and where to add executable cpp file

write a program to implement selection sort || Executable file download link

C++ PROGRAM TO IMPLEMENT STACK USING ARRAY | with executable cpp file

Program to find factorial of Number in C++ | Interesting321

C++ program to find largest of three numbers using inline function


Wednesday, 16 January 2019

write a program to implement selection sort in c

Hello friends,  selection sort is one of the sorting method, in this method we select first number and compare with second number, if second number is smaller than first number than swap both the number, if second number is larger, than it moves to next position, this method is repeat until reach last position


Ex:

selection sort :


4 2 5 7 9                It's a given number

4<->2 5 7 9            4 and 2 is compare if   2<4-->"true" than swap.

2 4 <->5 7 9          compare with next two position if 5<4-->"False"   

2 4 5 7 9

2 4 5 <->7 9     (7<5)-->False

2 4 5 7 9 

2 4 5 7 <->9   (9<7)-->False

Round two begins until sorting.

 implement selection sort in c :

#include <stdio.h>
int main()
{
  int a[100], n, i, j, position, swap;
  printf("Enter number of elements\n");
  scanf("%d", &n);
  printf("Enter %d integers\n", n);
  for (i = 0; i < n; i++)
    scanf("%d", &array[c]);
  for (i = 0; i < (n - 1); i++)
  {
    position = i;
   
    for (j = j + 1; j < n; j++)
    {
      if (array[position] > array[d])
        position = j;
    }
    if (position != c)
    {
      swap = a[i];
      a[i] = a[position];
      a[position] = swap;
    }
  }
  printf("Sorted list in ascending order:\n");

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


Output Window:




Download Executable .c file click below link

 Download