Found 7346 Articles for C++

C++ Program to Find Maximum Element in an Array using Binary Search

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

381 Views

This is a C++ Program to find the maximum element of an array using Binary Search Tree. The time complexity of this program is O(log(n)).AlgorithmBegin Construct the Binary Search Tree using the given data elements. Next traverse the root pointer to the rightmost child node available. Print the data part of the node as the maximum data element of the given data set. Print the Depth of the maximum data. EndExample Code#include using namespace std; struct node {    int d;    node *left;    node *right; }; ... Read More

C++ Program to Find Minimum Element in an Array using Linear Search

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

756 Views

This is a C++ Program to find the minimum element of an array using Linear Search approach. The time complexity of this program is O(n).AlgorithmBegin Assign the data element to an array. Assign the value at ‘0’ index to minimum variable. Compare minimum with other data element sequentially. Swap values if minimum value is more then the value at that particular index of the array. print the minimum value. EndExample Code#include using namespace std; int main() { int n, i, minimum, a[10] = {1, 6, 7, 10, 12, 14, 12, 16, 20, 26}; char ch; minimum = a[0]; cout

C++ Program to Perform Uniform Binary Search

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

215 Views

In uniform binary search here we implement binary search using a lookup table. It is an improvement in binary search since table lookup is faster than a shift and addition. The time complexity of this approach is O(log(n)).AlgorithmBegin    Assign the data to the array in a sorted manner.    Calculate the maximum length of lookup array and declare a new array ‘del’.    Assign the values to the lookup array as n/2, n/4 and so on till ‘0’, where n is the length of the data    array.    Call UniBinarySearch() function.    Assign mid to the value at ... Read More

C++ Program to Search Sorted Sequence Using Divide and Conquer with the Aid of Fibonacci Numbers

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

386 Views

In this C++ program we implement a Divide and Conquer approach using Fibonacci numbers. Using Fibonacci numbers, we calculate mid of data array to search the data item. The time complexity of this approach is O(log(n)).AlgorithmBegin    Assign the data to the array in a sorted manner.    Take input of the element to be searched.    Call FibonacciSearch() function.    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 ... Read More

C++ Program to Find kth Largest Element in a Sequence

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

209 Views

In this program, we need to extract the Kth largest element from a sequence. the time complexity of this technique can be improved by approaching the problem using max-heap. The time complexity of this program is O(n + k*log(n)).AlgorithmBegin    Send the max of the heap to the end of the sequence.    Heapify the remaining sequence.    Repeat the process for ‘k’ times.    Print the final state of the array.    Print the max from the heap extracted from kth iteration as a result. End.Example Code#include using namespace std; void MaxHeapify(int a[], int i, int n) { ... Read More

C++ Program to Search for an Element in a Binary Search Tree

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

2K+ Views

In this program we need to. Implement binary search to find the existence of a search sequence in a binary search tree. The worst case time complexity of Binary search is O(n) but for the average case O(log(n)).AlgorithmBegin    Construct binary search tree for the given unsorted data array by inserting data into tree one by one.    Take the input of data to be searched in the BST.    Now starting from the root node, compare the data with data part of the node.    if data < temp->d, move the temp pointer to the left child.    if ... Read More

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

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

369 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

248 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

453 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

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

3K+ Views

Quick sort is based on divide-and-conquer. The average time complexity of this algorithm is O(n*log(n)) but the worst case complexity is O(n^2). To reduce the chances of the worst case here Quicksort is implemented using randomization.Algorithmpartition(int a[], int l, int h)Begin    pivot = h    Index = l    start = l and end = h    while start < end do       while a[start] pivot do          end = end – 1       done       if start < end then          swap a[start] with a[end] ... Read More

Advertisements