Binary Search In C Program Using Recursion

Binary Search In C Program Using Recursion. If you are looking for a binary search in C with recursion example, this C programming tutorial will help you to learn how to write a program for binary search in C. Just go through this C programming example to learn about binary search, we are sure that you will be able to write a C program for binary search using recursion.

Binary Search In C

Binary Search in C – Table of Contents

What is Binary Search Algorithm?

Binary search is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the sorted array, if they are unequal, the half in which the target cannot lie is eliminated and the search continues for the remaining half until it is successful. Binary search is designed for fast searching.

Binary Search in C Using Recursion

Learn how to write a  program for binary search in c using recursion. Writing a binary search C program can be done using various techniques but here in this program, we show how to write a binary search program in c using recursion in a proper way. Happy coding.

Binary Search in C Program Using Recursion – Source Code

You can copy paste the below binary search program in c compiler to check how the source code work. Or write your own Binary Search C Program Using recursion with the help of this below c program for binary search.

/* BINARY SEARCH PROGRAM IN C USING RECURSION - BINARYSEARCH.C */

#include<stdio.h>

#include<stdlib.h>

#define size 10

 

int binsearch(int[], int, int, int);

 

int main() {

   int num, i, key, position;

   int low, high, list[size];

 

   printf("\nEnter the total number of elements");

   scanf("%d", &num);

 

   printf("\nEnter the elements of list :");

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

      scanf("%d", &list[i]);

   }

 

   low = 0;

   high = num - 1;

 

   printf("\nEnter element to be searched : ");

   scanf("%d", &key);

 

   position = binsearch(list, key, low, high);

 

   if (position != -1) {

      printf("\nNumber present at %d", (position + 1));

   } else

      printf("\n The number is not present in the list");

   return (0);

}

 

// Binary search function for binary search

int binsearch(int a[], int x, int low, int high) {

   int mid;

 

   if (low > high)

      return -1;

 

   mid = (low + high) / 2;

 

   if (x == a[mid]) {

      return (mid);

   } else if (x < a[mid]) {

      binsearch(a, x, low, mid - 1);

   } else {

      binsearch(a, x, mid + 1, high);

   }

}

C PROGRAM FOR Binary search – OUTPUT

After you compile and run the above binary search program in c using recursion, your C compiler asks you to enter elements for the sorted array to perform the binary search. After you enter elements, the program will be executed and give output.

Enter the total number of elements: 7
Enter the elements of list : 10 21 32 43 54 65 76
Enter element to be searched: 32
Number present at 3

C PROGRAMMING Tutorials

  1. Bubble Sort in C Using Pointers
  2. Bubble Sort Program in C Using Recursion
  3. Bubble Sort Program in C Using Array
  4. Bubble Sort Program in C Using Function
  5. Bubble Sort Program in C Using Linked List
  6. Stack Push Pop Program in C Using Arrays
  7. Factorial Program in C Using Pointers
  8. Factorial Program in C Using While Loop
  9. C Program For Factorial Using For Loop
  10. Factorial Program in C Using Recursion Function
  11. C Program To Reverse a String Using Pointers
  12. C Program To Swap Two Numbers Using Two Variables
  13. C Program To Swap Two Numbers Using Three Variables
  14. C Program For Prime Numbers – Check  a Number is Prime or Not
  15. C Program To Reverse a String with Using Function
  16. C Program to Reverse a String without Using Function
  17. C Program to Reverse a Sting Using Recursion
  18. C Program For Palindrome String
  19. C Program For Palindrome Numbers
  20. C Program To Reverse a String with Using Function
  21. C Program To Reverse a String without Using Function
  22. C Program To Reverse a String Using Recursion

Bubble Sort In C Using Liked List Program

Bubble Sort In C Using Linked List. If you are looking for a bubble sort program in C with linked list example, this C programming tutorial will help you to learn how to write a program for bubble sort in C. Just go through this C programming example to learn about bubble sort, we are sure that you will be able to write a C program for bubble sort using linked list.

