Initializing...

Preparing the app. This may take a moment.

Still loading… your network may be slow or a script is blocked.

DSL notes

Author:

| Size: 20.20 KB

|

1.Given {4,7,3,2,1,7,9,0}, find the location of 7 using Binary search and also display its first occurrence. #include<stdio.h> #include<conio.h>  void main() { int a[10]={4,7,3,2,1,7,9,0}; int i, j, n, low, high, mid, temp, key;

n=8;
printf("\n Given Array elements are:\n");
for(i=0;i<n;i++)
    printf("\n%d",a[i]);&nbsp;
for(i=0;i<n;i++)
{
    for(j=i+1;j<n;j++)
    {
        if(a[i]>a[j])
        {
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
        }
    }
}&nbsp;
printf("\nSorted Array elements are");
for(i=0;i<n;i++)
    printf("\n %d",a[i]);&nbsp;
printf("\n enter the element to be searched");
scanf("%d",&key);&nbsp;
low=0;
high=n-1;
while(low<=high)
{
    mid=(low+high)/2;
    if(key==a[mid])
    {
        printf("\n key %d found succesfully at position %d",key,mid + 1);
        break;
    }
    else if(key<a[mid])
        high=mid-1;
    else
        low=mid+1;
}&nbsp;
if(low>high)
    printf("\n key %d not found",key);&nbsp;
getch();

} 2.Given (5,3,1,6,0,2,4} order the numbers in ascending order using Quick Sort. #include <stdio.h> void quick_sort(int a[], int lb, int ub) { if (lb < ub) { int key = a[lb], i = lb + 1, j = ub, temp; while (i <= j) { while (i <= ub && a[i] < key) i++; while (j >= lb && a[j] > key) j--; if (i < j) { temp = a[i]; a[i] = a[j]; a[j] = temp; } else { break; } } temp = a[lb]; a[lb] = a[j]; a[j] = temp; quick_sort(a, lb, j - 1); quick_sort(a, j + 1, ub);} } int main() { int i, n, a[20]; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter elements: "); for (i = 0; i < n; i++) scanf("%d", &a[i]); quick_sort(a, 0, n - 1); printf("The sorted elements are: "); for (i = 0; i < n; i++) printf("%4d", a[i]); return 0;}  3.Perform the Merge Sort on the input {75,8,1,16,48,3,7,0} and display the output in descending order. #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<alloc.h> void mergesort(int a[], int i, int j); void merge(int a[], int i1, int j1, int i2, int j2);  int main() { int a[8]={75,8,1,16,48,3,7,0}, i; printf("Array elements are:"); for(i=0; i<8; i++) printf("%d\t",a[i]); mergesort(a, 0, 7); printf("\nSorted array is :"); for(i=0; i<8; i++) printf("%d\t",a[i]); getch(); return 0; } void mergesort(int a[], int i, int j) { int mid; if(i < j) { mid = (i+j) / 2; mergesort(a, i, mid); mergesort(a, mid+1, j); merge(a, i, mid, mid+1, j); } } void merge(int a[], int i1, int j1, int i2, int j2) { int temp[50]; int i, j, k; i = i1; j = i2; k = 0; while(i <= j1 && j <= j2) { if(a[i] > a[j]) temp[k++] = a[i++]; else temp[k++] = a[j++]; } while(i <= j1) temp[k++] = a[i++]; while(j <= j2) temp[k++] = a[j++]; for(i = i1, j = 0; i <= j2; i++, j++) a[i] = temp[j]; }  4. Write a program to insert the elements 61,16,8,27 into singly linked list and delete 8,61,27 from the list. Display your list after each insertion and deletion. #include <stdio.h> #include <stdlib.h> typedef struct node { int value; struct node *next; } DATA_NODE; DATA_NODE *first_node = NULL; void insert(int data) { DATA_NODE *temp_node = (DATA_NODE *) malloc(sizeof (DATA_NODE)); temp_node->value = data; temp_node->next = NULL; if (first_node == NULL) { first_node = temp_node; } else { DATA_NODE *head_node = first_node; while (head_node->next != NULL) { head_node = head_node->next; } head_node->next = temp_node; } } void delete(int pos) { int countvalue = 0; DATA_NODE *temp_node = first_node; DATA_NODE *next_node; while (temp_node != NULL) { countvalue++; temp_node = temp_node->next; } if (pos > 0 && pos <= countvalue) { temp_node = first_node; if (pos == 1) { first_node = temp_node->next; free(temp_node); } else { int i; for (i = 1; i < pos - 1; i++) { temp_node = temp_node->next; } next_node = temp_node->next; temp_node->next = next_node->next; free(next_node); } } else { printf("\nInvalid Position \n\n"); } } void display() { int count = 0; DATA_NODE *temp_node = first_node; printf("\nDisplay Linked List : \n"); while (temp_node != NULL) { printf("# %d # ", temp_node->value); count++; temp_node = temp_node->next; } printf("\nNo Of Items In Linked List : %d\n", count); } int main() { int option = 0; printf("Singly Linked List Exampl…

Comments

No comments yet

Please complete the captcha

7/14/2024

Create new paste with same settings

Not all user generated content is reviewed by AnonPaste. If you believe this paste violates our community guideline or terms of service, please report it here.

Initializing...

Preparing the app. This may take a moment before app is ready.

AnonPaste is a user-generated content hosting service. The platform and its operators are not responsible for content posted by users.