 # C++ Programming Code Examples

## C++ > Sorting Searching Code Examples

This algorithm takes the input of the number of 'Vertexes' and their corresponding degree. Checks various 'constraints', tries to build the graph. If it fails, no valid graph can be created

A 'function implementing' Binary search on a 'Sorted array'. Every time this function called, counted as a iteration of 'binary search'. So if value is "Less than Value" at start index more

Prints a total number of combination possible for given n and r value. The "time complexity" of this algorithm is O(n). This algorithm takes the input of n and r value. Function to find the

The Time complexity to generate this code is 'O(v*e)'. This algorithm takes the input of the number of vertexes for the tree. Then it takes the input if "Vertex Pairs" which have an edge

It is an improvement in BST by adding 2 more key functions - 'rank()' and 'select()'. The time complexity of Order-Statistic tree generation is O(n+n*log(n)). Once the tree is constructed

Firstly We need to find k numbers which have a minimum difference with the median of the data set. It includes sorting using 'Quick Sort' and then printing k numbers as a result. Time

A Function to build max heap from the initial array by checking all non-leaf node to satisfy the condition. And build "max-heap" k times, extract the 'maximum' and store it in the end

Construct "Binary Search" tree for "unsorted" data array. For the 'Maximum element' move the pointer to the rightmost child node. This value will be the minimum value among data.

Program find the 'median' of elements where elements are stored in 2 'different arrays'. We need to find 'combined' median of 2 different 'data set'. So it includes sorting of both arrays

First 'Compare the Element' at the beginning with another array element sequentially. And swap values if the element at the beginning is Larger than the other element. This value will

Linearly traverse the data array. Keep track of the "Smallest Number". Simultaneously keep updating the second smallest number also. A function to calculate second. If array element

Function to find the maximum sum sub-array which includes mid of the sub-array. Take the input of the "Integer Array". Using Divide and Conquer approach break the Array. Compute

Implement the 'naive method' to find the sub array having a maximum sum. The worst case time 'Complexity' of the algorithm is 'O(n*n)'. Take the input of the 'integer array'. Compare

Algorithm finds the median of 2 sorted arrays using binary search approach. Takes the input of 'n' Data Elements of both the arrays. Using decrease, conquer method find the combined

Construct binary search tree for the unsorted data array. For the "minimum element" move the pointer to the leftmost child node. So the value will be the "minimum value" among the

'Binary Search tree' for a given unsorted data array & maintain an additional count variable. If any 'Element is Repeated' then increase the count of that 'Node'. Proceed with the search

Prints a total number of permutation possible for a given string. The time complexity of this algorithm is "O(n)". Algorithm takes the input of the string. Then it checks for the repetition

Algorithm "Counts the partition" of the given number. There is no straight method to count a total number of the partition so we need to 'Generate and Count' them. Time complexity

Implement the binary search to find a peak in the array. If the 'middle element' is more than its both Neighbors, 'it is the Peak'. Otherwise, split the array and check the same. A function

This algorithm takes the input of the number of edges 'e' in the random "DAG". It connects 2 'Random Vertexes' and checks for any cycle generated due to this edge. If yes discard this

This algorithm takes the input of the 'specific' sequence. It generates random Subsequence from the given character string. A function to generate 'random' sequence of a given length
Algorithm takes the input of 'n' data element and prints all 'possible combination'. For that, it maintains a boolean 'Array of Length' "n". If the corresponding boolean value is true, then
Algorithm print all the 'possible combination' of each length from the given array. The time complexity of this algorithm is 'O(n*(2^n))'. A function to print all 'Combination of a Length'
Algorithm takes the input of "n" data element and 'prints all possible' combination of length 'k'. It maintains a boolean array of length 'n'. If the corresponding boolean value is true, then
This algorithm takes the 'Input of the Natural' number and prints all the possible partition of each number less than or equal to it. For each number, it starts with the number and breaks
C++ Program to find a search sequence using Binary search. Implement the "binary search" to find the first value of search sequence. It is there, compare remaining item 'Sequentially'
Bubble sort algorithm sort data by comparing 2 consecutive numbers. The time Complexity of this algorithm is O(n^2). And compare two consecutive number. Switch values if number
Divide the range into equal parts and assign a 'bucket' to each part. Split the data and insert them into the Corresponding bucket by using insertion sort. 'Merge' all the buckets into one
In c++ sort pogram sample, count the number of 'occurrences' of each element. And store it in the Array of size same as the range of data input. Use element value to refer the counter
Heap sort is comparison based algorithm. It's selection sort sample. The time complexity is O(n*log(n)). Build a max heap using the given data element. And then Delete the root node
The time complexity of this algorithm: 'O(n!)'. This algorithm 'takes the input' of "N" distinct numbers. It fixes an element at the end of the array and permutes the "remaining numbers"
Insertion sort algorithm sort data by inserting them one by one into the List. This algorithm is based on sorting playing cards by picking & inserting them one by one. Here we take data
Implement 'Binary search' using interpolation approach. The Time complexity is "O(log(n))". Implement the Binary Search on sorted array. Then Calculate mid value using 'interpolation'
Split the Data into two equal half until we get at most one element in both half. Merge Both into one making sure the resulting 'Sequence' is sorted. Then 'recursively' split them, merge
Merge-Sort is based on an algorithmic design pattern called Divide & Conquer. It forms tree structure. The height of the tree will be log(n) And we merge n element at every level of the
Quick sort is based on an 'algorithmic' design pattern called 'divide-conquer'. Unlike Merge Sort it does not require 'extra memory space'. The average time complexity is "O(n*log(n))"
Randomly select pivot value from the subpart of the array. Partition that subpart so that the values left of the 'pivot' are smaller and to the right are greater from the pivot. And consider
In this algorithm Sorting of Data is done from least significant digit to most significant digit. Here we need 10 different spaces labeled 0 to 9. Assume we have "n" Number of inputs. Let

Page 1 Page 2