Bubble Sort in C Using Linked List

Bubble Sort in C – Table of Contents

What is Bubble Sort?

Bubble Sort in C – Bubble sort is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order.

Bubble Sort Example

This below image illustrates what is Bubble Sort in C.

Bubble Sort Program in C
Bubble Sort Program in C

Bubble Sort in C Programming Tutorials

  1. Bubble Sort in C Using Pointers
  2. Bubble Sort Program in C Using Recursion
  3. Bubble Sort Program in C Using Array
  4. Bubble Sort Program in C Using Function

Bubble Sort in C Using Linked List

Learn how to write a  bubble sort in c using linked list. Writing a Bubble Sort C Program can be done using various techniques like an array, pointersrecursionfunction but here in this program, we show how to write a bubble sort program in c using linked list in a proper way.

Bubble Sort Program in C Using Linked List – Source Code

You can copy paste the below bubble sort program in c compiler to check how the source code work. Or write your own Bubble Sort in C Using linked list with the help of this below c program for bubble sort.

/* BUBBLE SORT PROGRAM IN C USING LINKED LIST - BUBBLESORT.C */

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
 
struct lnode {
 int data;
 struct lnode *next;
} *head, *visit;
 
/* adding a new entry to the linked list */
void llist_add(struct lnode **q, int num);
/* preforming a bubble sort on the linked list */
void llist_bubble_sort(void);
/* printing the entire linked list */
void llist_print(void);
 
int main(void) 
{
 
/* linked list */
 struct lnode *newnode = NULL;
 int i = 0,num,a[20]; /* variable declaration */
 
 printf("\n\n\t BUBBLE SORT IN C USING LINKED LIST\n"); 
 printf("\nEnter the number of elements : ");
 scanf("%d",&num); //Reading the number of elements from user
 /* Enter some random elements into the linked list */
 printf("\nEnter the elements to be sorted : \n");
 for(i = 0; i < num; i++) 
{
 scanf("%d",&a[i]);
 llist_add(&newnode, a[i]);
 }
 
 head = newnode;
 printf("\nList of input elements :\n");
 
 llist_print();
  
 printf("\n\nSorted List of elements by bubble sort :\n");
  
 llist_bubble_sort();
 llist_print();
 getch();
 return 0;
}
 
/* This logic will add a node at the end of a linked list */
void llist_add(struct lnode **q, int num) {
 struct lnode *tmp;
 
 tmp = *q;
 
 /* if the list is empty, create first node */
 if(*q == NULL) {
 *q = malloc(sizeof(struct lnode));
 tmp = *q;
 } else {
 /* go to last node */
 while(tmp->next != NULL)
 tmp = tmp->next;
 
 /* add node at the end */
 tmp->next = malloc(sizeof(struct lnode));
 tmp = tmp->next;
 }
 
 /* assign data to the last node */
 tmp->data = num;
 tmp->next = NULL;
}
 
/* print the entire linked list */
void llist_print(void) {
 visit = head;
 
 while(visit != NULL) {
 printf("%d\t", visit->data);
 visit = visit->next;
 }
 printf("\n");
}
 
/* preforming a bubble sort in c using linked list */
void llist_bubble_sort(void) {
 struct lnode *a = NULL;
 struct lnode *b = NULL;
 struct lnode *c = NULL;
 struct lnode *e = NULL;
 struct lnode *tmp = NULL;
 
 /*
 // the `c' node precedes the `a' and `e' node
 // pointing up the node to which the comparisons
 // are being made.
 */
 while(e != head->next) {
 c = a = head;
 b = a->next;
 while(a != e) {
 if(a->data > b->data) {
 if(a == head) {
 tmp = b -> next;
 b->next = a;
 a->next = tmp;
 head = b;
 c = b;
 } else {
 tmp = b->next;
 b->next = a;
 a->next = tmp;
 c->next = b;
 c = b;
 }
 } else {
 c = a;
 a = a->next;
 }
 b = a->next;
 if(b == e)
 e = a;
 }
 }
}

