AkshayHadapad

CLabDSA

Sep 16th, 2021 (edited)
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.98 KB | None | 0 0
  1. //Binary Search
  2. #include <stdio.h>
  3. int iterativeBinarySearch(int array[], int start_index, int end_index, int element){
  4.    while (start_index <= end_index){
  5.       int middle = start_index + (end_index- start_index )/2;
  6.       if (array[middle] == element)
  7.          return middle;
  8.       if (array[middle] < element)
  9.          start_index = middle + 1;
  10.       else
  11.          end_index = middle - 1;
  12.    }
  13.    return -1;
  14. }
  15. int main(void){
  16.    int array[] = {1, 4, 7, 9, 16, 56, 70};
  17.    int n = 7;
  18.    int element = 16;
  19.    int found_index = iterativeBinarySearch(array, 0, n-1, element);
  20.    if(found_index == -1 ) {
  21.       printf("Element not found in the array ");
  22.    }
  23.    else {
  24.       printf("Element found at index : %d",found_index);
  25.    }
  26.    return 0;
  27.     getch();
  28. }
  29.  
  30. //Merge Sort
  31. #include <stdio.h>
  32.  
  33. #define max 10
  34.  
  35. int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };
  36. int b[10];
  37.  
  38. void merging(int low, int mid, int high) {
  39.    int l1, l2, i;
  40.  
  41.    for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {
  42.       if(a[l1] <= a[l2])
  43.          b[i] = a[l1++];
  44.       else
  45.          b[i] = a[l2++];
  46.    }
  47.    
  48.    while(l1 <= mid)    
  49.       b[i++] = a[l1++];
  50.  
  51.    while(l2 <= high)  
  52.       b[i++] = a[l2++];
  53.  
  54.    for(i = low; i <= high; i++)
  55.       a[i] = b[i];
  56. }
  57.  
  58. void sort(int low, int high) {
  59.    int mid;
  60.    
  61.    if(low < high) {
  62.       mid = (low + high) / 2;
  63.       sort(low, mid);
  64.       sort(mid+1, high);
  65.       merging(low, mid, high);
  66.    } else {
  67.       return;
  68.    }  
  69. }
  70.  
  71. int main() {
  72.    int i;
  73.  
  74.    printf("List before sorting\n");
  75.    
  76.    for(i = 0; i <= max; i++)
  77.       printf("%d ", a[i]);
  78.  
  79.    sort(0, max);
  80.  
  81.    printf("\nList after sorting\n");
  82.    
  83.    for(i = 0; i <= max; i++)
  84.       printf("%d ", a[i]);
  85. }
  86.  
  87.  
  88. //STACk
  89. #include <stdio.h>
  90. #define MAXSIZE 5
  91.  
  92. struct stack
  93. {
  94.     int stk[MAXSIZE];
  95.     int top;
  96. };
  97. typedef struct stack STACK;
  98. STACK s;
  99.  
  100. void push(void);
  101. int  pop(void);
  102. void display(void);
  103.  
  104. void main ()
  105. {
  106.     int choice;
  107.     int option = 1;
  108.     s.top = -1;
  109.  
  110.     printf ("STACK OPERATION\n");
  111.     while (option)
  112.     {
  113.         printf ("------------------------------------------\n");
  114.         printf ("      1    -->    PUSH               \n");
  115.         printf ("      2    -->    POP               \n");
  116.         printf ("      3    -->    DISPLAY               \n");
  117.         printf ("      4    -->    EXIT           \n");
  118.         printf ("------------------------------------------\n");
  119.  
  120.         printf ("Enter your choice\n");
  121.         scanf    ("%d", &choice);
  122.         switch (choice)
  123.         {
  124.         case 1:
  125.             push();
  126.             break;
  127.         case 2:
  128.             pop();
  129.             break;
  130.         case 3:
  131.             display();
  132.             break;
  133.         case 4:
  134.             return;
  135.         }
  136.         fflush (stdin);
  137.         printf ("Do you want to continue(Type 0 or 1)?\n");
  138.         scanf    ("%d", &option);
  139.     }
  140.     getch();
  141. }
  142. /*  Function to add an element to the stack */
  143. void push ()
  144. {
  145.     int num;
  146.     if (s.top == (MAXSIZE - 1))
  147.     {
  148.         printf ("Stack is Full\n");
  149.         return;
  150.     }
  151.     else
  152.     {
  153.         printf ("Enter the element to be pushed\n");
  154.         scanf ("%d", &num);
  155.         s.top = s.top + 1;
  156.         s.stk[s.top] = num;
  157.     }
  158.     return;
  159. }
  160. /*  Function to delete an element from the stack */
  161. int pop ()
  162. {
  163.     int num;
  164.     if (s.top == - 1)
  165.     {
  166.         printf ("Stack is Empty\n");
  167.         return (s.top);
  168.     }
  169.     else
  170.     {
  171.         num = s.stk[s.top];
  172.         printf ("poped element is = %dn", s.stk[s.top]);
  173.         s.top = s.top - 1;
  174.     }
  175.     return(num);
  176. }
  177. /*  Function to display the status of the stack */
  178. void display ()
  179. {
  180.     int i;
  181.     if (s.top == -1)
  182.     {
  183.         printf ("Stack is empty\n");
  184.         return;
  185.     }
  186.     else
  187.     {
  188.         printf ("\n The status of the stack is \n");
  189.         for (i = s.top; i >= 0; i--)
  190.         {
  191.             printf ("%d\n", s.stk[i]);
  192.         }
  193.     }
  194.     printf ("\n");
  195. }
Add Comment
Please, Sign In to add comment