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

Implementation Code8

The document contains C++ code that defines various sorting and searching algorithms including linear search, binary search, insertion sort, selection sort, bubble sort, radix sort, merge sort, heap sort, and a main function that allows a user to test these algorithms on an input array and view the output. The code takes in user input of array elements, performs the requested algorithm (search or sort) on the array, and displays the output.

Uploaded by

Tech Gyan
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)
15 views

Implementation Code8

The document contains C++ code that defines various sorting and searching algorithms including linear search, binary search, insertion sort, selection sort, bubble sort, radix sort, merge sort, heap sort, and a main function that allows a user to test these algorithms on an input array and view the output. The code takes in user input of array elements, performs the requested algorithm (search or sort) on the array, and displays the output.

Uploaded by

Tech Gyan
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/ 7

CODE

#include <iostream>
using namespace std;
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
int sunil_lsearch(int data[], int size, int target)
{
for (int i = 0; i < size; i++)
{
if (data[i] == target)
{
return i;
}
}
return -1;
}
int sunil_bsearch(int data[], int size, int target)
{
int left = 0;
int right = size - 1;
while (left <= right)
{
int mid = left + (right - left) / 2;
if (data[mid] == target)
{
return mid;
}
else if (data[mid] < target)
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
return -1;
}
void sunil_insertionsort(int data[], int size)
{
for (int i = 1; i < size; i++)
{
int k = data[i];
int j = i - 1;
while (j >= 0 && data[j] > k)
{
data[j + 1] = data[j];
j--;
}
data[j + 1] = k;
}
}
void sunil_selectionsort(int data[], int size)
{
for (int i = 0; i < size - 1; i++)
{
int min_index = i;
for (int j = i + 1; j < size; j++)
{
if (data[j] < data[min_index])
{
min_index = j;
}
}
swap(data[i], data[min_index]);
}
}
void sunil_bubblesort(int data[], int size)
{
for (int i = 0; i < size - 1; i++)
{
for (int j = 0; j < size - i - 1; j++)
{
if (data[j] > data[j + 1])
{
swap(data[j], data[j + 1]);
}
}
}
}
void sunil_radixSort(int data[], int size)
{
int max = data[0];
for (int i = 1; i < size; i++)
{
if (data[i] > max)
max = data[i];
}
int div = 1;
while (max / div > 0)
{
int Array1[size];
int count[10] = {0};
for (int i = 0; i < size; i++)
count[(data[i] / div) % 10]++;
for (int i = 1; i < 10; i++)
count[i] += count[i - 1];
for (int i = size - 1; i >= 0; i--)
{
Array1[count[(data[i] / div) % 10] - 1] = data[i];
count[(data[i] / div) % 10]--;
}
for (int i = 0; i < size; i++)
data[i] = Array1[i];
div *= 10;
}
}
void sunil_merge(int data[], 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] = data[left + i];
for (int i = 0; i < n2; i++)
R[i] = data[mid + 1 + i];
int i = 0, j = 0, k = left;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
data[k] = L[i];
i++;
}
else
{
data[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
data[k] = L[i];
i++;
k++;
}

while (j < n2)


{
data[k] = R[j];
j++;
k++;
}
}

void sunil_mergeSort(int data[], int left, int right)


{
if (left < right)
{

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

sunil_mergeSort(data, left, mid);


sunil_mergeSort(data, mid + 1, right);

sunil_merge(data, left, mid, right);


}
}

void sunil_heapify(int data[], int size, int i)


{
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;

if (left < size && data[left] > data[largest])


largest = left;
if (right < size && data[right] > data[largest])
largest = right;

if (largest != i)
{
int temp = data[i];
data[i] = data[largest];
data[largest] = temp;
sunil_heapify(data, size, largest);
}
}

void sunil_heapSort(int data[], int size)


{
for (int i = size / 2 - 1; i >= 0; i--)
sunil_heapify(data, size, i);

for (int i = size - 1; i >= 0; i--)


{
int temp = data[0];
data[0] = data[i];
data[i] = temp;

sunil_heapify(data, i, 0);
}
}

void result(int i)
{
if (i == -1)
{
cout << "Element not found" << endl;
}
else
{
cout << "Element found at index: " << i << endl;
}
}

void display(int data[], int n)

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

