0% found this document useful (0 votes)
2 views

Programs in C Practical

Program c language

Uploaded by

vramoshi72
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Programs in C Practical

Program c language

Uploaded by

vramoshi72
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Linear search program in C

Searching is the process of finding a value in a list of values.The searching algorithms you are to
use in this assignment are linear, sentinel and binary search.

#include <stdio.h>

int main()

int array[10], search, i, n;

printf("Enter number of elements in array\n");

scanf("%d", &n);

printf("Enter %d integer(s)\n", n);

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

scanf("%d", &array[i]);

printf("Enter a number to search\n");

scanf("%d", &search);

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

if (array[i] == search) /* If required element is found */

printf("%d is present at location %d.\n", search, i+1);

break;

if (i == n)

printf("%d isn't present in the array.\n", search);

return 0;

}
Binary Search (Recursive Method)

// Binary Search in C

#include <stdio.h>

int binarySearch(int array[], int x, int low, int high) {

if (high >= low) {

int mid = low + (high - low) / 2;

// If found at mid, then return it

if (x == array[mid])

return mid;

// Search the right half

if (x > array[mid])

return binarySearch(array, x, mid + 1, high);

// Search the left half

return binarySearch(array, x, low, mid - 1);

return -1;

int main(void) {

int array[] = {3, 4, 5, 6, 7, 8, 9};

int n = sizeof(array) / sizeof(array[0]);

int x = 4;

int result = binarySearch(array, x, 0, n - 1);

if (result == -1)

printf("Not found");

else

printf("Element is found at index %d", result);

}
// Bubble sort in C
/* C program to implement bubble sort */
#include <stdio.h>
/*
* Main Function
*/
int main()
{
int n, j, i, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
int array[n];
printf("Enter %d integers\n", n);
for (i= 0; i < n; i++)
{
scanf("%d", &array[i]);
}
for (i = 0 ; i < n - 1; i++)
{
for (j = 0 ; j < n - i- 1; j++)
{
if (array[j] > array[j+1])
{
swap = array[j];
array[j] = array[j+1];
array[j+1] = swap;
}
}
}

printf("Sorted list in ascending order:\n");

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


printf("%d\n", array[i]);
return 0;
}
Selection sort
/*
* C Program to Implement Selection Sort
*/

#include <stdio.h>
void selectionSort(int arr[], int size);
void swap(int *a, int *b);
/*
* Selection sort function
*/
void selectionSort(int arr[], int size)
{
int i, j;
for (i = 0 ; i < size;i++)
{
for (j = i ; j < size; j++)
{
if (arr[i] > arr[j])
swap(&arr[i], &arr[j]);
}
}
}

/* Function to swap two variables */


void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}

/*
* Main Function
*/
int main()
{
int array[10], i, size;
printf("How many numbers you want to sort: ");
scanf("%d", &size);
printf("\nEnter %d numbers\t", size);
printf("\n");
for (i = 0; i < size; i++)
scanf("%d", &array[i]);
selectionSort(array, size);
printf("\nSorted array is ");
for (i = 0; i < size;i++)
printf(" %d ", array[i]);
return 0;
}
/*
* C Program to sort an array in ascending order using
* Insertion Sort using separate function
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int *insertionsort(int[], int);


int main(void)
{
int n;
puts("Enter the value of n : ");
scanf("%d", &n);
int *A = malloc((size_t)n * sizeof(int));
puts("******************************");
puts("\nEnter the elements of the array :");
puts("\n******************************");
puts("\nUnsorted array :\n");
for (int i = 0; i < n; i++)
{
scanf("%d", &A[i]);
}
int *result = insertionsort(A, n);
printf("\n%d", result[0]);
puts("\n******************************");
puts("\nRequired sorted array : ");
for (int j = 0; j < n; j++)
{
printf(" %d", result[j]);
}
}

int *insertionsort(int a[], int m)


{
for (int i = 1; i < m; i++)
{
int temp, j;
temp = a[i];
j = i - 1;
while ((temp < a[j]) && (j >= 0))
{
a[j + 1] = a[j];
j = j - 1;
}
a[j + 1] = temp;
}
return a;
}
/*
* C Program to Perform Merge Sort using Recursion and Functions
*/

#include <stdio.h>
#include <stdlib.h>

// merge function
void Merge(int arr[], int left, int mid, int right)
{
int i, j, k;
int size1 = mid - left + 1;
int size2 = right - mid;

// created temporary array


int Left[size1], Right[size2];

// copying the data from arr to temporary array


for (i = 0; i < size1; i++)
Left[i] = arr[left + i];

for (j = 0; j < size2; j++)


Right[j] = arr[mid + 1 + j];

// merging of the array


i = 0; // intital index of first subarray
j = 0; // inital index of second subarray
k = left; // initial index of parent array
while (i < size1 && j < size2)
{
if (Left[i] <= Right[j])
{
arr[k] = Left[i];
i++;
}
else
{
arr[k] = Right[j];
j++;
}
k++;
}

// copying the elements from Left[], if any


while (i < size1)
{
arr[k] = Left[i];
i++;
k++;
}

// copying the elements from Right[], if any


while (j < size2)
{
arr[k] = Right[j];
j++;
k++;
}
}

//merge sort function


void Merge_Sort(int arr[], int left, int right)
{
if (left < right)
{

int mid = left + (right - left) / 2;

// recursive calling of merge_sort


Merge_Sort(arr, left, mid);
Merge_Sort(arr, mid + 1, right);

Merge(arr, left, mid, right);


}
}

// driver code
int main()
{
int size;
printf("Enter the size: ");
scanf("%d", &size);

int arr[size];
printf("Enter the elements of array: ");
for (int i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}

Merge_Sort(arr, 0, size - 1);

printf("The sorted array is: ");


for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}

You might also like