Posts

Showing posts from May, 2022

BinarySearch (personal)

  #include <stdio.h> struct emp {     int no ;     int sal ;     char name [ 20 ]; }; int binarySearch ( struct emp a [] , int n , int x ) {     int low = 0 , mid , high = n - 1 , count = 0 ;     while ( low <= high )     {         count ++;         mid = ( low + high ) / 2 ;         if ( x < a [ mid ]. no )             high = mid - 1 ;         else if ( x > a [ mid ]. no )             low = mid + 1 ;         else         {             printf ( "no of search =   %d \n " , count );             return mid ;         }     }     return - 1 ; } void printArray ( struct emp array [] , int size ) { ...

quicksort (personal)

  #include <stdio.h> struct Student {         char student_name [ 50 ];     int student_roll_no ;     int total_marks ; }; void swap ( struct Student * a , struct Student * b ) {     struct Student t = * a ;     * a = * b ;     * b = t ; } int count = 1 ; int partition ( struct Student arr [] , int low , int high ) {     int pivot = arr [ high ]. student_roll_no ;     int i = ( low - 1 );     for ( int j = low ; j <= high - 1 ; j ++)     {         if ( arr [ j ]. student_roll_no < pivot )         {             i ++;             swap (& arr [ i ], & arr [ j ]);             count ++;         }     }     swap (& arr [ i + 1 ], & arr [ high ]); ...

Practical 8 by Karan

 #include <stdio.h> struct emp { int no; int sal; char name[20]; };   int binarySearch(struct emp a[], int n, int x) {        int low=0, mid, high=n-1, count = 0;    while(low <= high)    {        count++;         mid = (low + high) / 2;         if (x < a[mid].no)             high = mid - 1;         else if (x > a[mid].no)          low = mid + 1;        else {         printf("no of search = %d\n",count);       return mid;        }    } return -1; }    void printArray(struct emp array[], int size) {   for (int i = 0; i < size; ++i) {      printf("name = %s\n",array[i].name);     printf(" salary = %d\n ", array[i].sal);      printf(" number = %d...

Practical 7 by Karan

 // Quick sort in C #include <stdio.h> #include <stdlib.h> #include <string.h>  struct student { char name[20]; int rollno; int mark; } ; // function to find the partition position int partition(struct student array[], int low, int high) {      // select the rightmost element as pivot   int pivot = array[high].rollno;      // pointer for greater element   int i = (low - 1);   // traverse each element of the array   // compare them with the pivot   for (int j = low; j < high; j++) {     if (array[j].rollno<= pivot) {                // if element smaller than pivot is found       // swap it with the greater element pointed by i       i++;              // swap element at i with element at j           struct student temp ;      temp = array[i]; ...

Practical 10 by Paxton

 #include <stdio.h> #include <string.h> void printsubstring(char arr[],int start,int end) {     int i;     for(i=start;i<=end;i++)     {         printf("%c ",arr[i]);     }     printf("\n"); } void substringmatch(char arr[],char a,char b) {     int i,j;     int count1=0,count2=0;     for(i=0;i<strlen(arr)-2;i++)                 {         if(arr[i]==a)         {             for(j=strlen(arr)-1;j>i;j--)             {                 if(arr[j]==b)                 {                     count1++;                     printsubstring(arr,i,j);   ...

Practical 9 by Paxton

 #include<stdio.h> int main() {     int i,j,n;     int ot[25],od[25],add[25],k,o[25]={0};     printf("Enter the no or orders:");     scanf("%d",&n);     printf("Enter the order Details:\n");     for(i=0;i<n;i++)     {         printf("Order time:");         scanf("%d",&ot[i]);         printf("Order Duration:");         scanf("%d",&od[i]);         add[i]=ot[i]+od[i];         k=add[i]%n;        //printf("%d\n",k);        while(1)        {             if(o[k]==0)             {                 o[k]=i+1;                 //printf("%d\n",k);             ...

Practical 8 by Paxton

 #include<stdio.h> struct emp {     char ename[20];     int eno;     char esal[7]; }; int main() {     struct emp e[10];     int st,en,n,i=0,key=0,j=0,mid,flag=0,count=0;     printf("No of employee u wish to enter:");     scanf("%d",&n);     printf("Start Entering the data:\n");     for(i=0;i<n;i++)     {         printf("Employee name:");         scanf("%s",e[i].ename);         printf("Employee no:");         scanf("%d",&e[i].eno);         printf("Enter sal of emp:");         scanf("%s",e[i].esal);     }     printf("Your entered data:\n");     printf("Employee name\tEmployee no\tSalary\n");     for(i=0;i<n;i++)     {         printf("%s\t\t",e[i].ename);       ...

