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

Rauf Khann C M M File

Uploaded by

Abdul rauf Khan
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 views12 pages

Rauf Khann C M M File

Uploaded by

Abdul rauf Khan
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/ 12

write a program that implement a linear search in c++

#include <iostream>
using namespace std;

// Function to perform linear search


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

int main() {
int arr[] = {10, 25, 30, 40, 50};
int size = sizeof(arr) / sizeof(arr[0]);
int target;
cout << "Enter the number to search: ";
cin >> target;

int result = linearSearch(arr, size, target);

if (result != -1) {
cout << "Element found at index " << result << endl;
} else {
cout << "Element not found in the array." << endl;
}

return 0;
}
write a program that implement a Binary search in
c++

#include <iostream>
using namespace std;

// Function to perform binary search


int binarySearch(int arr[], int left, int right, int target) {
while (left <= right) {
int mid = left + (right - left) / 2;

// Check if target is present at mid


if (arr[mid] == target) {
return mid;
}

// If target is greater, ignore the left half


if (arr[mid] < target) {
left = mid + 1;
}
// If target is smaller, ignore the right half
else {
right = mid - 1;
}
}

// Return -1 if the target is not found


return -1;
}

int main() {
int arr[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
int size = sizeof(arr) / sizeof(arr[0]);
int target;

cout << "Enter the number to search: ";


cin >> target;

int result = binarySearch(arr, 0, size - 1, target);

if (result != -1) {
cout << "Element found at index " << result << endl;
} else {
cout << "Element not found in the array." << endl;
}

return 0;
}

Write a program that implement sorting technique

#include <iostream>
using namespace std;

// Function to perform Bubble Sort


void bubbleSort(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - 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;
}
}
}
}

// Function to print the array


void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}

int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int size = sizeof(arr) / sizeof(arr[0]);
cout << "Unsorted array: ";
printArray(arr, size);

bubbleSort(arr, size);

cout << "Sorted array: ";


printArray(arr, size);

return 0;
}

Write a program that implement tree concept

#include <iostream>
using namespace std;

// Node structure for the binary tree


struct Node {
int data;
Node* left;
Node* right;

Node(int value) {
data = value;
left = right = nullptr;
}
};

// Function to insert a new node in the binary tree


Node* insert(Node* root, int value) {
if (root == nullptr) {
return new Node(value);
}

if (value < root->data) {


root->left = insert(root->left, value);
} else if (value > root->data) {
root->right = insert(root->right, value);
}
return root;
}

// In-order traversal of the binary tree


void inorderTraversal(Node* root) {
if (root == nullptr) return;
inorderTraversal(root->left);
cout << root->data << " ";
inorderTraversal(root->right);
}

// Pre-order traversal of the binary tree


void preorderTraversal(Node* root) {
if (root == nullptr) return;
cout << root->data << " ";
preorderTraversal(root->left);
preorderTraversal(root->right);
}

// Post-order traversal of the binary tree


void postorderTraversal(Node* root) {
if (root == nullptr) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
cout << root->data << " ";
}

// Function to print the binary tree in 2D


void print2DUtil(Node* root, int space) {
const int COUNT = 5;
if (root == nullptr) return;
space += COUNT;
print2DUtil(root->right, space);
cout << endl;
for (int i = COUNT; i < space; i++) cout << " ";
cout << root->data << "\n";
print2DUtil(root->left, space);
}

void print2D(Node* root) {


print2DUtil(root, 0);
}
int main() {
Node* root = nullptr;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);

cout << "In-order traversal: ";


inorderTraversal(root);
cout << endl;

cout << "Pre-order traversal: ";


preorderTraversal(root);
cout << endl;

cout << "Post-order traversal: ";


postorderTraversal(root);
cout << endl;
cout << "2D representation of the tree:\n";
print2D(root);

return 0;
}

You might also like