C PROGRAM FOR BUBBLE SORT – OUTPUT

After you compile and run the above bubble sort program in c using linked list, your C compiler asks you to enter elements for bubble sort. After you enter elements, the program will be executed and give output.

C PROGRAMMING EXAMPLES

Bubble Sort In C Using Pointers Program

Bubble Sort In C Using Pointers. If you are looking for a bubble sort program in C with pointers example, this C programming tutorial will help you to learn how to write a program for bubble sort in C. Just go through this C programming example to learn about bubble sort, we are sure that you will be able to write a C program for bubble sort using pointers.

Bubble Sort in C Using Pointers

Bubble Sort in C – Table of Contents

What is Bubble Sort?

Bubble Sort in C is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order.

Bubble Sort Example

This below image illustrates what is Bubble Sort in C.

Bubble Sort Program in C
Bubble Sort Program in C

C Programming Tutorials

  1. Bubble Sort Program in C Using Recursion
  2. Bubble Sort Program in C Using Array
  3. Bubble Sort Program in C Using Function
  4. Stack Push Pop Program in C Using Arrays
  5. Factorial Program in C Using Pointers
  6. Factorial Program in C Using While Loop
  7. C Program For Factorial Using For Loop
  8. Factorial Program in C Using Recursion Function

Bubble Sort in C Using Pointers

Learn how to write a  bubble sort in c using pointers. Writing a Bubble Sort C Program can be done using various techniques like an array, pointers, recursionfunction but here in this program, we show how to write a bubble sort program in c using pointers in a proper way.

Bubble Sort Program in C Using Pointers – Source Code

You can copy paste the below bubble sort program in c compiler to check how the source code work. Or write your own Bubble Sort in C Using Pointers with the help of this below c program for bubble sort.

/* BUBBLE SORT PROGRAM IN C USING POINTERS - BUBBLESORT.C */

#include<stdio.h>

int *a[100],i,j,item;

void main()

{

   void sort(),display(); //function declarations

   int i;

   clrscr();

   printf("\n Enter the number of elements in the first array\n");

   scanf("%d",&item); //reding elements from user

   printf("\n Enter %d numbers\n",item);

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

   scanf("%d",&a[i]);
  
   sort(); //calling sort function to perform bubble sort

   display(); //calling display function to display elements

}

 void sort() //sort function to sort elements

 {

   int swap=1,*temp;

   for(i=0;i<item && swap==1;++i)

   {

    swap=0;

    for(j=0;j<item-(i+1);++j)

    if (a[j]>a[j+1])
    {

     temp=a[j];

    a[j]=a[j+1];

    a[j+1]=temp;

    swap=1;
   }

  }

 }


 void display() //display function

 {

  printf("\n Sorted elements are:\n");

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

  printf("%d\n",a[i]);

  getch();

 }

C PROGRAM FOR BUBBLE SORT – OUTPUT

After you compile and run the above bubble sort program in c using pointers, your C compiler asks you to enter elements for bubble sort. After you enter elements, the program will be executed and give output.

C PROGRAMMING EXAMPLES

Bubble Sort Program In C Using Recursion

Bubble Sort In C Using Recursion – If you are looking for a bubble sort program in C with recursion example, this C programming tutorial will help you to learn how to write a program for bubble sort in C. Just go through this C programming example to learn about bubble sort, we are sure that you will be able to write a C program for bubble sort using recursion.

Bubble Sort in C

Table of Contents

What is Bubble Sort?

Bubble Sort is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order.

Bubble Sort Example

This below image illustrates what is Bubble Sort in C.

Bubble Sort Program in C
Bubble Sort Program in C

C Programming Tutorials

  1. Bubble Sort Program in C Using Array
  2. Bubble Sort Program in C Using Function
  3. Stack Push Pop Program in C Using Arrays
  4. Factorial Program in C Using Pointers
  5. Factorial Program in C Using While Loop
  6. C Program For Factorial Using For Loop
  7. Factorial Program in C Using Recursion Function

