Found 34484 Articles for Programming

C++ Program to Check the Connectivity of Directed Graph Using DFS

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

493 Views

To check connectivity of a graph, we will try to traverse all nodes using any traversal algorithm. After completing the traversal, if there is any node, which is not visited, then the graph is not connected.For the directed graph, we will start traversing from all nodes to check connectivity. Sometimes one edge can have only outward edge but no inward edge, so that node will be unvisited from any other starting node.In this case the traversal algorithm is recursive DFS traversal.Input: Adjacency matrix of a graph0100000100000111000001000Output: The Graph is connected.Algorithmtraverse(u, visited)Input: The start node u and the visited node to ... Read More

C++ Program to Implement Interpolation Search Algorithm

Nancy Den
Updated on 30-Jul-2019 22:30:25

929 Views

For the binary search technique, the lists are divided into equal parts. For the interpolation searching technique, the procedure will try to locate the exact position using interpolation formula. After finding the estimated location, it can separate the list using that location. As it tries to find exact location every time, so the searching time reduces. This technique can find items easily if the items are uniformly distributed.The complexity of Interpolation Search TechniqueTime Complexity: O(log2(log2 n)) for average case, and O(n) for worst case (when items are distributed exponentially)Space Complexity: O(1)Input − A sorted list of data 10 13 15 ... Read More

C++ Program to Implement Counting Sort

Nancy Den
Updated on 30-Jul-2019 22:30:25

4K+ Views

Counting sort is a stable sorting technique, which is used to sort objects according the keys that are small numbers. It counts the number of keys whose key values are same. This sorting technique is efficient when difference between different keys are not so big, otherwise it can increase the space complexity.The complexity of counting Sort TechniqueTime Complexity: O(n+r)Space Complexity: O(n+r)Input − A list of unsorted data: 2 5 6 2 3 10 3 6 7 8Output − Array after Sorting: 2 2 3 3 5 6 6 7 8 10AlgorithmcountingSort(array, size)Input: An array of data, and the total number ... Read More

C++ Program to Implement Shell Sort

Nancy Den
Updated on 30-Jul-2019 22:30:25

1K+ Views

The shell sorting technique is based on the insertion sort. In the insertion sort sometimes we need to shift large block to insert item in the correct location. Using shell sort, we can avoid large number of shifting. The sorting is done with specific interval. After each pass the interval is reduced to make smaller interval.The complexity of Shell Sort TechniqueTime Complexity: O(n log n) for best case, and for other cases, it depends on the gap sequence.Space Complexity: O(1)Input − The unsorted list: 23 56 97 21 35 689 854 12 47 66 Output − Array after Sorting: 12 ... Read More

C++ Program to Implement Insertion Sort

Nancy Den
Updated on 30-Jul-2019 22:30:25

16K+ Views

This sorting technique is similar with the card sorting technique, in other words we sort cards using insertion sort mechanism. For this technique, we pick up one element from the data set and shift the data elements to make a place to insert back the picked up element into the data set.The complexity of Insertion Sort TechniqueTime Complexity: O(n) for best case, O(n2) for average and worst caseSpace Complexity: O(1)Input − The unsorted list: 9 45 23 71 80 55 Output − Array after Sorting: 9 23 45 55 71 80AlgorithminsertionSort(array, size)Input: An array of data, and the total number ... Read More

C++ Program to Implement Selection Sort

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

19K+ Views

In the selection sort technique, the list is divided into two parts. In one part all elements are sorted and in another part the items are unsorted. At first we take the maximum or minimum data from the array. After getting the data (say minimum) we place it at the beginning of the list by replacing the data of first place with the minimum data. After performing the array is getting smaller. Thus this sorting technique is done.The complexity of Selection Sort TechniqueTime Complexity: O(n2)Space Complexity: O(1)Input − The unsorted list: 5 9 7 23 78 20 Output − Array ... Read More

C++ Program to Implement Merge Sort

Krantik Chavan
Updated on 07-Sep-2023 00:49:37

38K+ Views

The merge sort technique is based on divide and conquer technique. We divide the while data set into smaller parts and merge them into a larger piece in sorted order. It is also very effective for worst cases because this algorithm has lower time complexity for worst case also.The complexity of Merge Sort TechniqueTime Complexity: O(n log n) for all casesSpace Complexity: O(n)Input − The unsorted list: 14 20 78 98 20 45 Output − Array after Sorting: 14 20 20 45 78 98Algorithmmerge(array, left, middle, right)Input: The data set array, left, middle and right indexOutput: The merged listBegin   ... Read More

C++ Program to Implement Heap Sort

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

630 Views

A Heap is a complete binary tree which is either Min Heap or Max Heap. In a Max Heap, the key at root must be maximum among all keys present in Heap. This property must be recursively true for all nodes in that Binary Tree. Min Heap is similar to MinHeap.Function descriptionsvoid BHeap::Insert(int ele): Perform insertion operation to insert element in heap.void BHeap::DeleteMin(): Perform deleteion operation to delete minimum value from heap.int BHeap::ExtractMin(): Perfrom operation to extract minimum value from heap.void BHeap::showHeap(): To show the elements of heap.void BHeap::heapifyup(int in): maintain heap structure in bottom up manner.void BHeap::heapifydown(int in): maintain ... Read More

C++ Program to Implement Bubble Sort

Nancy Den
Updated on 30-Jul-2019 22:30:25

14K+ Views

Bubble Sort is comparison based sorting algorithm. In this algorithm adjacent elements are compared and swapped to make correct sequence. This algorithm is simpler than other algorithms, but it has some drawbacks also. This algorithm is not suitable for large number of data set. It takes much time to solve the sorting tasks.The complexity of Bubble Sort TechniqueTime Complexity: O(n) for best case, O(n2) for average and worst caseSpace Complexity: O(1)Input − A list of unsorted data: 56 98 78 12 30 51 Output − Array after Sorting: 12 30 51 56 78 98AlgorithmbubbleSort(array, size)Input: An array of data, and ... Read More

C++ Program to Implement Bucket Sort

Nancy Den
Updated on 30-Jul-2019 22:30:25

2K+ Views

In the Bucket Sorting technique, the data items are distributed of a set of buckets. Each bucket can hold similar type of data. After distributing, each bucket is sorted using another sorting algorithm. After that all elements are gathered into the main list to get the sorted form.The complexity of Bucket Sort TechniqueTime Complexity: O(n + k) for best case and average case and O(n2 ) for worst case.Space Complexity: O(nk) for worst caseInput − A list of unsorted data: 0.25 0.36 0.58 0.41 0.29 0.22 0.45 0.79 0.01 0.69 Output − Array after Sorting: 0.01 0.22 0.25 0.29 0.36 ... Read More

Advertisements