Found 7197 Articles for C++

C++ Program to Implement a Binary Search Algorithm for a Specific Search Sequence

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

521 Views

In this Program we need to implement binary search to find the existence of a search sequence in an array. The time complexity of Binary search is O(log(n)).Required steps and pseudocodesBegin    BinarySearch() function has ‘arr’ the array of data and ‘n’ the number of values, start and end index, iteration count and b[0] be the element to be searched in the argument list.    Increment the iteration counter and compare the item value with the a[mid].    If item < a[mid] choose first half otherwise second half to proceed further.    Return index value to main.    In main(), ... Read More

C++ Program to Perform Searching Using Self-Organizing Lists

George John
Updated on 30-Jul-2019 22:30:25

399 Views

Self-Organizing list basically updates the list of given range of items on the basis of last searched item. In this method, the sequential searching approach is used. This algorithm shifts the more important data to the beginning of the list. The time complexity of this search technique is O(n).AlgorithmBegin    Function FibonacciSearch().    Calculate the mid value using ‘start+fib[index-2]’ expression.    If the chosen item is equal to the value at mid index, print result and return to main.    If it is lesser than the value at mid index, proceed with the left sub-array.    If it is more ... Read More

C++ Program to Compare Binary and Sequential Search

Chandu yadav
Updated on 30-Jul-2019 22:30:25

604 Views

Binary Search and Sequential or Linear Search both are used in computer programming to search an element. The time complexity of Binary Search is O(log(n)) and Sequential Search is O(n).AlgorithmBegin    Algorithm for Binary Search:    BinarySearch() function with ‘arr’ the array of data and ‘n’ the number of values, start and end index, iteration count and element to be searched in the argument list.    Increment iteration counter and compare the item value with the a[mid].    If item < a[mid] choose first half otherwise second half to proceed further.    Return iteration value on successful search. EndExample Code#include ... Read More

C++ Program to Implement Quick Sort with Given Complexity Constraint

Ravi Ranjan
Updated on 03-Jun-2025 17:46:44

3K+ Views

The quick sort technique is based on the partitioning of an array into smaller sub-arrays. It is based on the divide-and-conquer algorithm. The average time complexity of this algorithm is O(n*log(n)), but the worst complexity is O(n^2). To reduce the chances of the worst case, we implement the quick sort technique using randomization. In this article, we have an array having 6 elements. Our task is to sort the given array using the quick sort technique while avoiding the worst-case complexity. How to Implement Quick Sort with Given Complexity Constraint? In the ... Read More

C++ Program to Perform Stooge Sort

Ravi Ranjan
Updated on 12-May-2025 16:50:14

341 Views

Stooge Sort is a recursive sorting algorithm used to sort the given data. The stooge sort divides the array into two overlapping parts, 2/3 each and then sort the array in three steps by sorting first then second and again first part. The worst case time complexity of this algorithm is O(n^2.7095). In this article, we have an unsorted array. Our task is to implement the stooge sort algorithm for sorting the given array in C++. Example Here is an example of sorting the given array using stooge sort: Input: array = {5, 3, 8, 4, 2, 7} ... Read More

C++ Program to Perform the Shaker Sort

Ravi Ranjan
Updated on 12-May-2025 16:50:32

2K+ Views

Shaker sort is a variation of bubble sort and is both a stable and comparison based sorting algorithm. The shaker sort is also known as cocktail sort or bidirectional bubble sort as it sorts the array in both directions. In this article, we have an unsorted array. Our task is to implement the shaker sort in C++ to sort the given array. Example The following example sorts the unsorted array using the shaker sort: Input: array = {5, 1, 4, 2, 8, 0, 2} Output: Sorted array = {0, 1, 2, 2, 4, 5, 8} The ... Read More

C++ Program to Solve Knapsack Problem Using Dynamic Programming

Chandu yadav
Updated on 29-Apr-2025 15:56:46

16K+ Views

In the 0-1 knapsack problem, a set of items is given, each with a weight and a value. We need to determine the number of each item to include in a collection so that the total weight is less than or equal to the given limit and the total value is as large as possible. In this article, we will discuss how to solve the 0-1 knapsack problem using dynamic programming. Dynamic Programming is a technique where we solve problems by breaking them into smaller sub-problems. If a subproblem is solved multiple times, DP stores the results so that we ... Read More

Does Ternary operation exist in MySQL just like C or C++?

Samual Sam
Updated on 30-Jul-2019 22:30:25

113 Views

Yes, let us first see the working of ternary operator in C or C++ language.X=(X > 10 && ( X-Y) < 0) ?: X:(X-Y);Here is the demo code in C language. After that we will check in MySQL. The C code is as follows −#include int main() {    int X;    int Y;    int result;    printf("Enter the value for X:");    scanf("%d", &X);    printf("Enter the value for Y:");    scanf("%d", &Y);    result=( X > 1 && (X-Y) < 0) ? X: (X-Y);    printf("The Result is=%d", result);    return 0; }The snapshot of C ... Read More

How to create a static class in C++?

Arjun Thakur
Updated on 13-Apr-2023 00:15:52

27K+ Views

There is no such thing as a static class in C++. The closest approximation is a class that only contains static data members and static methods.Static data members in a class are shared by all the class objects as there is only one copy of them in the memory, regardless of the number of objects of the class. Static methods in a class can only access static data members, other static methods or any methods outside the class.A program that demonstrates static data members and static methods in a class in C++ is given as follows. Example #include ... Read More

Why array index starts from zero in C/C++ ?

Ravi Ranjan
Updated on 15-May-2025 16:13:57

6K+ Views

An array arr[i] is interpreted as *(arr+i). Here, arr denotes the address of the first array element or the 0th index element. So *(arr+i) means the element is at i distance from the first element of the array. So, array index starts from 0 as initially i is 0 which means the first element of the array. In this article, we will see an example of C and C++ code to understand the reason why an array starts from index 0. Why Array Index Starts from Zero? The array index starts from zero as it provides better efficiency and ... Read More

Advertisements