Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Binary Search
- #include <stdio.h>
- int iterativeBinarySearch(int array[], int start_index, int end_index, int element){
- while (start_index <= end_index){
- int middle = start_index + (end_index- start_index )/2;
- if (array[middle] == element)
- return middle;
- if (array[middle] < element)
- start_index = middle + 1;
- else
- end_index = middle - 1;
- }
- return -1;
- }
- int main(void){
- int array[] = {1, 4, 7, 9, 16, 56, 70};
- int n = 7;
- int element = 16;
- int found_index = iterativeBinarySearch(array, 0, n-1, element);
- if(found_index == -1 ) {
- printf("Element not found in the array ");
- }
- else {
- printf("Element found at index : %d",found_index);
- }
- return 0;
- getch();
- }
- //Merge Sort
- #include <stdio.h>
- #define max 10
- int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };
- int b[10];
- void merging(int low, int mid, int high) {
- int l1, l2, i;
- for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {
- if(a[l1] <= a[l2])
- b[i] = a[l1++];
- else
- b[i] = a[l2++];
- }
- while(l1 <= mid)
- b[i++] = a[l1++];
- while(l2 <= high)
- b[i++] = a[l2++];
- for(i = low; i <= high; i++)
- a[i] = b[i];
- }
- void sort(int low, int high) {
- int mid;
- if(low < high) {
- mid = (low + high) / 2;
- sort(low, mid);
- sort(mid+1, high);
- merging(low, mid, high);
- } else {
- return;
- }
- }
- int main() {
- int i;
- printf("List before sorting\n");
- for(i = 0; i <= max; i++)
- printf("%d ", a[i]);
- sort(0, max);
- printf("\nList after sorting\n");
- for(i = 0; i <= max; i++)
- printf("%d ", a[i]);
- }
- //STACk
- #include <stdio.h>
- #define MAXSIZE 5
- struct stack
- {
- int stk[MAXSIZE];
- int top;
- };
- typedef struct stack STACK;
- STACK s;
- void push(void);
- int pop(void);
- void display(void);
- void main ()
- {
- int choice;
- int option = 1;
- s.top = -1;
- printf ("STACK OPERATION\n");
- while (option)
- {
- printf ("------------------------------------------\n");
- printf (" 1 --> PUSH \n");
- printf (" 2 --> POP \n");
- printf (" 3 --> DISPLAY \n");
- printf (" 4 --> EXIT \n");
- printf ("------------------------------------------\n");
- printf ("Enter your choice\n");
- scanf ("%d", &choice);
- switch (choice)
- {
- case 1:
- push();
- break;
- case 2:
- pop();
- break;
- case 3:
- display();
- break;
- case 4:
- return;
- }
- fflush (stdin);
- printf ("Do you want to continue(Type 0 or 1)?\n");
- scanf ("%d", &option);
- }
- getch();
- }
- /* Function to add an element to the stack */
- void push ()
- {
- int num;
- if (s.top == (MAXSIZE - 1))
- {
- printf ("Stack is Full\n");
- return;
- }
- else
- {
- printf ("Enter the element to be pushed\n");
- scanf ("%d", &num);
- s.top = s.top + 1;
- s.stk[s.top] = num;
- }
- return;
- }
- /* Function to delete an element from the stack */
- int pop ()
- {
- int num;
- if (s.top == - 1)
- {
- printf ("Stack is Empty\n");
- return (s.top);
- }
- else
- {
- num = s.stk[s.top];
- printf ("poped element is = %dn", s.stk[s.top]);
- s.top = s.top - 1;
- }
- return(num);
- }
- /* Function to display the status of the stack */
- void display ()
- {
- int i;
- if (s.top == -1)
- {
- printf ("Stack is empty\n");
- return;
- }
- else
- {
- printf ("\n The status of the stack is \n");
- for (i = s.top; i >= 0; i--)
- {
- printf ("%d\n", s.stk[i]);
- }
- }
- printf ("\n");
- }
Add Comment
Please, Sign In to add comment