Implementation Code8
Implementation Code8
#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++;
}
if (largest != i)
{
int temp = data[i];
data[i] = data[largest];
data[largest] = temp;
sunil_heapify(data, size, largest);
}
}
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;
}
}
{
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++)
{
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:
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);
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