0% found this document useful (0 votes)
20 views21 pages

Week 13-14

Uploaded by

sanihawork29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views21 pages

Week 13-14

Uploaded by

sanihawork29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

Binary Search

Binary Search
 Binary Search is a searching algorithm for
finding an element's position in a sorted
array.
 In this approach, the element is always
searched in the middle of a portion of an
array.
 Binary search can be implemented only on a
sorted list of items. If the elements are not
sorted already, we need to sort them first.
Binary Search

The general steps for binary search method are


discussed below.
1. The array in which searching is to be performed is:
Binary Search
 Let x = 4 be the element to be searched.
2. Set two pointers low and high at the lowest and the highest
positions respectively.
Find the middle element mid of the array ie. arr[(low + high)/2] = 6.

Cont..
 3. Find the middle element mid of the array ie. arr[(low
+ high)/2] = 6.
Cont.

4. If x == mid, then return mid. Else, compare the


element to be searched with m.
5. If x > mid, compare x with the middle element of the
elements on the right side of mid. This is done by
setting low to low = mid + 1.
6. Else, compare x with the middle element of the
elements on the left side of mid. This is done by
setting high to high = mid - 1.
Binary Search

7. Repeat steps 3 to 6 until low meets high.


x=4 is found

Binary Search

8. x = 4 is found.
Binary Search Algorithm

do until the pointers low and high meet each other.


mid = (low + high)/2
if (x == arr[mid])
return mid
else if (x > arr[mid]) // x is on the right side
low = mid + 1
else // x is on the left side
high = mid - 1
1D Character Arrays (C-String)
Character Array as String

 A string is a collection of characters. There


are two types of strings commonly used in
C++ :
 Strings that are objects of string class (The
Standard C++ Library String Class)
 C-strings (C-style Strings)
C-strings

 In C programming, the collection of


characters is stored in the form of arrays.
This is also supported in C++ programming.
Hence it's called C-strings.

 C-strings are arrays of type char terminated


with a null character, that is, \0 (ASCII value
of null character is 0).
How to define a C-string?

 char str[] = "C++";


 In the above code, str is a string and it
holds 4 characters.

 Although "C++" has three characters, the


null character \0 is added to the end of
the string automatically.
Alternative ways of
defining a string
 char str[4] = "C++";
 char str[] = {'C','+','+','\0'};
 char str[4] = {'C','+','+','\0'};

 Like arrays, it is not necessary to use all the space


allocated for the string. For example:

 char str[100] = "C++";


Example 1: C++ String to Read a
Word
 #include <iostream>
 using namespace std;

 int main()
 {
 char str[100];

 cout << "Enter a string: ";


 cin >> str;
 cout << "You entered: " << str << endl;

 cout << "\nEnter another string: ";


 cin >> str;
 cout << "You entered: "<<str<<endl;

 return 0;
 }
Output
 Enter a string: C++
 You entered: C++

 Enter another string: Programming is fun.


 You entered: Programming

 Notice that, in the second example, only "Programming" is


displayed instead of "Programming is fun".

 This is because the extraction operator >> works as scanf() in C


and considers a space " " as a terminating character.
Example 2: C++ String to read a line of
text

 #include <iostream>
 using namespace std;

 int main()
 {
 char str[100];
 cout << "Enter a string: ";
 cin.get(str, 100);

 cout << "You entered: " << str << endl;


 return 0;
 }
Output
 Enter a string: Programming is fun.
 You entered: Programming is fun.

 To read the text containing blank space, cin.get function


can be used. This function takes two arguments.

 The first argument is the name of the string (address of the


first element of the string), and the second argument is the
maximum size of the array.

 In the above program, str is the name of the string and


100 is the maximum size of the array.
String Object

 In C++, you can also create a string


object for holding strings.

 Unlike using char arrays, string objects


have no fixed length and can be
extended as per your requirement.
Example 3: C++ string using string data type

 #include <iostream>
 using namespace std;

 int main()
 {
 // Declaring a string object
 string str;
 cout << "Enter a string: ";
 getline(cin, str);

 cout << "You entered: " << str << endl;


 return 0;
 }
Output
 Enter a string: Programming is fun.
 You entered: Programming is fun.

 In this program, a string str is declared. Then, the string is


asked from the user.

 Instead of using cin>> or cin.get() function, you can get


the entered line of text using getline().

 getline() function takes the input stream as the first


parameter, which is cin and str as the location of the line to
be stored.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy