 # C++ Programming Code Examples

## C++ > Sorting Searching Code Examples

### C++ Program to Implement Heap's Algorithm for Permutation of N Numbers

``` C++ Program to Implement Heap's Algorithm for Permutation of N Numbers - This algorithm print the permutation using heap algorithm. - The time complexity of this algorithm is 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. - Exit. #include<iostream> #include<iomanip> using namespace std; // A function swapping values using references. void swap(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; } // A function to implement Heap's Algorithm for the permutation of N numbers. void print(const int *v) { int i; int size = sizeof(v)/sizeof(int)+1; // Loop to print the sequence. cout<<"\t"; for ( i = 0; i < size; i++) cout<<setw(4)<<v[i]; cout<<"\n"; } void HeapPermute(int v[], int n) { int i; // Print the sequence if the heap top reaches to the 1. if (n == 1) print(v); else { // Fix a number at the heap top until only two one element remaining and permute remaining. for (i = 0; i < n; i++) { HeapPermute(v, n-1); // If odd then swap the value at the start index with the n-1. if(n%2 == 1) swap(&v, &v[n-1]); // If even then swap the value at the 'i' index with the n-1. else swap(&v[i], &v[n-1]); } } } int main() { int i, n, count = 1; cout<<"How many numbers do you want to enter: "; cin>>n; int num[n]; // Take the input. cout<<"\nEnter the numbers: "; for (i = 0; i < n; i++) { cin>>num[i]; count *= (i+1); } // Print the permutation's count. cout<<"\nThe number of permutations possible is: "<<count<<"\n"; // Calling Function to print all the permutation. HeapPermute(num, n); return 0; } ```

Count Frequency of Characters in The C++ - Frequency of character in a string means how many times a "particular character" is present in a string. In sample; suppose we have string hitesh in this word h repeated 2 times, it's the

Concatenate 2 Strings without using strcat - In the following program user would be asked to enter 'two Strings' and then the C program would 'concatenate them'. For concatenation we have not used the standard library strcat()

Read Two Integers and Swap their Values - C language program coding to take the two integers as input and store it in the variables m & n respectively. Call function swap. Pass addresses of variables to the function swap.

Check a Given Number is Perfect Number - This C program code checks whether a given number is perfect number. Perfect number is a number which is equal to sum of its divisor. For eg,divisors of 6 are 1,2 and 3. The sum of

C Program Code Concatenate Two Strings - C programming code concatenate 2 strings in single string. Concatenation of 'two strings' is the process of Joining them together to form a new string. Concatenation appends second

C Least Significant Bit (LSB) of a number is - C program code input any number from user and check whether the Least Significant Bit (LSB) of the given number is set (1) or not (0). How to check whether the least significant...

Implementations Quadratic Probing Hashs - Internal method to test if a positive number is prime. Not efficient algorithm. Using Internal method to return a prime number at least as large as n. Assumes "n > 0". Insert item x into

C Sum of Row & Column of a MxN Matrix - C Program finds the sum of each row & each column of a MxN matrix. The c program code accepts an MxN matrix. Then adds each row of the matrix and also adds each column of...