Found 7346 Articles for C++

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

629 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

C++ Program to Implement Radix Sort

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

4K+ Views

Radix sort is non-comparative sorting algorithm. This sorting algorithm works on the integer keys by grouping digits which share the same position and value. The radix is the base of a number system. As we know that in decimal system the radix or base is 10. So for sorting some decimal numbers, we need 10 positional box to store numbers.The complexity of Radix Sort TechniqueTime Complexity: O(nk)Space Complexity: O(n+k)Input − The unsorted list: 802 630 20 745 52 300 612 932 78 187 Output − Data after Sorting: 20 52 78 187 300 612 630 745 802 932AlgorithmradixSort(array, size, maxDigit)Input: ... Read More

C++ Program to Find Fibonacci Numbers using Dynamic Programming

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

3K+ Views

The Fibonacci sequence is like this, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ……In this sequence the nth term is the sum of (n-1)th and (n-2)th terms.To generate we can use the recursive approach, but in dynamic programming the procedure is simpler. It can store all Fibonacci numbers in a table, by using that table it can easily generate the next terms in this sequence.Input − Take the term number as an input. Say it is 10Output − The 10th fibinacci term is 55AlgorithmgenFiboSeries(n)Inputmax number of terms.OutputThe nth Fibonacci term.Begin define array named fibo of size ... Read More

C++ program to convert Fahrenheit to Celsius

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

3K+ Views

In this program we will see how to convert Celsius to Fahrenheit using C++. As we know the formula is simple.AlgorithmBegin Take the Celsius temperature in C calculate F = (9C/5)+32 return F EndExample Code#include using namespace std; main() { float f, c; cout > c; f = (9.0*c/5.0)+32; cout

C++ program to generate random number

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

4K+ Views

Let us see how to generate random numbers using C++. Here we are generating a random number in range 0 to some value. (In this program the max value is 100).To perform this operation we are using the srand() function. This is in the C library. The function void srand(unsigned int seed) seeds the random number generator used by the function rand.The declaration of srand() is like belowvoid srand(unsigned int seed)It takes a parameter called seed. This is an integer value to be used as seed by the pseudo-random number generator algorithm. This function returns nothing.To get the number we ... Read More

Advertisements