Ds File Part 2
Ds File Part 2
Ds File Part 2
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
Algorithm:
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);
Output:
PRACTICAL-11
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>
Output:
PRACTICAL-12
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>
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
Algorithm:
Step 1 - If the element is the first element, assume that it is already sorted. Return 1.
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.
Code:
#include <stdio.h>
Output: