Found 7197 Articles for C++

C/C++ Program for Odd-Even Sort (Brick Sort)?

Arnab Chakraborty
Updated on 01-Jul-2020 14:31:25

520 Views

Here we will see how the brick sort works. The Brick sort is one modification of bubble sort. This algorithm is divided into two parts. These parts are odd part and even parts. In the odd part we will use the bubble sort on odd indexed items, and in the even part we will use the bubble sort on even indexed elements. Let us see the algorithm to get the idea.AlgorithmbrickSort(arr, n)begin    flag := false    while the flag is not true, do       flag := true       for i := 1 to n-2, increase ... Read More

C/C++ Program for Finding the vertex, focus and directrix of a parabola?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

175 Views

Here we will see how to find the vertex, focus directrix of a parabola using C or C++ program. To get these parameters we need the general equation of a parabola. The general formula is −𝑦 = 𝑎𝑥2 + 𝑏𝑥 + 𝑐The values of a, b and c are given.The formula for the vertex −The formula for the focus −The formula for the Directrix - y −Example Live Demo#include using namespace std; void getParabolaDetails(float a, float b, float c) {    cout

C++ Program to Split the array and add the first part to the end?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

523 Views

Here we will see how to split an array, and add the first part after splitting at the end position. Suppose the array contents are {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. We want to cut this intro two parts. The first part is from index 0 to 3 (split size 4), and second part is rest. After adding the first part at the end, the array elements will be like this {4, 5, 6, 7, 8, 9, 0, 1, 2, 3}. To solve this problem, we will follow this algorithm.AlgorithmsplitArray(arr, n, k)begin    for i := ... Read More

C++ Program to find sum of even factors of a number?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

397 Views

In this section we will see how we can get the sum of all even prime factors of a number in an efficient way. There is a number say n = 480, we have to get all factor of this. The prime factors of 480 are 2, 2, 2, 2, 2, 3, 5. The sum of all even factors is 2+2+2+2+2 = 10. To solve this problem, we have to follow this rule −When the number is divisible by 2, add them into the sum, and divide the number by 2 repeatedly.Now the number must be odd. So we will ... Read More

C++ Program for Recursive Bubble Sort?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

1K+ Views

In this section we will see another approach of famous bubble sort technique. We have used bubble sort in iterative manner. But here we will see recursive approach of the bubble sort. The recursive bubble sort algorithm is look like this.AlgorithmbubbleRec(arr, n)begin    if n = 1, return    for i in range 1 to n-2, do       if arr[i] > arr[i+1], then          exchange arr[i] and arr[i+1]       end if    done    bubbleRec(arr, n-1) endExample Live Demo#include using namespace std; void recBubble(int arr[], int n){    if (n == 1)     ... Read More

C++ Program for Range sum queries without updates?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

328 Views

Here we will see how to get the sum of elements from index i to index j in an array. This is basically the range query. The task is easy by just running one loop from index i to j, and calculate the sum. But we have to care about that this kind of range query will be executed multiple times. So if we use the mentioned method, it will take much time. To solve this problem using more efficient way we can get the cumulative sum at first, then the range sum can be found in constant time. Let ... Read More

C++ Program for Largest K digit number divisible by X?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

147 Views

In this problem we will try to find largest K-digit number, that will be divisible by X. To do this task we will take the largest K digit number by this formula ((10^k) – 1). Then check whether the number is divisible by X or not, if not, we will get the exact number by using this formula.𝑚𝑎𝑥−(𝑚𝑎𝑥 𝑚𝑜𝑑 𝑋)One example is like a 5-digit number, that is divisible by 29. So the largest 5-digit number is 99999. This is not divisible by 29. Now by applying the formula we will get −99999−(99999 𝑚𝑜𝑑 29)=99999−7=99992The number 99992 is divisible by ... Read More

C++ Program for Gnome Sort?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

497 Views

Here we will see how the gnome sort works. This is another sorting algorithm. In this approach if the list is already sorted it will take O(n) time. So best case time complexity is O(n). But average case and worst case complexity is O(n^2). Now let us see the algorithm to get the idea about this sorting technique.AlgorithmgnomeSort(arr, n)begin    index := 0    while index < n, do       if index is 0, then          index := index + 1       end if       if arr[index] >= arr[index -1], then ... Read More

C++ Program for Comb Sort?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

356 Views

The basic idea of comb sort and the bubble sort is same. In other words, comb sort is an improvement on the bubble sort. In the bubble sorting technique, the items are compared with the next item in each phase. But for the comb sort, the items are sorted in a specific gap. After completing each phase, the gap is decreased. The decreasing factor or the shrink factor for this sort is 1.3. It means that after completing each phase the gap is divided by 1.3. Time Complexity is O(n log n) for best case. O(n2/2nP) (p is number of ... Read More

C++ Program for Cocktail Sort?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

640 Views

The Cocktail sort is another variation of bubble sort. In the bubble sort technique, it always searches from left to right, and finds the largest element at the end, in the second phase it finds the second largest element at the second last position. This sorting technique traverses in both directions alternatively. Let us see the algorithm to understand the idea.Algorithmcocktail(array, n)Begin    flag := true    start := 0, end := n-1    while flag is set, do       flag := false       for i in range start to end-1, do         ... Read More

Advertisements