C++ Articles - Page 672 of 719

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

356 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

17K+ 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

121 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

How to print size of array parameter in a function in C++?

Ravi Ranjan
Updated on 15-May-2025 19:34:00

327 Views

To print the size of an array parameter in a function in C++, we will use the typeOf() operator. In this article, we have passed an array as an argument to the function. Our task is to print the size of this array in C++. Printing Size of Static Array Parameter When we pass an array as an argument of function in C++, it is considered as a pointer. The sizeOf() operator returns the size of the pointer, depending on the system(64-bit or 32-bit) rather than returning the size of the array. Here is a code example explaining this. ... Read More

When are static objects destroyed in C++?

Ravi Ranjan
Updated on 16-May-2025 17:25:38

3K+ Views

C++ Static Object A static object is declared with the static keyword. The static objects are initialized only once and stored in the static storage area. The static objects are only destroyed when the program terminates i.e. they live until program termination. In this article, we will understand static object, their types, and their examples. Syntax of Static Object The syntax for declaring a static object is as follows: Animal cat; //object declaration static Animal dog; //static object declaration Types of Static Objects The static objects can be of two types, which are mentioned below: ... Read More

How static variables in member functions work in C++?

Ravi Ranjan
Updated on 16-May-2025 17:24:40

1K+ Views

The static variables in C++ are defined using the static keyword. The static member variables in a class are shared by all the class objects as there is only one copy of them in memory, regardless of the number of objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present. In this article, we will understand the static member variables, their characteristics, and how static variables work in member functions with the help of example code. The characteristics of the static keyword are mentioned below: Characteristics ... Read More

Advertisements