Binary Search Sample Code

CK CK 3788 
Created at  
405 0 0

This is Binary search that returns a negative one if the number is not found.
This code does a bubble sort and then proforms the binary search.

#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <time.h>
const c=10;
void inputa(int a[ ]);
void outputa(int a[ ]);
void dosort(int a[ ]);
void swap(int &x, int &y);
void search(int a[ ]);
int dosearch(int a[ ], int b);
int main()
{
	int a[c];
	time_t seconds;
	time(&seconds);
	srand((unsigned int) seconds);
	inputa(a);
	outputa(a);
	cout<<endl;
	dosort(a);
	outputa(a);
	search(a);
	return 0;
}
void inputa(int a[ ])
{
	int i;
	for(i=0; i<c;i++)
	{
		a[i]=rand()%101;
	}
}
void outputa(int a[ ])
{
	int pig;
	for(pig=0; pig<c;pig++)
	{
		cout<<a[pig]<<endl;
	}
}
void dosort(int a[ ])
{
	int j, pig;
	for(pig=0;pig<c;pig++)
	{
		for(j=0;j<c;j++)
		{
			if (a[j]>a[pig])
			{
				swap(a[j],a[pig]);
			}
		}
	}
}
void swap(int &a, int &b)
{
	int temp;
	temp=a;
	a=b;
	b=temp;
}
void search(int a[ ])
{
	int b;
	cout<<"Enter Number to Search for: ";
	cin>>b;
	dosearch(a,b);
	cout<<"The number was found in line number "<<dosearch(a,b)<<endl;
}
int dosearch(int a[ ], int b)
{
	int high=c;
	int mid=c/2;
	int low=-1;
	int d=0;
	int line=0;
	do
	{
		if(b>a[mid])
		{
			low=mid+1;
			mid=(high+low)/2;
		}
		else if (b<a[mid])
		{
			high=mid+1;
			mid=(high+low)/2;
		}
		else if(a[mid]==b)
		{
			return mid+1;
		}
	}while(low<high);
	return -1;
}
Tags Bubble Sort C++ Shell Comand Facebook X
Comments 0
Similar posts
  1. Double Linked List based in C++
    796
  2. How to run shell command by MFC ?
    435
  3. MFC based World Wide Web HTTP Server Source Code
    459
  4. Creating and Using a CAsyncSocket Object to use CAsyncSocket
    472
  5. Fast and Good Keyboard/Mouse Test without the message handler
    982
  6. Simple CGI Programming in C
    545
  7. C++ CGI Example working on Apache
    483
  8. How to search file on certain directory ?
    629
  9. How to call SetTimer function on MFC CDialog class ?
    650
  10. Makefile to automate the build process for C++
    279
  11. How to load HTML resource on MFC ?
    841
  12. URL Encode, Decode function for MFC
    604
  13. KMP String Matching Algorithm
    672
  1. Finding process name on OS
    407
  2. Convert System.DateTime to UNIX timestamp
    381
  3. Retrieve system information
    495
  4. How to call SetTimer function on MFC CDialog class ?
    650
  5. How to rename(change file name) in C# ?
    1,075
  6. Find Files in certain directory
    926
  7. Implementing C#'s foreach loop in Delphi 8
    459
  8. How to detect resource leaks in a Form
    939
  9. The best way to enable and disable buttons, menu items and toolbar buttons
    667
  10. How to exit from a console application
    810
  11. How to restrict a program to a single instance
    729
  12. Compress and decompress strings in C#
    971
  13. Graphics in C# - Irregular Forms
    908
  14. Extracting the Country from IP Address
    468
  15. How to run shell command by MFC ?
    435
  16. Double Linked List based in C++
    796
  17. String Replace Function For C#
    1,547
  18. How to print character in C# by ASCII code
    476
  19. MFC based World Wide Web HTTP Server Source Code
    459
  20. Creating and Using a CAsyncSocket Object to use CAsyncSocket
    472
  21. UDP Send and Receive Using CAsyncSocket
    1,204
  22. Using the shell to receive notification of removable media being inserted or removed
    680
  23. Adding Custom Paper Sizes To Named Printers
    646
  24. Print HTML In C# With Or Without The Web Browser Control And The Print Dialog
    972
  25. A Customizable Printing Text Class
    432
  26. Simplified .NET Printing In C#
    692
  27. How To Print Text In C#
    455
  28. C# Lists
    1,151
  29. Simple Example of IF/THEN using C#
    1,265
  30. Fast and Good Keyboard/Mouse Test without the message handler
    982
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    39
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    34
  3. How do I create animated background for Google Meet?
    20,611
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    244
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    94
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    157
  7. Array of arguments passed to script in PHP
    90
  8. Invalid Auction Storms in Ad Tech
    98
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    202
  10. The Two Clocks of Ad Server Architecture
    70
  11. Frequency Cap Architecture Overview
    514
  12. Collecting Proof-of-Play from 100,000 Screens
    146
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    287
  14. What is RedisVL?
    109
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    230
  16. TPU & GPU Cluster Management Tools
    243
  17. The Concept of Scaffolding in AI Agents
    233
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    583
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    258
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    339
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    202
  22. Machine Learning: Training vs. Ranking
    221
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    222
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    195
  25. Designing a Data Curation System Around a Vector Database
    220
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    256
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    656
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    195
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    216
  30. Understanding Data Lineage
    260