Found 34484 Articles for Programming

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

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

372 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

251 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

455 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

C++ Program to Perform Stooge Sort

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

240 Views

Stooge Sort is used to sort the given data. It is a recursive sorting algorithm. Stooge Sort divides the array into two overlapping parts, 2/3 each and Sort the array in three steps by sorting I then II and again I part. The worst case time complexity of this algorithm is O(n^2.7095).AlgorithmBegin Take input of data. Call StoogeSort() function with ‘a’ the array of data and ‘n’ the number of values, in the argument list. Implement Sorting using recursive approach. Divide the array into first 2/3 element as ... Read More

C++ Program to Perform the Shaker Sort

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

1K+ Views

Shaker sort is used to sort the given data. Shaker sort, unlike Bubble sort, orders the array in both directions. The worst complexity of this algorithm is O(n^2).AlgorithmBegin    ShakerSort() function has ‘arr’ the array of data and ‘n’ the number of values, in the argument list.    // Implement Sorting algorithm using nested for loops.    The parent loop will run on ‘i’ from 0 to n-1 and contains two loops inside.    The first loop will run on ‘j’ from i+1 to n-1 and use swap() if a[j] < a[j-1].    Decrement n.    The second loop will ... Read More

C++ Program to Solve Knapsack Problem Using Dynamic Programming

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

13K+ Views

This is a C++ program to solve 0-1 knapsack problem using dynamic programming. In 0-1 knapsack problem, a set of items are 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 large as possible.AlgorithmBegin Input set of items each with a weight and a value Set knapsack capacity Create a function that returns maximum of two integers. Create a function which returns the maximum value that can be ... Read More

Create Pair Tuple from Array in Java

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

547 Views

Use the fromArray() method to create a Pair Tuple from Array.Let us first see what we need to work with JavaTuples. To work with Pair class in JavaTuples, you need to import the following package −import org.javatuples.Pair;Note − Steps to download and run JavaTuples program. If you are using Eclipse IDE to run Pair Class in JavaTuples, then Right Click Project ->Properties ->Java Build Path ->Add External Jars and upload the downloaded JavaTuples jar file.The following is an example −Exampleimport org.javatuples.Pair; public class Demo { public static void main(String[] args) { ... Read More

Create Unit Tuple in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

104 Views

To create a Unit Tuple in Java is quite easy.Let us first see what we need to work with JavaTuples. To work with the Unit class in JavaTuples, you need to import the following package −import org.javatuples.Unit;Let us see an example.Note − Steps to download and run JavaTuples program. If you are using Eclipse IDE to run Unit Class in Java Tuples, then Right Click Project ->Properties ->Java Build Path ->Add External Jars and upload the downloaded JavaTuples jar file.Exampleimport org.javatuples.Unit; public class Demo { public static void main(String[] args) { ... Read More

Create Septet Tuple in Java

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

82 Views

To create a Septet Tuple in Java, use the with() method.Let us first see what we need to work with JavaTuples. To work with Septet class in JavaTuples, you need to import the following package −import org.javatuples.Septet;Note − Steps to download and run JavaTuples program. If you are using Eclipse IDE to run Septet Class in JavaTuples, then Right Click Project ->Properties ->Java Build Path ->Add External Jars and upload the downloaded JavaTuples jar file.The following is an example −Exampleimport org.javatuples.Septet; public class Demo { public static void main(String[] args) { ... Read More

Advertisements