0% found this document useful (0 votes)
11 views8 pages

C Program Unit-1-1

The document provides C programs and algorithms for various searching and sorting techniques, including linear search, binary search, bubble sort, insertion sort, selection sort, merge sort, and radix sort. Each section outlines the algorithm steps and includes a corresponding C code implementation. The document serves as a comprehensive guide for understanding and implementing these fundamental algorithms in C programming.

Uploaded by

asdflkj.amrutha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views8 pages

C Program Unit-1-1

The document provides C programs and algorithms for various searching and sorting techniques, including linear search, binary search, bubble sort, insertion sort, selection sort, merge sort, and radix sort. Each section outlines the algorithm steps and includes a corresponding C code implementation. The document serves as a comprehensive guide for understanding and implementing these fundamental algorithms in C programming.

Uploaded by

asdflkj.amrutha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

//C Program to Implement Linear Search

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;
}

// C Program to implement Bubble Sort


Algorithm

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 3 − Repeat Step 2 until we reach the end of 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.

Step 5 − The final output achieved is the sorted array.

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;
}
}
}
}

// C Program to implement Insertion Sort


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

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

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;
}

// C Program to implement Selection Sort


Algorithm
Step 1 − Set MIN to location 0
Step 2 − Search the minimum element in the list
Step 3 − Swap with value at location MIN
Step 4 − Increment MIN to point to next element
Step 5 − Repeat until list is sorted
Program
#include <stdio.h>
int main()
{
int arr[10]={6,12,0,18,11,99,55,45,34,2};
int n=10;
int i, j, position, swap;
for (i = 0; i < (n - 1); i++)
{
position = i;
for (j = i + 1; j < n; j++)
{
if (arr[position] > arr[j])
position = j;
}
if (position != i)
{
swap = arr[i];
arr[i] = arr[position];
arr[position] = swap;
}
}
for (i = 0; i < n; i++)
printf("%d\t", arr[i]);
return 0;
}

// C Program to implement Merge Sort


Algorithm

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]);
}

void merging(int low, int mid, int high)


{
int l1, l2, i;
for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++)
{
if(a[l1] <= a[l2])
b[i] = a[l1++];
else
b[i] = a[l2++];
}

while(l1 <= mid)


b[i++] = a[l1++];

while(l2 <= high)


b[i++] = a[l2++];
for(i = low; i <= high; i++)
a[i] = b[i];
}

void sort(int low, int high)


{
int mid;
if(low < high)
{
mid = (low + high) / 2;
sort(low, mid);
sort(mid+1, high);
merging(low, mid, high);
}
else
{
return;
}
}
// C Program to implement Radix Sort
Algorithm

Program

You might also like