Bubble Sort in C Using Recursion

Learn how to write a  bubble sort in c using recursion. Writing a Bubble Sort C Program can be done using various techniques like an array, pointers, recursion, function but here in this program, we show how to write a bubble sort program in c using recursion in a proper way.

Bubble Sort Program in C Using Recursion – Source Code

You can copy paste the below bubble sort program in c compiler to check how the source code work. Or write your own Bubble Sort in C Using Function with the help of this below c program for bubble sort.

/* BUBBLE SORT PROGRAM IN C USING RECURSION - BUBBLESORT.C */

#include <stdio.h>
  #include <stdlib.h>

  /* sorts the given numbers in ascending order */
  void bubbleSort(int *data, int n) {
        int i, temp;

        if (n > 0) {
                for (i = 1; i < n; i++) {
                        if (data[i - 1] > data[i]) {
                                temp = data[i];
                                data[i] = data[i - 1];
                                data[i - 1] = temp;
                        }
                }

                bubbleSort(data, n - 1);
        }

        return;
  }

  int main() {
        int i, n, *data;

        /* get the number of inputs from the user */
        printf("Enter the number of inputs:");
        scanf("%d", &n);

        /* dynamically allocate memory to store i/p values */
        data = (int *) malloc(sizeof(int) * n);

        /* get the input data from the user */
        for (i = 0; i < n; i++) {
                printf("data[%d]: ", i);
                scanf("%d", &data[i]);
        }
        /* sorts the given numbers */

        bubbleSort(data, n);




        /* print the sorted numbers */

        printf("Data After Bubble Sort:\n");

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

                printf("%d ", data[i]);

        }

        printf("\n");




        return 0;

  }

C PROGRAM FOR BUBBLE SORT – OUTPUT

After you compile and run the above bubble sort program in c using recursion, your C compiler asks you to enter elements for bubble sort. After you enter elements, the program will be executed and give output like below expected output using bubble sort functionality.

 

C PROGRAMMING EXAMPLES

Bubble Sort Program In C Using Function

Bubble Sort Program In C Using Function. If you are looking for a bubble sort program in C with function example, this C programming tutorial will help you to learn how to write a program for bubble sort in C. Just go through this C programming tutorial to learn about bubble sort, we are sure that you will be able to write a C program for bubble sort using function.

Bubble Sort in C

Table of Contents

What is Bubble Sort?

Bubble Sort is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order.

Bubble Sort Example

This below image illustrates what is Bubble Sort in C.

Bubble Sort Program in C
Bubble Sort Program in C

C Programming Tutorials

  1. Bubble Sort Program in C Using Array
  2. Stack Push Pop Program in C Using Arrays
  3. Factorial Program in C Using Pointers
  4. Factorial Program in C Using While Loop
  5. C Program For Factorial Using For Loop
  6. Factorial Program in C Using Recursion Function

Bubble Sort in C with Function

Learn how to write a  bubble sort in c using function. Writing a Bubble Sort C Program can be done using various techniques like an array, pointers, recursion, function but here in this program, we show how to write a bubble sort program in c using function in a proper way.

Bubble Sort Program in C Using Function – Source Code

You can copy paste the below bubble sort program in c compiler to check how the source code work. Or write your own Bubble Sort in C Using Function with the help of this below c program for bubble sort.

/* BUBBLE SORT PROGRAM IN C USING FUNCTION - BUBBLESORT.C */

#include<stdio.h>
#include<stdlib.h>
void bubblesort(int a[],int size);
void main()
{
  int a[50],n,i;
  printf("\n Enter the size of the array");
  scanf("%d",&n);
  if(n>50)
  {
    printf("\n error");
    exit(0);
  }

 printf("\n Enter the array elements: \n");

 for(i=0;i<n;i++)
  scanf("%d",&a[i]);
  bubblesort(a,n);
  printf("\n The sorted array is\n");
  for(i=0;i<n;i++)
  printf("%d\t",a[i]);
}
void bubblesort(int a[],int size)
{
  int temp,i,j;
  for(i=0;i<size;i++)
  {
   for(j=0;j<size-1;j++)
   {
    if(a[j]>a[j+1])
    {
     temp=a[j];
     a[j]=a[j+1];
     a[j+1]=temp;
    }
  }
 }
}