Practical 9

 #include <stdio.h> #include <stdlib.h> int *array; int cmp(const void *a, const void *b) {  int ia = *(int *)a;  int ib = *(int *)b;  return array[ia] < array[ib] ? -1 : array[ia] > array[ib]; }    int main() {  int ti, di, n;  scanf("%d", &n);  int data[n];  for (int i = 0; i < n; i++)  {    printf("order.no,processing time%d",i + 1);   scanf("%d %d", &ti, &di);   data[i] = ti + di;  }  int size = sizeof(data) / sizeof(*data);  int index[size];  int i;  for (i = 0; i < size; i++)  {   index[i] = i;  }  array = data;  qsort(index, size, sizeof(*index), cmp);  for(int i=0;i < n - 1;i++){      for(int j=0;j<n - i - 1;j++){          if(data[j]>data[j + 1]){            int temp=data[j];                 data[...

Practical 8 - NEW

 #include <stdio.h> #include <string.h> typedef struct Employee {  char Employee_name[20];  int emp_no;  float emp_salary; } Employee; void binarysearch(Employee s[], int num, int n) {  int count = 0;  int first = 0;  int last = n - 1;  int flag = 0, mid;  while (first <= last)  {   mid = (first + last) / 2;   count++;   if (num == s[mid].emp_no)   {    flag = 1;    break;   }   else if (num > s[mid].emp_no)    first = mid + 1;   else    last = mid - 1;  }  if (flag == 1)  {   printf("\nRecord found");   printf("\nName\tEmployee no.\tSalary\n");   printf("-------------------------------");   printf("\n%s\t %d\t\t%0.2f\n", s[mid].Employee_name, s[mid].emp_no, s[mid].emp_salary);   printf("\nTotal comparisons happened = %d\n", count);  }  else   printf("\nRecord not found");  // return fla...

DS practical 8

 DS practical 8 #include <stdio.h> struct Employee {   char employee_name[50];   int emp_no;   int emp_salary; }; int count=0; int binarySearch(int num,struct Employee ar[],int n) {     int mid;     int l=n-1,f=0;     while(f<=l)     {         count++;         mid=(f+l)/2;         if(ar[mid].emp_no==num)         return mid;         else if(ar[mid].emp_no<num)         {             f=mid+1;         }           else if(ar[mid].emp_no>num)         {             l=mid-1;         }     }         return -1; } int main() {     int n,i,number;     printf("Enter number of employees: ");     scanf("%d",...

Ds practical 7

 Ds practical 7 //7th DS #include <stdio.h> struct Student {  char student_name[50];  int student_roll_no;  int total_marks; }; void swap(struct Student* a, struct Student* b) { struct Student t = *a; *a = *b; *b = t; } int count=1; int partition (struct Student arr[], int low, int high) { int pivot = arr[high].student_roll_no; int i = (low - 1); for (int j = low; j <= high - 1; j++) { if (arr[j].student_roll_no < pivot) { i++; swap(&arr[i], &arr[j]); count++; } } swap(&arr[i + 1], &arr[high]); return (i + 1); } void quickSort(struct Student arr[], int low, int high) { if (low < high) { int pi = partition(arr, low, high); quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } void printArray(struct Student arr[], int size) { int i; for (i = 0; i < size; i++) { printf("%s \t%d \t%d\n",arr[i].student_name,arr[i].student_roll_no,arr[i].total_marks); } } int main() {  int n,i;  printf("Enter number of students: "); ...

Prim's algorithm

Prim's algorithm  #include <stdio.h> #include <stdlib.h> #define infinity 9999 #define MAX 20 int G[MAX][MAX], spanning[MAX][MAX], n; int prims(); int main() {     int i, j, total_cost;     printf("Enter no. of vertices:");     scanf("%d", &n);     printf("\nEnter the adjacency matrix:\n");     for (i = 0; i < n; i++)         for (j = 0; j < n; j++)             scanf("%d", &G[i][j]);     total_cost = prims();     printf("\nspanning tree matrix:\n");     for (i = 0; i < n; i++)     {         printf("\n");         for (j = 0; j < n; j++)             printf("%d\t", spanning[i][j]);     }     printf("\n\nTotal cost of spanning tree=%d", total_cost);     return 0; } int prims() {     int cost[MAX][MAX];  ...