Binary Search Algorithm
hi friends, here I will share a little about c + + programming material is the material on the Binary Search Algorithm, Binary Search is a binary search, which is a binary search locates the position of the item in the array to be sorted. Binary search algorith works by comparing the value inputted into the middle of the array elements. so the comparison is to determine whether the element is equal to the input, the input is less than or more. When the same elements have been compared with the input input is entered then the search stops and it returns the position of the element. and If the element is not the same as the input in the insert then the comparison is made to determine whether the input is less than or greater than elemen.untuk more details here I will give an example to all your friends, so that all my friends better understand.
BINARY
SEARCH ALGORITHM
|
#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <conio.h>
#include <iomanip>
using namespace std;
int BinarySearch(char
nim[19][10],char semester[19][6], char key1[10],
char key2[6], int low, int
height){
int mid,idx=(-1);
while (low <= height) {
char
append1[16]="";
char
append2[16]="";
mid = (int)(low +
height)/2;
strcat(append1,nim[mid]);
strcat(append1,semester[mid]);
strcat(append2,key1);
strcat(append2,key2);
if
(strcmp(append2,append1)==0){
idx=mid;
return idx;
}
if
(strcmp(append2,append1)>0) {
low = mid + 1;
}
if
(strcmp(append2,append1)<0){
height = mid - 1;
}
}
return idx;
}
int main()
{
char
key1[10]="123456789";
char
key2[6]="12345";
int val;
char
nim[19][10]={"800100001","800100001","800100001","800100001",
"800200002","800200002","800200002","800300003","800300003","800400004","800400004",
"800400004","800500005","800600006","800600006","800700007","800700007","800800008",
"800800008"};
char
semester[19][6]={"2004A","2004B","2005A","2005A","2004A","2005A","2005B",
"2004A","2004B","2004A","2004B","2005A","2005A","2004A","2005A","2004B","2005A","2004A","2004B"};
float
ipk[19]={2.78,3.23,2.9,3.04,2.89,2.47,2.25,3.16,3.34,2.93,2.72,2.54,3.57,3.34,3.17,1.78,1.87,2.45,2.23};
float
sks[19]={20,42,62,80,16,36,58,20,40,16,32,48,20,16,32,12,24,16,36};
cout << "\n
=======================================================";
cout<<setw(4)<<"\n
"<<"|"<<setw(8)<<"SKS |";
cout<<setw(20)<<"NIM
|";
cout<<setw(13)<<"IPK
|";
cout<<setw(13)<<"SEMESTER
|"<<endl;
cout << "
=======================================================\n";
for (int i=0;i<=18;i++)
{
cout<<setw(4)<<i<<"|"<<setw(7)<<sks[i]<<"|";//nim[i];
cout<<setw(19)<<nim[i]<<"|";
cout<<setw(12)<<ipk[i]<<"|";
cout<<setw(12)<<semester[i]<<"|"<<endl;
}
cout << "
=======================================================\n";
cout<<"Masukkan
NIM : "; cin>>key1;
cout<<"Masukkan
semester : "; cin>>key2;
val=BinarySearch(nim,semester,key1,key2,
0, 18);
if (val== (-1))
{ cout<<"\nData
tidak ada"; }
else
{
cout<<"NIM :
"<<nim[val]<<endl;
cout<<"Semester
: "<<semester[val]<<endl;
cout<<"IPK :
"<<ipk[val]<<endl;
cout<<"SKS :
"<<sks[val]<<endl;
}
system("pause");
}
|
EmoticonEmoticon