codeparrot/xlcost-text-to-code huggingface dataset:
Replace node with depth in a binary tree | CPP program to replace every key value with its depth . ; A tree node structure ; Utility function to create a new Binary Tree node ; Helper function replaces the data with depth Note : Default value of level is 0 for root . ; Base Case ; Replace data with current depth ; A utility function to print inorder traversal of a Binary Tree ; Driver function to test above functions ; Constructing tree given in the above figure
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; struct Node * newNode ( int data ) { Node * temp = new Node ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } void replaceNode ( struct Node * node , int level = 0 ) { if ( node == NULL ) return ; node -> data = level ; replaceNode ( node -> left , level + 1 ) ; replaceNode ( node -> right , level + 1 ) ; } void printInorder ( struct Node * node ) { if ( node == NULL ) return ; printInorder ( node -> left ) ; cout << node -> data << " ▁ " ; printInorder ( node -> right ) ; } int main ( ) { struct Node * root = new struct Node ; root = newNode ( 3 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 5 ) ; root -> left -> left = newNode ( 1 ) ; root -> left -> right = newNode ( 4 ) ; cout << " Before ▁ Replacing ▁ Nodes STRNEWLINE " ; printInorder ( root ) ; replaceNode ( root ) ; cout << endl ; cout << " After ▁ Replacing ▁ Nodes STRNEWLINE " ; printInorder ( root ) ; return 0 ; }
Product of minimum edge weight between all pairs of a Tree | C ++ Implementation of above approach ; Function to return ( x ^ y ) mod p ; Declaring size array globally ; Initializing DSU data structure ; Function to find the root of ith node in the disjoint set ; Weighted union using Path Compression ; size of set A is small than size of set B ; size of set B is small than size of set A ; Function to add an edge in the tree ; Build the tree ; Function to return the required product ; Sorting the edges with respect to its weight ; Start iterating in decreasing order of weight ; Determine Current edge values ; Calculate root of each node and size of each set ; Using the formula ; Calculating final result ; Weighted union using Path Compression ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define mod  1000000007 NEW_LINE int power ( int x , unsigned int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } int size [ 300005 ] ; int freq [ 300004 ] ; vector < pair < int , pair < int , int > > > edges ; void initialize ( int Arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { Arr [ i ] = i ; size [ i ] = 1 ; } } int root ( int Arr [ ] , int i ) { while ( Arr [ i ] != i ) { i = Arr [ i ] ; } return i ; } void weighted_union ( int Arr [ ] , int size [ ] , int A , int B ) { int root_A = root ( Arr , A ) ; int root_B = root ( Arr , B ) ; if ( size [ root_A ] < size [ root_B ] ) { Arr [ root_A ] = Arr [ root_B ] ; size [ root_B ] += size [ root_A ] ; } else { Arr [ root_B ] = Arr [ root_A ] ; size [ root_A ] += size [ root_B ] ; } } void AddEdge ( int a , int b , int w ) { edges . push_back ( { w , { a , b } } ) ; } void MakeTree ( ) { AddEdge ( 1 , 2 , 1 ) ; AddEdge ( 1 , 3 , 3 ) ; AddEdge ( 3 , 4 , 2 ) ; } int MinProduct ( ) { int result = 1 ; sort ( edges . begin ( ) , edges . end ( ) ) ; for ( int i = edges . size ( ) - 1 ; i >= 0 ; i -- ) { int curr_weight = edges [ i ] . first ; int Node1 = edges [ i ] . second . first ; int Node2 = edges [ i ] . second . second ; int Root1 = root ( freq , Node1 ) ; int Set1_size = size [ Root1 ] ; int Root2 = root ( freq , Node2 ) ; int Set2_size = size [ Root2 ] ; int prod = Set1_size * Set2_size ; int Product = power ( curr_weight , prod , mod ) ; result = ( ( result % mod ) * ( Product % mod ) ) % mod ; weighted_union ( freq , size , Node1 , Node2 ) ; } return result % mod ; } int main ( ) { int n = 4 ; initialize ( freq , n ) ; MakeTree ( ) ; cout << MinProduct ( ) ; }
Check whether an array can be made strictly decreasing by modifying at most one element | C ++ implementation of the approach ; Function that returns true if the array can be made strictly decreasing with at most one change ; To store the number of modifications required to make the array strictly decreasing ; Check whether the last element needs to be modify or not ; Check whether the first element needs to be modify or not ; Loop from 2 nd element to the 2 nd last element ; Check whether arr [ i ] needs to be modified ; Modifying arr [ i ] ; Check if arr [ i ] is equal to any of arr [ i - 1 ] or arr [ i + 1 ] ; If more than 1 modification is required ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool check ( int * arr , int n ) { int modify = 0 ; if ( arr [ n - 1 ] >= arr [ n - 2 ] ) { arr [ n - 1 ] = arr [ n - 2 ] - 1 ; modify ++ ; } if ( arr [ 0 ] <= arr [ 1 ] ) { arr [ 0 ] = arr [ 1 ] + 1 ; modify ++ ; } for ( int i = n - 2 ; i > 0 ; i -- ) { if ( ( arr [ i - 1 ] <= arr [ i ] && arr [ i + 1 ] <= arr [ i ] ) || ( arr [ i - 1 ] >= arr [ i ] && arr [ i + 1 ] >= arr [ i ] ) ) { arr [ i ] = ( arr [ i - 1 ] + arr [ i + 1 ] ) / 2 ; modify ++ ; if ( arr [ i ] == arr [ i - 1 ] arr [ i ] == arr [ i + 1 ] ) return false ; } } if ( modify > 1 ) return false ; return true ; } int main ( ) { int arr [ ] = { 10 , 5 , 11 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( check ( arr , n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Maximal Disjoint Intervals | C ++ implementation of the approach ; Function to sort the vector elements by second element of pairs ; Function to find maximal disjoint set ; Sort the list of intervals ; First Interval will always be included in set ; End point of first interval ; Check if given interval overlap with previously included interval , if not then include this interval and update the end point of last added interval ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE bool sortbysec ( const pair < int , int > & a , const pair < int , int > & b ) { return ( a . second < b . second ) ; } void maxDisjointIntervals ( vector < pair < int , int > > list ) { sort ( list . begin ( ) , list . end ( ) , sortbysec ) ; cout << " [ " << list [ 0 ] . first << " , ▁ " << list [ 0 ] . second << " ] " << endl ; int r1 = list [ 0 ] . second ; for ( int i = 1 ; i < list . size ( ) ; i ++ ) { int l1 = list [ i ] . first ; int r2 = list [ i ] . second ; if ( l1 > r1 ) { cout << " [ " << l1 << " , ▁ " << r2 << " ] " << endl ; r1 = r2 ; } } } int main ( ) { int N = 4 ; vector < pair < int , int > > intervals = { { 1 , 4 } , { 2 , 3 } , { 4 , 6 } , { 8 , 9 } } ; maxDisjointIntervals ( intervals ) ; return 0 ; }
Print k different sorted permutations of a given array | C ++ implementation of the approach ; Utility function to print the original indices of the elements of the array ; Function to print the required permutations ; To keep track of original indices ; Sort the array ; Count the number of swaps that can be made ; Cannot generate 3 permutations ; Print the first permutation ; Find an index to swap and create second permutation ; Print the last permutation ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int next_pos = 1 ; void printIndices ( int n , pair < int , int > a [ ] ) { for ( int i = 0 ; i < n ; i ++ ) cout << a [ i ] . second << " ▁ " ; cout << endl ; } void printPermutations ( int n , int a [ ] , int k ) { pair < int , int > arr [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] . first = a [ i ] ; arr [ i ] . second = i ; } sort ( arr , arr + n ) ; int count = 1 ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] . first == arr [ i - 1 ] . first ) count ++ ; if ( count < k ) { cout << " - 1" ; return ; } for ( int i = 0 ; i < k - 1 ; i ++ ) { printIndices ( n , arr ) ; for ( int j = next_pos ; j < n ; j ++ ) { if ( arr [ j ] . first == arr [ j - 1 ] . first ) { swap ( arr [ j ] , arr [ j - 1 ] ) ; next_pos = j + 1 ; break ; } } } printIndices ( n , arr ) ; } int main ( ) { int a [ ] = { 1 , 3 , 3 , 1 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int k = 3 ; printPermutations ( n , a , k ) ; return 0 ; }
Maximum width of a binary tree | C ++ program to calculate width of binary tree ; A binary tree node has data , pointer to left child and a pointer to right child ; Function protoypes ; Function to get the maximum width of a binary tree ; Get width of each level and compare the width with maximum width so far ; Get width of a given level ; Compute the " height " of a tree -- the number of nodes along the longest path from the root node down to the farthest leaf node . ; compute the height of each subtree ; use the larger one ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; Driver code ; Constructed binary tree is : 1 / \ 2 3 / \ \ 4 5 8 / \ 6 7 ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; class node { public : int data ; node * left ; node * right ; } ; int getWidth ( node * root , int level ) ; int height ( node * node ) ; node * newNode ( int data ) ; int getMaxWidth ( node * root ) { int maxWidth = 0 ; int width ; int h = height ( root ) ; int i ; for ( i = 1 ; i <= h ; i ++ ) { width = getWidth ( root , i ) ; if ( width > maxWidth ) maxWidth = width ; } return maxWidth ; } int getWidth ( node * root , int level ) { if ( root == NULL ) return 0 ; if ( level == 1 ) return 1 ; else if ( level > 1 ) return getWidth ( root -> left , level - 1 ) + getWidth ( root -> right , level - 1 ) ; } int height ( node * node ) { if ( node == NULL ) return 0 ; else { int lHeight = height ( node -> left ) ; int rHeight = height ( node -> right ) ; return ( lHeight > rHeight ) ? ( lHeight + 1 ) : ( rHeight + 1 ) ; } } node * newNode ( int data ) { node * Node = new node ( ) ; Node -> data = data ; Node -> left = NULL ; Node -> right = NULL ; return ( Node ) ; } int main ( ) { node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; root -> right -> right = newNode ( 8 ) ; root -> right -> right -> left = newNode ( 6 ) ; root -> right -> right -> right = newNode ( 7 ) ; cout << " Maximum ▁ width ▁ is ▁ " << getMaxWidth ( root ) << endl ; return 0 ; }
Divide N segments into two non | C ++ Program to divide N segments into two non empty groups such that given condition is satisfied ; Function to print the answer if it exists using the concept of merge overlapping segments ; Sort the indices based on their corresponding value in V ; Resultant array Initialise all the values in resultant array with '2' except the position at the first index of sorted vec which is initialised as '1' Initialise maxR to store the maximum of all right values encountered so far ; If the i - th index has any any point in common with the ( i - 1 ) th index classify it as '1' in resultant array and update maxR if necessary else we have found the breakpoint and we can exit the loop ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printAnswer ( vector < pair < int , int > > v , int n ) { vector < pair < int , pair < int , int > > > vec ; for ( int i = 0 ; i < n ; i ++ ) { vec . push_back ( { v [ i ] . first , { v [ i ] . second , i } } ) ; } sort ( vec . begin ( ) , vec . end ( ) ) ; vector < int > res ( n , 2 ) ; int maxR = vec [ 0 ] . second . first ; res [ vec [ 0 ] . second . second ] = 1 ; bool ok = false ; for ( int i = 1 ; i < n ; i ++ ) { if ( maxR >= vec [ i ] . first ) { res [ vec [ i ] . second . second ] = res [ vec [ i - 1 ] . second . second ] ; maxR = max ( maxR , vec [ i ] . second . first ) ; } else { ok = true ; break ; } } if ( ok ) { for ( auto x : res ) cout << x << " ▁ " ; cout << ' ' ; } else cout << " Not ▁ possible STRNEWLINE " ; } int main ( ) { vector < pair < int , int > > v = { { 2 , 8 } , { 3 , 4 } , { 5 , 8 } , { 9 , 10 } } ; int n = ( int ) v . size ( ) ; printAnswer ( v , n ) ; }
Count distinct elements in an array | CPP program to print all distinct elements of a given array ; This function prints all distinct elements ; Creates an empty hashset ; Traverse the input array ; If not present , then put it in hashtable and increment result ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDistinct ( int arr [ ] , int n ) { unordered_set < int > s ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . find ( arr [ i ] ) == s . end ( ) ) { s . insert ( arr [ i ] ) ; res ++ ; } } return res ; } int main ( ) { int arr [ ] = { 6 , 10 , 5 , 4 , 9 , 120 , 4 , 6 , 10 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countDistinct ( arr , n ) ; return 0 ; }
In | C ++ program for the above approach ; Calculating next gap ; Function for swapping ; Merging the subarrays using shell sorting Time Complexity : O ( nlog n ) Space Complexity : O ( 1 ) ; merge sort makes log n recursive calls and each time calls merge ( ) which takes nlog n steps Time Complexity : O ( n * log n + 2 ( ( n / 2 ) * log ( n / 2 ) ) + 4 ( ( n / 4 ) * log ( n / 4 ) ) + ... . . + 1 ) Time Complexity : O ( logn * ( n * log n ) ) i . e . O ( n * ( logn ) ^ 2 ) Space Complexity : O ( 1 ) ; Calculating mid to slice the array in two halves ; Recursive calls to sort left and right subarrays ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nextGap ( int gap ) { if ( gap <= 1 ) return 0 ; return ( int ) ceil ( gap / 2.0 ) ; } void swap ( int nums [ ] , int i , int j ) { int temp = nums [ i ] ; nums [ i ] = nums [ j ] ; nums [ j ] = temp ; } void inPlaceMerge ( int nums [ ] , int start , int end ) { int gap = end - start + 1 ; for ( gap = nextGap ( gap ) ; gap > 0 ; gap = nextGap ( gap ) ) { for ( int i = start ; i + gap <= end ; i ++ ) { int j = i + gap ; if ( nums [ i ] > nums [ j ] ) swap ( nums , i , j ) ; } } } void mergeSort ( int nums [ ] , int s , int e ) { if ( s == e ) return ; int mid = ( s + e ) / 2 ; mergeSort ( nums , s , mid ) ; mergeSort ( nums , mid + 1 , e ) ; inPlaceMerge ( nums , s , e ) ; } int main ( ) { int nums [ ] = { 12 , 11 , 13 , 5 , 6 , 7 } ; int nums_size = sizeof ( nums ) / sizeof ( nums [ 0 ] ) ; mergeSort ( nums , 0 , nums_size - 1 ) ; for ( int i = 0 ; i < nums_size ; i ++ ) { cout << nums [ i ] << " ▁ " ; } return 0 ; }
Rearrange an array to maximize i * arr [ i ] | C ++ program for the Optimal Solution ; Function to calculate the maximum points earned by making an optimal selection on the given array ; Sorting the array ; Variable to store the total points earned ; Driver code
#include <bits/stdc++.h> NEW_LINE #include <iostream> NEW_LINE using namespace std ; static int findOptimalSolution ( int a [ ] , int N ) { sort ( a , a + N ) ; int points = 0 ; for ( int i = 0 ; i < N ; i ++ ) { points += a [ i ] * i ; } return points ; } int main ( ) { int a [ ] = { 1 , 4 , 2 , 3 , 9 } ; int N = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << ( findOptimalSolution ( a , N ) ) ; return 0 ; }
Minimum number of towers required such that every house is in the range of at least one tower | C ++ implementation of above approach ; Function to count the number of tower ; first we sort the house numbers ; for count number of towers ; for iterate all houses ; count number of towers ; find find the middle location ; traverse till middle location ; this is point to middle house where we insert the tower ; now find the last location ; traverse till last house of the range ; return the number of tower ; Driver code ; given elements ; print number of towers
#include <bits/stdc++.h> NEW_LINE using namespace std ; int number_of_tower ( int house [ ] , int range , int n ) { sort ( house , house + n ) ; int numOfTower = 0 ; int i = 0 ; while ( i < n ) { numOfTower ++ ; int loc = house [ i ] + range ; while ( i < n && house [ i ] <= loc ) i ++ ; -- i ; loc = house [ i ] + range ; while ( i < n && house [ i ] <= loc ) i ++ ; } return numOfTower ; } int main ( ) { int house [ ] = { 7 , 2 , 4 , 6 , 5 , 9 , 12 , 11 } ; int range = 2 ; int n = sizeof ( house ) / sizeof ( house [ 0 ] ) ; cout << number_of_tower ( house , range , n ) ; }
Check if the characters of a given string are in alphabetical order | C ++ implementation of above approach ; Function that checks whether the string is in alphabetical order or not ; length of the string ; create a character array of the length of the string ; assign the string to character array ; sort the character array ; check if the character array is equal to the string or not ; Driver code ; check whether the string is in alphabetical order or not
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isAlphabaticOrder ( string s ) { int n = s . length ( ) ; char c [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { c [ i ] = s [ i ] ; } sort ( c , c + n ) ; for ( int i = 0 ; i < n ; i ++ ) if ( c [ i ] != s [ i ] ) return false ; return true ; } int main ( ) { string s = " aabbbcc " ; if ( isAlphabaticOrder ( s ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Sort first k values in ascending order and remaining n | C ++ program to sort first k elements in increasing order and remaining n - k elements in decreasing ; function to sort the array ; Sort first k elements in ascending order ; Sort remaining n - k elements in descending order ; Driver code ; Our arr contains 8 elements
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printOrder ( int arr [ ] , int n , int k ) { sort ( arr , arr + k ) ; sort ( arr + k , arr + n , greater < int > ( ) ) ; } int main ( ) { int arr [ ] = { 5 , 4 , 6 , 2 , 1 , 3 , 8 , 9 , -1 } ; int k = 4 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printOrder ( arr , n , k ) ; for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; return 0 ; }
Maximum width of a binary tree | A queue based C ++ program to find maximum width of a Binary Tree ; A binary tree node has data , pointer to left child and a pointer to right child ; Function to find the maximum width of the tree using level order traversal ; Base case ; Initialize result ; Do Level order traversal keeping track of number of nodes at every level . ; Get the size of queue when the level order traversal for one level finishes ; Update the maximum node count value ; Iterate for all the nodes in the queue currently ; Dequeue an node from queue ; Enqueue left and right children of dequeued node ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left ; struct Node * right ; } ; int maxWidth ( struct Node * root ) { if ( root == NULL ) return 0 ; int result = 0 ; queue < Node * > q ; q . push ( root ) ; while ( ! q . empty ( ) ) { int count = q . size ( ) ; result = max ( count , result ) ; while ( count -- ) { Node * temp = q . front ( ) ; q . pop ( ) ; if ( temp -> left != NULL ) q . push ( temp -> left ) ; if ( temp -> right != NULL ) q . push ( temp -> right ) ; } } return result ; } struct Node * newNode ( int data ) { struct Node * node = new Node ; node -> data = data ; node -> left = node -> right = NULL ; return ( node ) ; } int main ( ) { struct Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; root -> right -> right = newNode ( 8 ) ; root -> right -> right -> left = newNode ( 6 ) ; root -> right -> right -> right = newNode ( 7 ) ; cout << " Maximum ▁ width ▁ is ▁ " << maxWidth ( root ) << endl ; return 0 ; }
Merging and Sorting Two Unsorted Stacks | C ++ program to merge to unsorted stacks into a third stack in sorted way . ; Sorts input stack and returns sorted stack . ; pop out the first element ; while temporary stack is not empty and top of stack is greater than temp ; pop from temporary stack and push it to the input stack ; push temp in temporary of stack ; Push contents of both stacks in result ; Sort the result stack . ; main function ; This is the temporary stack
#include <bits/stdc++.h> NEW_LINE using namespace std ; stack < int > sortStack ( stack < int > & input ) { stack < int > tmpStack ; while ( ! input . empty ( ) ) { int tmp = input . top ( ) ; input . pop ( ) ; while ( ! tmpStack . empty ( ) && tmpStack . top ( ) > tmp ) { input . push ( tmpStack . top ( ) ) ; tmpStack . pop ( ) ; } tmpStack . push ( tmp ) ; } return tmpStack ; } stack < int > sortedMerge ( stack < int > & s1 , stack < int > & s2 ) { stack < int > res ; while ( ! s1 . empty ( ) ) { res . push ( s1 . top ( ) ) ; s1 . pop ( ) ; } while ( ! s2 . empty ( ) ) { res . push ( s2 . top ( ) ) ; s2 . pop ( ) ; } return sortStack ( res ) ; } int main ( ) { stack < int > s1 , s2 ; s1 . push ( 34 ) ; s1 . push ( 3 ) ; s1 . push ( 31 ) ; s2 . push ( 1 ) ; s2 . push ( 12 ) ; s2 . push ( 23 ) ; stack < int > tmpStack = sortedMerge ( s1 , s2 ) ; cout << " Sorted ▁ and ▁ merged ▁ stack ▁ : STRNEWLINE " ; while ( ! tmpStack . empty ( ) ) { cout << tmpStack . top ( ) << " ▁ " ; tmpStack . pop ( ) ; } }
Lexicographical concatenation of all substrings of a string | CPP Program to create concatenation of all substrings in lexicographic order . ; Creating an array to store substrings ; finding all substrings of string ; Sort all substrings in lexicographic order ; Concatenating all substrings ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string lexicographicSubConcat ( string s ) { int n = s . length ( ) ; int sub_count = n * ( n + 1 ) / 2 ; string arr [ sub_count ] ; int index = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int len = 1 ; len <= n - i ; len ++ ) arr [ index ++ ] = s . substr ( i , len ) ; sort ( arr , arr + sub_count ) ; string res = " " ; for ( int i = 0 ; i < sub_count ; i ++ ) res += arr [ i ] ; return res ; } int main ( ) { string s = " abc " ; cout << lexicographicSubConcat ( s ) ; return 0 ; }
Insertion Sort by Swapping Elements | Iterative CPP program to sort an array by swapping elements ; Utility function to print a Vector ; Function performs insertion sort on vector V ; Insert V [ i ] into list 0. . i - 1 ; Swap V [ j ] and V [ j - 1 ] ; Decrement j by 1 ; Driver Code
#include <iostream> NEW_LINE #include <vector> NEW_LINE using namespace std ; using Vector = vector < int > ; void printVector ( const Vector & V ) { for ( auto e : V ) { cout << e << " ▁ " ; } cout << endl ; } void insertionSort ( Vector & V ) { int N = V . size ( ) ; int i , j , key ; for ( i = 1 ; i < N ; i ++ ) { j = i ; while ( j > 0 and V [ j ] < V [ j - 1 ] ) { swap ( V [ j ] , V [ j - 1 ] ) ; j -= 1 ; } } } int main ( ) { Vector A = { 9 , 8 , 7 , 5 , 2 , 1 , 2 , 3 } ; cout << " Array : ▁ " << endl ; printVector ( A ) ; cout << " After ▁ Sorting ▁ : " << endl ; insertionSort ( A ) ; printVector ( A ) ; return 0 ; }
Program to sort string in descending order | CPP program to sort a string in descending order using library function ; Driver code ; descOrder ( s ) ; function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void descOrder ( string s ) { sort ( s . begin ( ) , s . end ( ) , greater < char > ( ) ) ; } int main ( ) { string s = " geeksforgeeks " ; return 0 ; }
Sort string of characters | C ++ program to sort a string of characters ; function to print string in sorted order ; Hash array to keep count of characters . Initially count of all charters is initialized to zero . ; Traverse string and increment count of characters ; ' a ' - ' a ' will be 0 , ' b ' - ' a ' will be 1 , so for location of character in count array we wil do str [ i ] - ' a ' . ; Traverse the hash array and print characters ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_CHAR = 26 ; void sortString ( string & str ) { int charCount [ MAX_CHAR ] = { 0 } ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) charCount [ str [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) for ( int j = 0 ; j < charCount [ i ] ; j ++ ) cout << ( char ) ( ' a ' + i ) ; } int main ( ) { string s = " geeksforgeeks " ; sortString ( s ) ; return 0 ; }