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

#include stdio.h practical

The document contains C code for sorting an unsorted array using bubble sort and for performing a linear search on an array. It includes functions to sort and print the array, as well as to search for a specific element and return its index. The main function demonstrates both functionalities with example arrays.

Uploaded by

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

#include stdio.h practical

The document contains C code for sorting an unsorted array using bubble sort and for performing a linear search on an array. It includes functions to sort and print the array, as well as to search for a specific element and return its index. The main function demonstrates both functionalities with example arrays.

Uploaded by

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

#include <stdio.

h>
#include <stdlib.h>

// sorting unsorted array through bubble sort


void sort_unsorted_array(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]) {
// Swap arr[j] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}}}}

// printing array
void print_array(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
int unsorted_arr[] = {2, 6, 9, 1, 3, 6};
int n = sizeof(unsorted_arr)/sizeof(unsorted_arr[0]);

printf("Unsorted array: \n");


print_array(unsorted_arr, n);

// Sort the unsorted array


sort_unsorted_array(unsorted_arr, n);

// Print the sorted array


printf("Sorted array: \n");
print_array(unsorted_arr, n);

return 0;
}

// Online C compiler to run C program online


#include <stdio.h>

int linear_search(int arr[], int n, int target) {


for (int i = 0; i < n; i++) {
if (arr[i] == target) {
return i; // Return the index if the element is found
}
}
return -1; // Return -1 if the element is not found
}

int main() {
int arr[] = {10, 25, 30, 45, 50};
int n = sizeof(arr) / sizeof(arr[0]);
int target= 10;

int result = linear_search(arr, n, target);

if (result != -1) {
printf("Element %d found at index %d.\n", target, result);
} else {
printf("Element %d not found in the array.\n", target);
}

return 0;
}

You might also like