[go: up one dir, main page]

0% found this document useful (0 votes)
23 views13 pages

Algo

The document contains a series of programs written in C++ that demonstrate various algorithms including Linear Search, Binary Search, Bubble Sort, Insertion Sort, Merge Sort, and Quick Sort. Each program includes code snippets along with prompts for user input and outputs the results of the operations performed. The document is structured with an index and detailed implementations for each algorithm.

Uploaded by

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

Algo

The document contains a series of programs written in C++ that demonstrate various algorithms including Linear Search, Binary Search, Bubble Sort, Insertion Sort, Merge Sort, and Quick Sort. Each program includes code snippets along with prompts for user input and outputs the results of the operations performed. The document is structured with an index and detailed implementations for each algorithm.

Uploaded by

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

INDEX

S.No Name of Program Date of Date of Sign


. Execution Submission

1. Write a program to perform


Linear Search.

2. Write a program to perform


Binary Search.

3. Write a program to implement


Bubble Sort.

4. Write a program to implement


Insertion Sort.

5. Write a program to implement


Merge Sort.

6. Write a program to implement


Quick Sort.
Program 01: Write a program to perform Linear Search.
Code:
#include <iostream>
using namespace std;
int main()
{
cout << "Prem Saini\n";
int arr[100], size;
int item, loc;
cout << "Enter the size of the array: ";
cin >> size;
cout << "Enter the elements of the array: " << endl;
for (int i = 0; i < size; i++)
{
cin >> arr[i];
}
cout << "Enter item to search: " << endl;
cin >> item;
for (loc = 0; loc <= size; loc++)
{
if (arr[loc] == item)
{
cout << "element found at position:" << loc + 1 << endl;
return 0;
}
}
cout << "element not found!";
return 0;
}
Output:
Program 02: Write a program to perform Binary Search.
Code:
#include <iostream>
using namespace std;

int main()
{
int size, n;
int arr[100];
cout << "Prem Saini\n";
cout << "Enter the size of the array: ";
cin >> size;
cout << "Enter the elements of the sorted array: " << endl;
for (int i = 0; i < size; i++)
{
cin >> arr[i];
}
cout << "Enter the element to search: ";
cin >> n;
int beg = 0, end = size - 1, mid;
while (beg <= end)
{
mid = (beg + end) / 2;
if (arr[mid] == n)
{
cout << "Element found at position: " << mid + 1 << endl;
return 0;
}
if (arr[mid] < n) {
beg = mid + 1;
}
else {
end = mid - 1;
}
}
cout << "Element not found!" << endl;
return 0;
}
Output:
Program 03: Write a program to perform Bubble Sort.
Code:
#include <iostream>
using namespace std;

void bubbleSort(int arr[], int n)


{ for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
swap(arr[j], arr[j + 1]);
}
}
}
}

void printArray(int arr[], int n)


{
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}

int main()
{
int n;
cout << "Prem Saini\n";

cout << "Enter the size of the array: ";


cin >> n;

int arr[n];

cout << "Enter " << n << " elements: ";


for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
cout << "Before sorting: ";
printArray(arr, n);

bubbleSort(arr, n);

cout << "After sorting: ";


printArray(arr, n);

return 0;
}

Output:
Program 04: Write a program to perform Insertion Sort.
Code:
#include <iostream>
using namespace std;

void insert(int a[], int n) {


int i, j, temp;
for (i = 1; i < n; i++) {
temp = a[i];
j = i - 1;

while (j >= 0 && a[j] > temp) {


a[j + 1] = a[j];
j = j - 1;
}
a[j + 1] = temp;
}
}

void printArr(int a[], int n) {


for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << endl;
}

int main() {
int size, a[100];
cout << "Prem Saini\n";

cout << "Enter size of array: ";


cin >> size;

if (size < 1 || size > 100) {


cout << "Invalid size. Please enter a value between 1 and 100." << endl;
return 1;
}

cout << "Enter elements of array: ";


for (int i = 0; i < size; i++) {
cin >> a[i];
}
cout << "Before sorting: " << endl;
printArr(a, size);

insert(a, size);

cout << "After sorting: " << endl;


printArr(a, size);

return 0;
}

Output:
Program 05: Write a program to perform Merge Sort.
Code:
#include <iostream>
using namespace std;

void merge(int arr[], int left, int mid, int right) {


int n1 = mid - left + 1;
int n2 = right - mid;
int L[n1], R[n2];
for (int i = 0; i < n1; i++) {
L[i] = arr[left + i];
}
for (int j = 0; j < n2; j++) {
R[j] = arr[mid + 1 + j];
}
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
void mergeSort(int arr[], int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
cout << "Prem Saini\n";
int n;
cout << "Enter the size of the array: ";
cin >> n;
int arr[n];
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cout << "Before sorting: ";
printArray(arr, n);
mergeSort(arr, 0, n - 1);
cout << "After sorting: ";
printArray(arr, n);

return 0;
}

Output:
Program 06: Write a program to perform Quick Sort.
Code:
#include <iostream>
using namespace std;

int partition(int arr[], int low, int high) {


int pivot = arr[high];
int i = low - 1;

for (int j = low; j < high; j++) {


if (arr[j] < pivot) {
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[high]);
return i + 1;
}

void quickSort(int arr[], int low, int high) {


if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

void printArray(int arr[], int n) {


for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}

int main() {
cout << "Prem Saini\n";
int n;
cout << "Enter the size of the array: ";
cin >> n;

int arr[n];
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

cout << "Before sorting: ";


printArray(arr, n);

quickSort(arr, 0, n - 1);

cout << "After sorting: ";


printArray(arr, n);

return 0;
}

Output:

You might also like