I need to deduce the time that different searches spend.I've been using different numbers,but the time doesn't change at all.
#include <iostream>
#include <iomanip>
#include <string>
#include <time.h>
#include <vector>
#include <fstream>
#include <chrono>
using namespace std;
bool MasCheckOne(int mas[], int k, int q) {
for (int i = 0; i < q; i++) {
std::cout << i << " ";
if (mas[i] == k) {
std::cout << "index: " << i << endl;
return true;
}
}
return false;
}
bool MasCheckTwo(int mas[], int k, int q) {
for (int i = 0; i < q; i++) {
std::cout << i << " ";
if (mas[i] == k) {
std::cout << "index: " << i << endl;;
return true;
}
}
return false;
}
bool MasCheckThree(int mas[], int k, int q) {
int i;
int u;
int l=0;
u = q-1;
int mid = (l + (u - l) / 2);
while(l <= u) {
std::cout << k << " ";
i = (l + u) / 2;
if (k > mas[i] ) {
l = i + 1;
}
else if (k < mas[i]) {
u = i - 1;
}
else if (mas[mid] == k) {
std::cout << "index" << mid << endl;
return true;
}
else if (k == mas[i]) {
std::cout <<"index"<< l << endl;
return true;
}
}
return false;
}
//Mas generation in which outer file with a list of <q> size numbers goes
int *MasGeneration(string file_name,int q) {
std::ifstream ifs(file_name);
int *arr=new int[q];
for (int i = 0; !ifs.eof(); ++i)
{
ifs >> arr[i];
//std::cout << arr[i] << ' ';
}
return arr;
}
int main() {
setlocale(LC_ALL, "RU");
int k; //The number that you search for
int choice;
int mas_choice;
int q; //The number of element
string file_name;
cout << "Mas.txt for array of 3000 elements"<<" " << "Mas1000.txt for array of 1000 elements" <<" " << "Mas500.txt for array of 500 elements" << endl;
std::cin >> file_name;
cout << "Input the size " << endl;
std::cin >> q;
int* mas=MasGeneration(file_name, q);
std::cout << " " << endl;
std::cout << "What search?" << endl;
std::cout << "1.Sequential search" << endl;
std::cout << "2.Fast Sequential search" << endl;
std::cout << "3.Binary search" << endl;
std::cin >> choice;
std::cout << "The number you look for" << endl;
std::cin >> k;
auto begin = std::chrono::steady_clock::now();
int* mas1 = new int[q+1];
//serial search
if(choice==1){
if (MasCheckOne(mas, k, q)) {
std::cout << "Success" << endl;
}
else {
std::cout << "Failure" << endl;
}
}
//fast serial search
if(choice==2){
for (int i = 0; i < q; i++) {
mas1[i] = mas[i];
}
delete[] mas;
mas = mas1;
mas[q] = k;
if (MasCheckTwo(mas, k, q))
std::cout << "Success" << endl;
else
std::cout << "Failure" << endl;
}
//Binary search
if (choice == 3) {
for (int i = 0; i < q; i++) {
for (int j = 0; j < (q-1); j++) {
if (mas[j] > mas[j + 1]) {
int b = mas[j]; // created extra variable
mas[j] = mas[j + 1]; // change their places
mas[j + 1] = b;
}
}
}
if (MasCheckThree(mas, k, q))
std::cout << "Success" << endl;
else
std::cout << "Failure" << endl;
}
auto end = std::chrono::steady_clock::now();
auto elapsed_ms =
std::chrono::duration_cast<std::chrono::milliseconds>(end - begin);
std::cout << fixed << setprecision(10) << "время: " << elapsed_ms.count() << endl;;// к-во знаков после запятой
}
It is believed that Binary search should be faster when used with a large array size, but in my case it is almost 10 times slower than Sequential search (counting for 3000 elements).