Posts

Showing posts from December, 2023

INSERTION SORT

  INSERTION SORT :- ALGORITHM:= STEP1. SET A[0]:= INFINITY STEP2. REPEAT STEP 3 TO 5 FOR K =2,3,...N STEP3. SET TEMP:= A[K] AND PTR:=K-1 STEP 4. REPEAT WHILE TEMP<A[PTR]                A) SET A[PTR+1]:=A[PTR]                B) set PTR:=PTR-1 STEP5. SET A[PTR+1];=TEMP STEP6. RETURN CODE:- #include <stdio.h> #include <conio.h> void insertionSort ( int arr [], int size ) {     int i, j, key;     for (i = 1 ; i < size; i ++ )     {         key = arr [i];         j = i - 1 ;         while (j >= 0 && arr [j] > key) {             arr [j + 1 ] = arr [j];             j = j - 1 ;         }         arr [j + 1 ] = key;     } } void main () {     int a [ 5...

MERGING AND SORTING

  MERGING AND SORTING:- CODE:- #include<stdio.h> #include<conio.h> int main() { int i, j, n1, n2, k, a1[20], a2[20], m[20]; clrscr(); printf("enter size of first array : "); scanf("%d", &n1); printf("\n"); for (i=0; i<n1; i++) {     printf("enter array element :");     scanf( "%d", &a1[i] ); } printf("\n"); printf("enter size of second array :"); scanf( "%d", &n2 ); printf("\n"); for(j=0; j<n2; j++) {     printf("enter array element :");     scanf( "%d", &a2[j] ); } printf("\n\n The first array :"); for(i=0; i<n1; i++) {     printf("%d  ", &a1[i]); } printf("\n\n The Second array :"); for(j=0; j<n2; j++) {     printf("%d  ", &a2[j]); } k=0; i=0; j=0; while(i<n1 && j<n2) {     if(a1[i]<a2[j])     {     m[k++] = a1[i++];     }     else     {     m[k++] = a2[j++];     } } w...

BINARY SORT

  BINARY SEARCH:- ALGORITHM:- STEP 1. SET BEG:=LB,END:=UB AND MID =INT(BEG+END)/2) STEP 2. REPEAT STEP 3 & 4 WHILE BEG<=END AND DATA[MID] is not equal to ITEM STEP 3. IF ITEM <DATA[MID],THEN               SET END:=MID-1               ELSE              SET BEG:=MID+1 STEP 4. SET MID:= INT(BEG+END)/2 STEP 5. IF DATA [MID] =ITEM, THEN               SET LOC;=MID             ELSE             SET LOC:=NULL STEP 6. EXIT CODE:-

MERGE SORT

  MERGE SORT:- ALGORITHM:- STEP 1. SET L:=1  STEP 2. REPEAT STEP 3 TO 6 WHILE L<N; STEP 3. CALL MERGEPASS (A,N,L,B) STEP 4. CALL MERGEPASS(B,N,2*L,A) STEP 5. SET L:=4*L STEP 6. EXIT CODE:- #include<stdio.h> #include<conio.h> int main() {   int i,j,n1,n2,a1[20],a2[20],m[20];   clrscr();   printf("enter size of first array : ");   scanf("%d",&n1);    printf("\n");     for(i=0;i<n1;i++) { printf("enter array element : "); scanf("%d",&a1[i]); }  printf("\n");     printf("enter size of second array : ");     scanf("%d",&n2);     printf("\n");      for(i=0;i<n2;i++) { printf("enter array element : "); scanf("%d",&a2[i]); }           printf("\n\n the first array : ");           for(i=0;i<n1;i++) { printf("%d  ",a1[i]); }          printf("\n\n the second ar...

SELECTION SORT

  SELECTION SORT ALGORITHM:- STEP 1. REPEAT STEP 2 & 3 FOR K=1,2...N-1 STEP 2. MIN(A,K,N,LOC) STEP 3. SET TEMP:=A[K],A[K]:=A[LOC]  AND   A[LOC];=TEMP STEP 4. EXIT CODE:- #include<stdio.h> #include<conio.h> int main() {     int i,j,n,arr[20],min=0,temp=0;     clrscr();     printf("Enter the size of array : ");     scanf("%d",&n);     printf("\n");     for(i=0; i<n; i++)     { printf("enter array element : "); scanf("%d",&arr[i]);     }      printf("\nThe Unsorted array you entered : \n");     for(i=0; i<n; i++)     { printf("%d   ",arr[i]);     }     printf("\n");     for(i=0;i<n-1;i++)     { min=i; for(j=i+1;j<n;j++) {     if(arr[j]<arr[min])     { min=j;     } }     temp=arr[i]; arr[i]=arr[min]; arr[...

BUBBLE SORT

  BUBBLE SORT:- ALGORITHM:- STEP 1. REPEAT STEP 2 & 3 FOR K=1 TO N-1 STEP 2. SET PTR:= 1 STEP 3. REPEAT WHILE PTR <= N-K                A) IF DATA [PTR]>DATA[PTR+1] THEN INTERCHANGE DATA[PTR] & DATA[PTR+1]                B) SET PTR:= PTR+1 STEP 4. EXIT CODE:- #include <stdio.h> #include <conio.h> int main () {     int i,j,n, arr [ 20 ],temp;     int flag;     clrscr ();     printf ( "Enter the size of array : " );     scanf ( " %d " , & n);     printf ( " \n " );     for (i = 0 ; i < n; i ++ )     {     printf ( "enter array element : " );     scanf ( " %d " , & arr [i]);     }     printf ( " \n Unsorted Array : " );     for (i = 0 ; i < n; i ++ )     {     printf ( " %d   " , arr [i]);     } ...

DELETION OF ARRAY

  INSERTION SORT :- ALGORITHM:= STEP1. SET A[0]:= INFINITY STEP2. REPEAT STEP 3 TO 5 FOR K =2,3,...N STEP3. SET TEMP:= A[K] AND PTR:=K-1 STEP 4. REPEAT WHILE TEMP<A[PTR]                A) SET A[PTR+1]:=A[PTR]                B) set PTR:=PTR-1 STEP5. SET A[PTR+1];=TEMP STEP6. RETURN CODE:- #include <stdio.h> #include <conio.h> void insertionSort ( int arr [], int size ) {     int i, j, key;     for (i = 1 ; i < size; i ++ )     {         key = arr [i];         j = i - 1 ;         while (j >= 0 && arr [j] > key) {             arr [j + 1 ] = arr [j];             j = j - 1 ;         }         arr [j + 1 ] = key;     } } void main () {     int a [ 5...

TRAVERSING ARRAY

  TRAVERSING ARRAY:- ALGORITHM:- STEP1. SET K=LB STEP2. REPEAT STEP 3 & 4 WHILE K<=UB STEP3. VISIT ELEMENT LA[K]              SET K :=K|+1 STEP4. EXIT CODE:-. #include<stdio.h> #include<conio.h> void main() {     int a[5], i, size = 5;     clrscr();     printf("Enter array elements:\n");     for (i = 0; i < size; i++) {         scanf("%d", &a[i]);     }     printf("Array elements during traversal:\n");     for (i = 0; i < size; i++) {         printf("%d ", a[i]);     }     printf("\n");     getch(); }

INSERTION IN ARRAY

  INSERTION IN ARRAY:- ALGORITHM:-       STEP 1. set j=n  STEP2. repeat step 3 and 4 while j>=k  STEP3. set LA[j+1];=LA[J]  STEP4. SET J:= J-1  STEP5. SET LA[K]:=ITEM  STEP6.  SET N:=N+1  STEP7. EXIT CODE :-  #include<stdio.h> #include<conio.h> void main() {     int a[5], i = 0, j, position, value, size = 5;     clrscr();      printf("Enter array elements:\n");          for (j = 0; j < 5; j++) {         scanf("%d", &a[j]);     }          printf("Enter the position to insert (0-based index): ");     scanf("%d", &position);          if (position < 0 || position > size) {         printf("Invalid position. Insertion failed.\n");     } else {         printf("Enter the value to insert: ");   ...

LINEAR SEARCH

  LINEAR SEARCH:- ALGORITHM:-       STEP 1.  Begin STEP 2. set a[5] {10,20,30,40,50} STEP3. set i=0 STEP4. input searching item STEP5. repeat step 6 & 7 while i<5 STEP6. if a [i]=item then print item found and location=i & exit STEP 7. i=i+1 STEP8. if i>5  then print item not found STEP 9. EXIT CODE :-          #include<stdio.h> #include<conio.h> void main() {     int a[5], i = 0, j, item;     clrscr();     printf("Enter array elements:\n");     for (j = 0; j < 5; j++) {         scanf("%d", &a[j]);     }   printf("Enter item to search:\n");     scanf("%d", &item);      while (i < 5) {         if (a[i] == item) {             printf("Item found at location: %d\n", i);             break; // Added to exit the l...

DAA PRACTICAL

  LINEAR SEARCH:- ALGORITHM:-       STEP 1.  Begin STEP 2. set a[5] {10,20,30,40,50} STEP3. set i=0 STEP4. input searching item STEP5. repeat step 6 & 7 while i<5 STEP6. if a [i]=item then print item found and location=i & exit STEP 7. i=i+1 STEP8. if i>5  then print item not found STEP 9. EXIT CODE :-          #include<stdio.h> #include<conio.h> void main() {     int a[5], i = 0, j, item;     clrscr();     printf("Enter array elements:\n");     for (j = 0; j < 5; j++) {         scanf("%d", &a[j]);     }   printf("Enter item to search:\n");     scanf("%d", &item);      while (i < 5) {         if (a[i] == item) {             printf("Item found at location: %d\n", i);             break; // Added to exit the l...