C Program Unit-1-1
C Program Unit-1-1
Algorithm
To search for the given element using linear search, follow the below approach:
Start traversing from the start of the dataset.
Compare the current element with the key (element to be searched).
If the element is equal to the key, return index.
Else, increment the index and repeat the step 2 and 3.
If we reach the end of the list without finding the element equal to the key, return some value to
represent that the element is not found.
Program
#include <stdio.h>
int main()
{
int arr[] = { 10, 50, 30, 70, 80, 60, 20, 90, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
int key = 30;
int i = linearSearch(arr, n, key);
if (i == -1)
printf("Key Not Found");
else
printf("Key Found at Index: %d", i);
return 0;
}
int linearSearch(int* arr, int n, int key)
{
for (int i = 0; i < n; i++)
{
if (arr[i] == key)
{
return i;
}
}
return -1;
}
// C Program to implement binary search using iteration
Algorithm
Create a function that takes an array, left index, right index, and the key to be searched.
Use a loop to iterate while the subarray has elements i.e. left < right.
Calculate the midpoint mid.
Compare the key with the middle element i.e. arr[mid]
o If the key matches the middle element, return mid.
o If the key is greater, adjust the left index to search the right subarray by
making left = mid + 1.
o If the key is smaller, adjust the right index to search the left subarray by
making right = mid – 1.
If the key is not found, return -1.
Program
#include <stdio.h>
int main()
{
int arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91};
int size = sizeof(arr) / sizeof(arr[0]);
int key = 23;
int result = binarySearch(arr, 0, size - 1, key);
if (result == -1)
{
printf("Element is not present in array");
}
else
{
printf("Element is present at index %d", result);
}
return 0;
}
int binarySearch(int arr[], int left, int right, int key)
{
while (left <= right)
{
int mid = left + (right - left) / 2;
if (arr[mid] == key)
{
return mid;
}
if (arr[mid] < key)
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
return -1;
}
Step 1 − Check if the first element in the input array is greater than the next element in the array.
Step 2 − If it is greater, swap the two elements; otherwise move the pointer forward in the array.
Step 4 − Check if the elements are sorted; if not, repeat the same process (Step 1 to Step 3) from
the last element of the array to the first.
Program
#include <stdio.h>
int main()
{
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
void bubble_sort(int arr[], int n)
{
int i, j;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
Step3 - 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.
Program
#include <stdio.h>
int main(void)
{
int n, i, j, temp;
int arr[64];
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (i = 1; i < n; i++)
{
j = i;
while (j > 0 && arr[j - 1] > arr[j])
{
temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
j--;
}
}
printf("Sorted list in ascending order:\n");
for (i = 0; i < n; i++)
{
printf("%d\n", arr[i]);
}
return 0;
}
Program
#include <stdio.h>
#define max 10
int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };
int b[10];
int main()
{
int i;
printf("List before sorting\n");
for(i = 0; i <= max; i++)
printf("%d ", a[i]);
sort(0, max);
printf("\nList after sorting\n");
for(i = 0; i <= max; i++)
printf("%d ", a[i]);
}
Program