C++ Binary Sort Search (CBSS)

Hi dev, here is an example of a C++ search program that is done sequentially that we have not had the chance to try, due to device constraints, I have tried it in an online compiler but it seems that the header library is not as complete as if using a desktop editor, for example at that time we were learning to use Boland C++.

Sorted Search Program Example

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
bool ketemu, henti=0;
int i=0,cari,posisi,tamp;
ketemu=0;
int larik[50];
randomize();
for(int i=0;i<50;i++)
larik[i]=random(50);
for(int i=0;i<50-1;i++)
for(int j=i+1;j<50;j++)
if(larik[i]>larik[j])
{
tamp=larik[i];
larik[i]=larik[j];
larik[j]=tamp;
}
for(int i=0;i<50;i++)
cout<<larik[i]<<" ";
cout<<"\nData yg dicari : ";cin>>cari;
while(!ketemu && !henti)
{
if (cari==larik[i])
{
ketemu=1;
posisi=i;
}
if(cari<larik[i])
henti=1;
cout<<cari<<" "<<larik[i]<<" "<<henti<<endl;
cout<<"perulangan : "<<i<<endl;getch();
i++;
}
if(ketemu==1)
cout<<"Data ada, posisi : "<<posisi;
else
cout<<"Data tidak ada...";
getch();
}

Binary Search Program Example

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
bool ketemu;
int cari,posisi,tamp;
int larik[10];
randomize();
for(int i=0;i<10;i++)
larik[i]=random(10);
for(int i=0;i<10-1;i++)
for(int j=i+1;j<10;j++)
if(larik[i]>larik[j])
{
tamp=larik[i];
larik[i]=larik[j];
larik[j]=tamp;
}
for(int i=0;i<10;i++)
cout<<larik[i]<<" ";
cout<<"\nData yg dicari : ";cin>>cari;
int atas, tengah, bawah;
ketemu=0; bawah=0; atas=10;
while(bawah<atas)
{
tengah=(atas+bawah)/2;
if(cari < larik[tengah])
atas=tengah-1;
else if (cari > larik[tengah])
bawah=tengah+1;
else if (cari == larik[tengah])
{
ketemu=1;
posisi=tengah;
bawah=atas+1;
}
}
if(ketemu==1)
cout<<"Data ada, posisi : "<<posisi;
else
cout<<"Data tidak ada...";
getch();
}

Hi dev, on the other hand I also want to share some of the most memorable tasks at that time.

The Most Memorable C++ Assignment Ever

Instruction:

Make 3 groups in one class, moderator, operator, presenter, and those who answer questions can take turns. Each group makes a task for presentation based on the questions below. Make a printed report to be collected and a power point for presentation

The report structure is:

  • a. Problems
  • b. Discussion (explain the functions formed and the function operations)
  • c. Complete program and capture running.

1]. Create a program using a linked list to enter data with the menu:

  • a. Add
  • b. Edit
  • c. Delete
  • d. Search
  • e. Done

Your Choice [a..e]
In the structure there are unique fields (eg student number, employee number, etc.) that are used for identification. Data storage is sorted according to its unique field. The process of adding and deleting will place the data according to its order.

2]. Create a program using a double linked list to enter data with the menu:

  • a. Add
  • b. Edit
  • c. Delete
  • d. Search
  • e. Done

Your Choice [a..e]
In the structure there are unique fields (eg student number, employee number, etc.) that are used for identification. Data storage is sorted according to its unique field. The process of adding and deleting will place the data according to its order.

3]. Create a program using a stack to enter data with the menu:

  • a. Add
  • b. Edit
  • c. Delete
  • d. Search
  • e. Done

Your Choice [a..e]
In the structure there are unique fields (eg student number, employee number, etc.) used for identification. Data storage is sorted according to its unique field. The process of adding/deleting using push and pop only.

Netizens

Thanks for sharing the case for Trial, I will try it in a programming language other than c#. Maybe in PHP or React  😀 - You can see other case examples that I solved on my blog, for example Program to remove Prefix using php... READY 😁


Post a Comment

Previous Next

نموذج الاتصال