int main()
{
int n, a, target, i;
cout << "Enter the number of elements you want in the array: ";
cin >> n;

int data[n];
cout << "Enter the values in the array: ";
for (int i = 0; i < n; i++)
{

cin >> data[i];


}
cout << endl;

while (1)
{
cout << "+++++++++++++++++++++++++++++";
cout << "\n MENU \n";
cout << "1. LINEAR SEARCH " << endl;
cout << "2. BINARY SEARCH " << endl;
cout << "3. INSERTION SORT " << endl;
cout << "4. SELECTION SORT " << endl;
cout << "5. BUBBLE SORT " << endl;
cout << "6. RADIX SORT " << endl;
cout << "7. HEAPSORT " << endl;
cout << "8. MERGE SORT " << endl;
cout << "9. DISPLAY " << endl;
cout << "10. EXIT " << endl;
cout << "\n+++++++++++++++++++++++++++++\n";
cout << "enter the choice : ";
cin >> a;
switch (a)
{
case 1:
cout << "\n+++++++++++++++++++++++++++++\n";
cout << "Enter the target for search: ";
cin >> target;
i = sunil_lsearch(data, n, target);
result(i);
cout << "\n+++++++++++++++++++++++++++++\n";
break;
case 2:
cout << "\n+++++++++++++++++++++++++++++\n";
cout << "Enter the target for search: ";
cin >> target;
i = sunil_bsearch(data, n, target);
result(i);
cout << "\n+++++++++++++++++++++++++++++\n";
break;
case 3:

cout << "\n+++++++++++++++++++++++++++++\n";


sunil_insertionsort(data, n);
cout << "after sorting: ";
display(data, n);
cout << "\n+++++++++++++++++++++++++++++\n";
break;

case 4:
cout << "\n+++++++++++++++++++++++++++++\n";
sunil_selectionsort(data, n);
cout << "after sorting: ";
display(data, n);
cout << "\n+++++++++++++++++++++++++++++\n";

break;
case 5:
cout << "\n+++++++++++++++++++++++++++++\n";

sunil_bubblesort(data, n);

cout << "after sorting: ";


display(data, n);
cout << "\n+++++++++++++++++++++++++++++\n";
break;
case 6:
cout << "\n+++++++++++++++++++++++++++++\n";
sunil_radixSort(data, n);
cout << "after sorting: ";
display(data, n);
cout << "\n+++++++++++++++++++++++++++++\n";
break;
case 7:
cout << "\n+++++++++++++++++++++++++++++\n";
sunil_heapSort(data, n);
cout << "after sorting: ";
display(data, n);
cout << "\n+++++++++++++++++++++++++++++\n";
break;
case 8:
cout << "\n+++++++++++++++++++++++++++++\n";
sunil_mergeSort(data, 0, n - 1);
cout << "after sorting: ";
display(data, n);
cout << "\n+++++++++++++++++++++++++++++\n";
break;
case 9:
cout << "\n+++++++++++++++++++++++++++++\n";
display(data, n);
cout << "\n+++++++++++++++++++++++++++++\n";
break;
case 10:
exit(1);
}
}
return 0;
}
OUTPUT

Enter the number of elements you want in the array: 5 8. MERGE SORT
Enter the values in the array: 1 9. DISPLAY
6 10. EXIT
3
2 +++++++++++++++++++++++++++++
4 enter the choice : 1

+++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++
MENU Enter the target for search: 4
1. LINEAR SEARCH Element found at index: 3
2. BINARY SEARCH
3. INSERTION SORT +++++++++++++++++++++++++++++
4. SELECTION SORT +++++++++++++++++++++++++++++
5. BUBBLE SORT MENU
6. RADIX SORT 1. LINEAR SEARCH
7. HEAPSORT 2. BINARY SEARCH
8. MERGE SORT 3. INSERTION SORT
9. DISPLAY 4. SELECTION SORT
10. EXIT 5. BUBBLE SORT
6. RADIX SORT
+++++++++++++++++++++++++++++ 7. HEAPSORT
enter the choice : 9 8. MERGE SORT
9. DISPLAY
+++++++++++++++++++++++++++++ 10. EXIT
1 6 3 2 4
+++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++ enter the choice : 10
+++++++++++++++++++++++++++++
MENU
1. LINEAR SEARCH
2. BINARY SEARCH
3. INSERTION SORT
4. SELECTION SORT
5. BUBBLE SORT
6. RADIX SORT
7. HEAPSORT
8. MERGE SORT
9. DISPLAY
10. EXIT

+++++++++++++++++++++++++++++
enter the choice : 5

+++++++++++++++++++++++++++++
after sorting: 1 2 3 4 6

+++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++
MENU
1. LINEAR SEARCH
2. BINARY SEARCH
3. INSERTION SORT
4. SELECTION SORT
5. BUBBLE SORT
6. RADIX SORT
7. HEAPSORT

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy