Ds File Part 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

PRACTICAL-9

Aim: Write a Program to implement Linear Search

Algorithm:
Linear Search (Array A, Value x)
Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit

Code:
#include<stdio.h>

int main()
{
int arr[20], size, key, i, index;
printf("Number of elements in the list: ");
scanf("%d", &size);
printf("Enter elements of the list: ");
for (i = 0; i < size; i++)
scanf("%d", &arr[i]);
printf("Enter the element to search ie. key element: ");
scanf("%d", &key);
for (index = 0; index < size; index++)
if (arr[index] == key)
break;
if (index < size)
printf("Key element found at index %d", index);
else
printf("Key element not found");
return 0;
}

Output:
PRACTICAL-10

Aim: Write a Program to implement Binary Search

Algorithm:

Step 1 - Read the search element from the user.

Step 2 - Find the middle element in the sorted list.

Step 3 - Compare the search element with the middle element in the sorted list.

Step 4 - If both are matched, then display "Given element is found!!!" and terminate
the function.

Step 5 - If both are not matched, then check whether the search element is smaller or
larger than the middle element.

Step 6 - If the search element is smaller than middle element, repeat steps 2, 3, 4 and
5 for the left sublist of the middle element.

Step 7 - If the search element is larger than middle element, repeat steps 2, 3, 4 and 5
for the right sublist of the middle element.

Step 8 - Repeat the same process until we find the search element in the list or until
sublist contains only one element.

Step 9 - If that element also doesn't match with the search element, then display
"Element is not found in the list!!!" and terminate the function.

Code:
#include<stdio.h>
#include<conio.h>

void main()
{
int first, last, middle, size, i, sElement, list[100];
clrscr();
printf("Enter the size of the list: ");
scanf("%d",&size);
printf("Enter %d integer values in Assending order\n", size);

for (i = 0; i < size; i++)


scanf("%d",&list[i]);
printf("Enter value to be search: ");
scanf("%d", &sElement);
first = 0;
last = size - 1;
middle = (first+last)/2;
while (first <= last) {
if (list[middle] < sElement)
first = middle + 1;
else if (list[middle] == sElement) {
printf("Element found at index %d.\n",middle);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Element Not found in the list.");
getch();
}

Output:
PRACTICAL-11

Aim: Write a Program to implement Bubble Sorting

Algorithm:

Step 1- begin BubbleSort(arr)  
Step 2-    for all array elements  
Step 3-     if arr[i] > arr[i+1]  
Step 4-    swap(arr[i], arr[i+1])  
Step 5-    end if  
Step 6-    end for     
Step 7-    return arr     
Step 8- end BubbleSort

Code:
#include <stdio.h>

void bubble_sort(int A[], int n)


{
int i, j, temp;
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(A[j]>A[j+1])
{
temp=A[j];
A[j]=A[j+1];
A[j+1]=temp;
}
}
}
printf(“The sorted elements are:\n”);
for(i=0;i<n;i++)
printf("%d\t",A[i]);
}
int main()
{
int A[20];
int n,i;
printf("Enter the number of elements:\n");
scanf("%d",&n);
printf("Enter the elements of array:\n");
for(i=0;i<n;i++)
scanf("%d",&A[i]);
bubble_sort(A, n);
}

Output:
PRACTICAL-12

Aim: Write a Program to implement Selection Sorting

Algorithm:

Step 1: Repeat Steps 2 and 3 for i = 0 to n-1  
Step 2: CALL SMALLEST(arr, i, n, pos)  
Step 3: SWAP arr[i] with arr[pos]  
[END OF LOOP]  
Step 4: EXIT  
  
SMALLEST (arr, i, n, pos)  
Step 1: [INITIALIZE] SET SMALL = arr[i]  
Step 2: [INITIALIZE] SET pos = i  
Step 3: Repeat for j = i+1 to n  
if (SMALL > arr[j])  
      SET SMALL = arr[j]  
SET pos = j  
[END OF if]  
[END OF LOOP]  
Step 4: RETURN pos

Code:
#include <stdio.h>

void Selection_Sort(int A[],int n)


{
int i,j,min,temp,temp1;
for(i=0;i<n-1;i++)
{
min=A[i];
temp1=i;
for(j=i+1;j<n;j++)
{
if(min>A[j])
{
min=A[j];
temp1=j;
}
temp=A[temp1];
A[temp1]=A[i];
A[i]=temp;
}
printf("The sorted elements are:\n");
for(i=0;i<n;i++)
printf("%d\t",A[i]);
}

int main()
{
int A[20];
int size, i;
printf ("Enter the size of array:\n");
scanf("%d",&size);
printf("Enter the elements of an array:\n");
for(int i=0;i<size;i++)
scanf("%d",&A[i]);
Selection_Sort(A, size);
return 0;
}

Output:
PRACTICAL-13

Aim: Write a Program to implement Insertion Sorting

Algorithm:

Step 1 - If the element is the first element, assume that it is already sorted. Return 1.

Step 2 - Pick the next element, and store it separately in a key.

Step 3 - Now, compare the key with all elements in the sorted array.

Step 4 - If the element in the sorted array is smaller than the current element, then move
to the next element. Else, shift greater elements in the array towards the right.

Step 5 - Insert the value.

Step 6 - Repeat until the array is sorted.

Code:
#include <stdio.h>

void Insertion_Sort(int A[], int n)


{
int i, j, key;
for(i=1;i<n;i++)
{
key=A[i];
j=i-1;
while(j>=0&&key<A[j])
{
A[j+1]=A[j];
j=j-1;
}
A[j+1]=key;
}
}
int main() {
int A[20];
int size, i;
printf ("Enter the size of array:\n");
scanf("%d",&size);
printf("Enter the elements of an array:\n");
for(int i=0;i<size;i++)
scanf("%d",&A[i]);
Insertion_Sort(A, size);
printf("The sorted elements are:\n");
for(i=0;i<size;i++)
printf("%d\t",A[i]);
return 0;
}

Output:

You might also like