C PROGRAM FOR BUBBLE SORT – OUTPUT

After you compile and run the above bubble sort program in c using function, your C compiler asks you to enter elements for bubble sort. After you enter elements, the program will be executed and give output like below expected output using bubble sort functionality.

Output:

Enter the size of the array: 5

Enter the array elements: 29541

The sorted array is 12459

C PROGRAMMING EXAMPLES

Stack Push Pop Program In C Using Arrays With Example

Stack push pop program in c using arrays with an example. If you are looking for a stack push pop program in c, this C programming tutorial will help you to learn how to code stack program in c using arrays. Just go through this C programming example, you will be able to write a C program to push and pop.

Stack Push Pop Program in C

Table of Contents

Stack Push Pop Program in C using Arrays

Learn how to write a  stack program in C using arrays. Writing a stack push pop program in C can be done using various techniques but here in this program, we show how to write a C program to push and pop using arrays in a proper way.

C Programming Tutorials

  1. Factorial Program in C Using Pointers
  2. Factorial Program in C Using While Loop
  3. C Program For Factorial Using For Loop
  4. Factorial Program in C Using Recursion Function
  5. C Program For Palindrome String
  6. C Program For Palindrome Numbers
  7. C Program To Reverse a String with Using Function
  8. C Program To Reverse a String without Using Function
  9. C Program To Reverse a String Using Recursion

Push Function Creation

/*STACK PUSH() AND POP() IMPLEMENTATION USING ARRAYS*/

#include <stdio.h>

#include<conio.h>

#define MAX 5

int top, status;

 

/*PUSH FUNCTION*/

void push (int stack[], int item)

{   if (top == (MAX-1))

status = 0;

    else

    {   status = 1;

++top;

stack [top] = item;

    }

}

Pop Function Creation

/*POP FUNCTION*/

int pop (int stack[])

{  

int ret;

    if (top == -1)

    {   ret = 0;

status = 0;

    }

    else

    {   status = 1;

ret = stack [top];

--top;

    }

return ret;

}

Function To Display Stack 

/*FUNCTION TO DISPLAY STACK*/

void display (int stack[])

{   int i;

    printf ("\nThe Stack is: ");

    if (top == -1)

printf ("empty");

    else

    {   for (i=top; i>=0; --i)

  printf ("\n--------\n|%3d   |\n--------",stack[i]);

    }

    printf ("\n");

}

Stack Push Pop Program in C Source Code

/*MAIN PROGRAM*/

void main()

{  

int stack [MAX], item;

    int ch;

    clrscr ();

    top = -1;

 

    do

    {  do

       {   printf ("\NMAIN MENU");

  printf ("\n1.PUSH (Insert) in the Stack");

  printf ("\n2.POP  (Delete) from the Stack");

  printf ("\n3.Exit (End the Execution)");

  printf ("\nEnter Your Choice: ");

  scanf  ("%d", &ch);

  if (ch<1 || ch>3)

      printf ("\nInvalid Choice, Please try again");

}while (ch<1 || ch>3);

       switch (ch)

       {case 1:

printf ("\nEnter the Element to be pushed : ");

scanf  ("%d", &item);

printf (" %d", item);

push (stack, item);

if (status)

{   printf ("\nAfter Pushing ");

   display (stack);

   if (top == (MAX-1))

printf ("\nThe Stack is Full");

}

else

   printf ("\nStack overflow on Push");

break;

       case 2:

item = pop (stack);

if (status)

{    printf ("\nThe Popped item is %d.  After Popping: ");

    display (stack);

}

else

    printf ("\nStack underflow on Pop");

break;

       default:

printf ("\nEND OF EXECUTION");

       }

    }while (ch != 3);

getch();

}

C PROGRAMMING EXAMPLES