codeparrot/xlcost-text-to-code huggingface dataset:
XOR Linked List | C program to implement the above approach ; Structure of a node in XOR linked list ; Stores data value of a node ; Stores XOR of previous pointer and next pointer ; Function to find the XOR of two nodes ; Function to insert a node with given value at given position ; If XOR linked list is empty ; Initialize a new Node ; Stores data value in the node ; Stores XOR of previous and next pointer ; Update pointer of head node ; If the XOR linked list is not empty ; Stores the address of current node ; Stores the address of previous node ; Initialize a new Node ; Update curr node address ; Update new node address ; Update head ; Update data value of current node ; Function to print the middle node ; Stores XOR pointer in current node ; Stores XOR pointer of in previous Node ; Stores XOR pointer of in next node ; Traverse XOR linked list ; Forward traversal ; Update prev ; Update curr ; If the length of the linked list is odd ; If the length of the linked list is even ; Driver Code ; Create following XOR Linked List head -- > 4 a > 7 a > 5
#include <inttypes.h> NEW_LINE #include <stdio.h> NEW_LINE #include <stdlib.h> NEW_LINE struct Node { int data ; struct Node * nxp ; } ; struct Node * XOR ( struct Node * a , struct Node * b ) { return ( struct Node * ) ( ( uintptr_t ) ( a ) ^ ( uintptr_t ) ( b ) ) ; } struct Node * insert ( struct Node * * head , int value ) { if ( * head == NULL ) { struct Node * node = ( struct Node * ) malloc ( sizeof ( struct Node ) ) ; node -> data = value ; node -> nxp = XOR ( NULL , NULL ) ; * head = node ; } else { struct Node * curr = * head ; struct Node * prev = NULL ; struct Node * node = ( struct Node * ) malloc ( sizeof ( struct Node ) ) ; curr -> nxp = XOR ( node , XOR ( NULL , curr -> nxp ) ) ; node -> nxp = XOR ( NULL , curr ) ; * head = node ; node -> data = value ; } return * head ; } int printMiddle ( struct Node * * head , int len ) { int count = 0 ; struct Node * curr = * head ; struct Node * prev = NULL ; struct Node * next ; int middle = ( int ) len / 2 ; while ( count != middle ) { next = XOR ( prev , curr -> nxp ) ; prev = curr ; curr = next ; count ++ ; } if ( len & 1 ) { printf ( " % d " , curr -> data ) ; } else { printf ( " % d ▁ % d " , prev -> data , curr -> data ) ; } } int main ( ) { struct Node * head = NULL ; insert ( & head , 4 ) ; insert ( & head , 7 ) ; insert ( & head , 5 ) ; printMiddle ( & head , 3 ) ; return ( 0 ) ; }
Program to toggle K | C program to toggle K - th bit of a number N ; Function to toggle the kth bit of n ; Driver code
#include <stdio.h> NEW_LINE int toggleBit ( int n , int k ) { return ( n ^ ( 1 << ( k - 1 ) ) ) ; } int main ( ) { int n = 5 , k = 2 ; printf ( " % d STRNEWLINE " , toggleBit ( n , k ) ) ; return 0 ; }
Program to clear K | C program to clear K - th bit of a number N ; Function to clear the kth bit of n ; Driver code
#include <stdio.h> NEW_LINE int clearBit ( int n , int k ) { return ( n & ( ~ ( 1 << ( k - 1 ) ) ) ) ; } int main ( ) { int n = 5 , k = 1 ; printf ( " % d STRNEWLINE " , clearBit ( n , k ) ) ; return 0 ; }
Left Shift and Right Shift Operators in C / C ++ | C ++ Program to demonstrate use of right shift operator ; a = 5 ( 00000101 ) , b = 9 ( 00001001 ) ; The result is 00000010 ; The result is 00000100
#include <stdio.h> NEW_LINE using namespace std ; int main ( ) { unsigned char a = 5 , b = 9 ; printf ( " a > > 1 ▁ = ▁ % d STRNEWLINE " , a >> 1 ) ; printf ( " b > > 1 ▁ = ▁ % d STRNEWLINE " , b >> 1 ) ; return 0 ; }
Left Shift and Right Shift Operators in C / C ++ |  ; shift y by 61 bits left
#include <stdio.h> NEW_LINE int main ( ) { int x = 19 ; unsigned long long y = 19 ; printf ( " x ▁ < < ▁ 1 ▁ = ▁ % d STRNEWLINE " , x << 1 ) ; printf ( " x ▁ > > ▁ 1 ▁ = ▁ % d STRNEWLINE " , x >> 1 ) ; printf ( " y ▁ < < ▁ 61 ▁ = ▁ % lld STRNEWLINE " , y << 61 ) ; return 0 ; }
Left Shift and Right Shift Operators in C / C ++ |
#include <stdio.h> NEW_LINE int main ( ) { int i = 3 ; printf ( " pow ( 2 , ▁ % d ) ▁ = ▁ % d STRNEWLINE " , i , 1 << i ) ; i = 4 ; printf ( " pow ( 2 , ▁ % d ) ▁ = ▁ % d STRNEWLINE " , i , 1 << i ) ; return 0 ; }
Bitwise recursive addition of two integers | C program to do recursive addition of two integers ; If bitwise & is 0 , then there is not going to be any carry . Hence result of XOR is addition . ; Driver code
#include <stdio.h> NEW_LINE int add ( int x , int y ) { int keep = ( x & y ) << 1 ; int res = x ^ y ; if ( keep == 0 ) return res ; add ( keep , res ) ; } int main ( ) { printf ( " % d " , add ( 15 , 38 ) ) ; return 0 ; }
Count total bits in a number | C program to find total bit in given number ; log function in base 2 take only integer part ; Driven program
#include <stdio.h> NEW_LINE #include <math.h> NEW_LINE unsigned countBits ( unsigned int number ) { return ( int ) log2 ( number ) + 1 ; } int main ( ) { unsigned int num = 65 ; printf ( " % d STRNEWLINE " , countBits ( num ) ) ; return 0 ; }
Find the n | Efficient C program to find n - th palindrome ; Construct the nth binary palindrome with the given group number , aux_number and operation type ; No need to insert any bit in the middle ; Length of the final binary representation ; Fill first and last bit as 1 ; Start filling the a [ ] from middle , with the aux_num binary representation ; Get the auxiliary number 's ith bit and  fill around middle ; Insert bit 0 in the middle ; Length of the final binary representation ; Fill first and last bit as 1 ; Start filling the a [ ] from middle , with the aux_num binary representation ; Get the auxiliary number 's ith bit and fill  around middle ; else Insert bit 1 in the middle ; Length of the final binary representation ; Fill first and last bit as 1 ; Start filling the a [ ] from middle , with the aux_num binary representation ; Get the auxiliary number 's ith bit and fill  around middle ; Convert the number to decimal from binary ; Will return the nth binary palindrome number ; Add number of elements in all the groups , until the group of the nth number is found ; Total number of elements until this group ; Element 's offset position in the group ; Finding which bit to be placed in the middle and finding the number , which we will fill from the middle in both directions ; We need to fill this auxiliary number in binary form the middle in both directions ; op = 0 ; Need to Insert 0 at middle ; op = 1 ; Need to Insert 1 at middle ; Driver code ; Function Call
#include <stdio.h> NEW_LINE #define INT_SIZE  32 NEW_LINE int constructNthNumber ( int group_no , int aux_num , int op ) { int a [ INT_SIZE ] = { 0 } ; int num = 0 , len_f ; int i = 0 ; if ( op == 2 ) { len_f = 2 * group_no ; a [ len_f - 1 ] = a [ 0 ] = 1 ; while ( aux_num ) { a [ group_no + i ] = a [ group_no - 1 - i ] = aux_num & 1 ; aux_num = aux_num >> 1 ; i ++ ; } } else if ( op == 0 ) { len_f = 2 * group_no + 1 ; a [ len_f - 1 ] = a [ 0 ] = 1 ; a [ group_no ] = 0 ; while ( aux_num ) { a [ group_no + 1 + i ] = a [ group_no - 1 - i ] = aux_num & 1 ; aux_num = aux_num >> 1 ; i ++ ; } } { len_f = 2 * group_no + 1 ; a [ len_f - 1 ] = a [ 0 ] = 1 ; a [ group_no ] = 1 ; while ( aux_num ) { a [ group_no + 1 + i ] = a [ group_no - 1 - i ] = aux_num & 1 ; aux_num = aux_num >> 1 ; i ++ ; } } for ( i = 0 ; i < len_f ; i ++ ) num += ( 1 << i ) * a [ i ] ; return num ; } int getNthNumber ( int n ) { int group_no = 0 , group_offset ; int count_upto_group = 0 , count_temp = 1 ; int op , aux_num ; while ( count_temp < n ) { group_no ++ ; count_upto_group = count_temp ; count_temp += 3 * ( 1 << ( group_no - 1 ) ) ; } group_offset = n - count_upto_group - 1 ; if ( ( group_offset + 1 ) <= ( 1 << ( group_no - 1 ) ) ) { aux_num = group_offset ; } else { if ( ( ( group_offset + 1 ) - ( 1 << ( group_no - 1 ) ) ) % 2 ) else aux_num = ( ( group_offset ) - ( 1 << ( group_no - 1 ) ) ) / 2 ; } return constructNthNumber ( group_no , aux_num , op ) ; } int main ( ) { int n = 9 ; printf ( " % d " , getNthNumber ( n ) ) ; return 0 ; }
Toggle all the bits of a number except k | C program to toggle all bits except kth bit ; Returns a number with all bit toggled in n except k - th bit ; 1 ) Toggle k - th bit by doing n ^ ( 1 << k ) 2 ) Toggle all bits of the modified number ; Driver code
#include <stdio.h> NEW_LINE unsigned int toggleAllExceptK ( unsigned int n , unsigned int k ) { return ~ ( n ^ ( 1 << k ) ) ; } int main ( ) { unsigned int n = 4294967295 ; unsigned int k = 0 ; printf ( " % u " , toggleAllExceptK ( n , k ) ) ; return 0 ; }
Operators in C | Set 2 ( Relational and Logical Operators ) | C program to demonstrate working of relational operators ; greater than example ; greater than equal to ; less than example ; lesser than equal to ; equal to ; not equal to
#include <stdio.h> NEW_LINE int main ( ) { int a = 10 , b = 4 ; if ( a > b ) printf ( " a ▁ is ▁ greater ▁ than ▁ b STRNEWLINE " ) ; else printf ( " a ▁ is ▁ less ▁ than ▁ or ▁ equal ▁ to ▁ b STRNEWLINE " ) ; if ( a >= b ) printf ( " a ▁ is ▁ greater ▁ than ▁ or ▁ equal ▁ to ▁ b STRNEWLINE " ) ; else printf ( " a ▁ is ▁ lesser ▁ than ▁ b STRNEWLINE " ) ; if ( a < b ) printf ( " a ▁ is ▁ less ▁ than ▁ b STRNEWLINE " ) ; else printf ( " a ▁ is ▁ greater ▁ than ▁ or ▁ equal ▁ to ▁ b STRNEWLINE " ) ; if ( a <= b ) printf ( " a ▁ is ▁ lesser ▁ than ▁ or ▁ equal ▁ to ▁ b STRNEWLINE " ) ; else printf ( " a ▁ is ▁ greater ▁ than ▁ b STRNEWLINE " ) ; if ( a == b ) printf ( " a ▁ is ▁ equal ▁ to ▁ b STRNEWLINE " ) ; else printf ( " a ▁ and ▁ b ▁ are ▁ not ▁ equal STRNEWLINE " ) ; if ( a != b ) printf ( " a ▁ is ▁ not ▁ equal ▁ to ▁ b STRNEWLINE " ) ; else printf ( " a ▁ is ▁ equal ▁ b STRNEWLINE " ) ; return 0 ; }
Operators in C | Set 2 ( Relational and Logical Operators ) | C program to demonstrate working of logical operators ; logical AND example ; logical OR example ; logical NOT example
#include <stdio.h> NEW_LINE int main ( ) { int a = 10 , b = 4 , c = 10 , d = 20 ; if ( a > b && c == d ) printf ( " a ▁ is ▁ greater ▁ than ▁ b ▁ AND ▁ c ▁ is ▁ equal ▁ to ▁ d STRNEWLINE " ) ; else printf ( " AND ▁ condition ▁ not ▁ satisfied STRNEWLINE " ) ; if ( a > b c == d ) printf ( " a ▁ is ▁ greater ▁ than ▁ b ▁ OR ▁ c ▁ is ▁ equal ▁ to ▁ d STRNEWLINE " ) ; else printf ( " Neither ▁ a ▁ is ▁ greater ▁ than ▁ b ▁ nor ▁ c ▁ is ▁ equal ▁ " " ▁ to ▁ d STRNEWLINE " ) ; if ( ! a ) printf ( " a ▁ is ▁ zero STRNEWLINE " ) ; else printf ( " a ▁ is ▁ not ▁ zero " ) ; return 0 ; }
Operators in C | Set 2 ( Relational and Logical Operators ) |
#include <stdbool.h> NEW_LINE #include <stdio.h> NEW_LINE int main ( ) { int a = 10 , b = 4 ; bool res = ( ( a != b ) || printf ( " GeeksQuiz " ) ) ; return 0 ; }
How to swap two bits in a given integer ? | C program to swap bits in an integer ; This function swaps bit at positions p1 and p2 in an integer n ; Move p1 'th to rightmost side  ; Move p2 'th to rightmost side  ; XOR the two bits ; Put the xor bit back to their original positions ; XOR ' x ' with the original number so that the two sets are swapped ; Driver program to test above function
#include <stdio.h> NEW_LINE int swapBits ( unsigned int n , unsigned int p1 , unsigned int p2 ) { unsigned int bit1 = ( n >> p1 ) & 1 ; unsigned int bit2 = ( n >> p2 ) & 1 ; unsigned int x = ( bit1 ^ bit2 ) ; x = ( x << p1 ) | ( x << p2 ) ; unsigned int result = n ^ x ; } int main ( ) { int res = swapBits ( 28 , 0 , 3 ) ; printf ( " Result ▁ = ▁ % d ▁ " , res ) ; return 0 ; }
Write a function that returns 2 for input 1 and returns 1 for 2 |
int invert ( int x ) { if ( x == 1 ) return 2 ; else return 1 ; }
Write a function that returns 2 for input 1 and returns 1 for 2 |
int invertSub ( int x ) { return ( 3 - x ) ; }
Bitwise Operators in C / C ++ |  ; Function to return the only odd occurring element ; Driver Method
#include <stdio.h> NEW_LINE int findOdd ( int arr [ ] , int n ) { int res = 0 , i ; for ( i = 0 ; i < n ; i ++ ) res ^= arr [ i ] ; return res ; } int main ( void ) { int arr [ ] = { 12 , 12 , 14 , 90 , 14 , 14 , 14 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printf ( " The ▁ odd ▁ occurring ▁ element ▁ is ▁ % d ▁ " , findOdd ( arr , n ) ) ; return 0 ; }
Bitwise Operators in C / C ++ |
#include <stdio.h> NEW_LINE int main ( ) { int x = 2 , y = 5 ; ( x & y ) ? printf ( " True ▁ " ) : printf ( " False ▁ " ) ; ( x && y ) ? printf ( " True ▁ " ) : printf ( " False ▁ " ) ; return 0 ; }
Hamming code Implementation in C / C ++ | C program for the above approach ; Store input bits ; Store hamming code ; Function to calculate bit for ith position ; Traverse to store Hamming Code ; If current boit is 1 ; Update i ; Function to calculate hamming code ; Find msg bits having set bit at x 'th position of number ; Traverse the msgBits ; Update the code ; Update the code [ i ] to the input character at index j ; Traverse and update the hamming code ; Find current position ; Find value at current position ; Update the code ; Print the Hamming Code ; Driver Code ; Given input message Bit ; Function Call
#include <math.h> NEW_LINE #include <stdio.h> NEW_LINE int input [ 32 ] ; int code [ 32 ] ; int ham_calc ( int , int ) ; void solve ( int input [ ] , int ) ; int ham_calc ( int position , int c_l ) { int count = 0 , i , j ; i = position - 1 ; while ( i < c_l ) { for ( j = i ; j < i + position ; j ++ ) { if ( code [ j ] == 1 ) count ++ ; } i = i + 2 * position ; } if ( count % 2 == 0 ) return 0 ; else return 1 ; } void solve ( int input [ ] , int n ) { int i , p_n = 0 , c_l , j , k ; i = 0 ; while ( n > ( int ) pow ( 2 , i ) - ( i + 1 ) ) { p_n ++ ; i ++ ; } c_l = p_n + n ; j = k = 0 ; for ( i = 0 ; i < c_l ; i ++ ) { if ( i == ( ( int ) pow ( 2 , k ) - 1 ) ) { code [ i ] = 0 ; k ++ ; } else { code [ i ] = input [ j ] ; j ++ ; } } for ( i = 0 ; i < p_n ; i ++ ) { int position = ( int ) pow ( 2 , i ) ; int value = ham_calc ( position , c_l ) ; code [ position - 1 ] = value ; } printf ( " The generated Code Word is : " for ( i = 0 ; i < c_l ; i ++ ) { printf ( " % d " , code [ i ] ) ; } } void main ( ) { input [ 0 ] = 0 ; input [ 1 ] = 1 ; input [ 2 ] = 1 ; input [ 3 ] = 1 ; int N = 4 ; solve ( input , N ) ; }
First non | CPP program to find first non - repeating character using 1D array and one traversal . ; The function returns index of the first non - repeating character in a string . If all characters are repeating then returns INT_MAX ; Initialize all characters as absent . ; After below loop , the value of arr [ x ] is going to be index of of x if x appears only once . Else the value is going to be either - 1 or - 2. ; If this character occurs only once and appears before the current result , then update the result ; Driver program to test above function
#include <limits.h> NEW_LINE #include <stdio.h> NEW_LINE #include <math.h> NEW_LINE #define NO_OF_CHARS  256 NEW_LINE int firstNonRepeating ( char * str ) { int arr [ NO_OF_CHARS ] ; for ( int i = 0 ; i < NO_OF_CHARS ; i ++ ) arr [ i ] = -1 ; for ( int i = 0 ; str [ i ] ; i ++ ) { if ( arr [ str [ i ] ] == -1 ) arr [ str [ i ] ] = i ; else arr [ str [ i ] ] = -2 ; } int res = INT_MAX ; for ( int i = 0 ; i < NO_OF_CHARS ; i ++ ) if ( arr [ i ] >= 0 ) res = min ( res , arr [ i ] ) ; return res ; } int main ( ) { char str [ ] = " geeksforgeeks " ; int index = firstNonRepeating ( str ) ; if ( index == INT_MAX ) printf ( " Either ▁ all ▁ characters ▁ are ▁ " " repeating ▁ or ▁ string ▁ is ▁ empty " ) ; else printf ( " First ▁ non - repeating ▁ character " " ▁ is ▁ % c " , str [ index ] ) ; return 0 ; }
Triacontagon Number | C program for above approach ; Finding the nth triacontagonal Number ; Driver program to test above function
#include <stdio.h> NEW_LINE #include <stdlib.h> NEW_LINE int triacontagonalNum ( int n ) { return ( 28 * n * n - 26 * n ) / 2 ; } int main ( ) { int n = 3 ; printf ( "3rd ▁ triacontagonal ▁ Number ▁ is ▁ = ▁ % d " , triacontagonalNum ( n ) ) ; return 0 ; }
Hexacontagon Number | C program for above approach ; Finding the nth hexacontagon Number ; Driver program to test above function
#include <stdio.h> NEW_LINE #include <stdlib.h> NEW_LINE int hexacontagonNum ( int n ) { return ( 58 * n * n - 56 * n ) / 2 ; } int main ( ) { int n = 3 ; printf ( "3rd ▁ hexacontagon ▁ Number ▁ is ▁ = ▁ % d " , hexacontagonNum ( n ) ) ; return 0 ; }
Enneacontagon Number | C program for above approach ; Finding the nth enneacontagon Number ; Driver program to test above function
#include <stdio.h> NEW_LINE #include <stdlib.h> NEW_LINE int enneacontagonNum ( int n ) { return ( 88 * n * n - 86 * n ) / 2 ; } int main ( ) { int n = 3 ; printf ( "3rd ▁ enneacontagon ▁ Number ▁ is ▁ = ▁ % d " , enneacontagonNum ( n ) ) ; return 0 ; }
Triacontakaidigon Number | C program for above approach ; Finding the nth triacontakaidigon Number ; Driver program to test above function
#include <stdio.h> NEW_LINE #include <stdlib.h> NEW_LINE int triacontakaidigonNum ( int n ) { return ( 30 * n * n - 28 * n ) / 2 ; } int main ( ) { int n = 3 ; printf ( "3rd ▁ triacontakaidigon ▁ Number ▁ is ▁ = ▁ % d " , triacontakaidigonNum ( n ) ) ; return 0 ; }
Icosihexagonal Number | C program for above approach ; Finding the nth Icosihexagonal Number ; Driver program to test above function
#include <stdio.h> NEW_LINE #include <stdlib.h> NEW_LINE int IcosihexagonalNum ( int n ) { return ( 24 * n * n - 22 * n ) / 2 ; } int main ( ) { int n = 3 ; printf ( "3rd ▁ Icosihexagonal ▁ Number ▁ is ▁ = ▁ % d " , IcosihexagonalNum ( n ) ) ; return 0 ; }
Icosikaioctagon or Icosioctagon Number | C program for above approach ; Finding the nth icosikaioctagonal Number ; Driver program to test above function
#include <stdio.h> NEW_LINE #include <stdlib.h> NEW_LINE int icosikaioctagonalNum ( int n ) { return ( 26 * n * n - 24 * n ) / 2 ; } int main ( ) { int n = 3 ; printf ( "3rd ▁ icosikaioctagonal ▁ Number ▁ is ▁ = ▁ % d " , icosikaioctagonalNum ( n ) ) ; return 0 ; }
Octacontagon Number | C program for above approach ; Finding the nth octacontagon Number ; Driver program to test above function
#include <stdio.h> NEW_LINE #include <stdlib.h> NEW_LINE int octacontagonNum ( int n ) { return ( 78 * n * n - 76 * n ) / 2 ; } int main ( ) { int n = 3 ; printf ( "3rd ▁ octacontagon ▁ Number ▁ is ▁ = ▁ % d " , octacontagonNum ( n ) ) ; return 0 ; }
Hectagon Number | C program for above approach ; Finding the nth hectagon Number ; Driver program to test above function
#include <stdio.h> NEW_LINE #include <stdlib.h> NEW_LINE int hectagonNum ( int n ) { return ( 98 * n * n - 96 * n ) / 2 ; } int main ( ) { int n = 3 ; printf ( "3rd ▁ hectagon ▁ Number ▁ is ▁ = ▁ % d " , hectagonNum ( n ) ) ; return 0 ; }
Tetracontagon Number | C program for above approach ; Finding the nth tetracontagon Number ; Driver program to test above function
#include <stdio.h> NEW_LINE #include <stdlib.h> NEW_LINE int tetracontagonNum ( int n ) { return ( 38 * n * n - 36 * n ) / 2 ; } int main ( ) { int n = 3 ; printf ( "3rd ▁ tetracontagon ▁ Number ▁ is ▁ = ▁ % d " , tetracontagonNum ( n ) ) ; return 0 ; }
Search an element in a reverse sorted array | C program for the above approach ; Function to search if element X is present in reverse sorted array ; Store the first index of the subarray in which X lies ; Store the last index of the subarray in which X lies ; Store the middle index of the subarray ; Check if value at middle index of the subarray equal to X ; Element is found ; If X is smaller than the value at middle index of the subarray ; Search in right half of subarray ; Search in left half of subarray ; If X not found ; Driver Code
#include <stdio.h> NEW_LINE int binarySearch ( int arr [ ] , int N , int X ) { int start = 0 ; int end = N ; while ( start <= end ) { int mid = start + ( end - start ) / 2 ; if ( X == arr [ mid ] ) { return mid ; } else if ( X < arr [ mid ] ) { start = mid + 1 ; } else { end = mid - 1 ; } } return -1 ; } int main ( ) { int arr [ ] = { 5 , 4 , 3 , 2 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int X = 4 ; int res = binarySearch ( arr , N , X ) ; printf ( " ▁ % d ▁ " , res ) ; return 0 ; }
Pancake sorting | C program to sort array using pancake sort ; Reverses arr [ 0. . i ] ; Returns index of the maximum element in arr [ 0. . n - 1 ] ; The main function that sorts given array using flip operations ; Start from the complete array and one by one reduce current size by one ; Find index of the maximum element in arr [ 0. . curr_size - 1 ] ; Move the maximum element to end of current array if it 's not already  at the end ; To move at the end , first move maximum number to beginning ; Now move the maximum number to end by reversing current array ; A utility function to print n array of size n ; Driver program to test above function
#include <stdio.h> NEW_LINE #include <stdlib.h> NEW_LINE void flip ( int arr [ ] , int i ) { int temp , start = 0 ; while ( start < i ) { temp = arr [ start ] ; arr [ start ] = arr [ i ] ; arr [ i ] = temp ; start ++ ; i -- ; } } int findMax ( int arr [ ] , int n ) { int mi , i ; for ( mi = 0 , i = 0 ; i < n ; ++ i ) if ( arr [ i ] > arr [ mi ] ) mi = i ; return mi ; } void pancakeSort ( int * arr , int n ) { for ( int curr_size = n ; curr_size > 1 ; -- curr_size ) { int mi = findMax ( arr , curr_size ) ; if ( mi != curr_size - 1 ) { flip ( arr , mi ) ; flip ( arr , curr_size - 1 ) ; } } } void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; ++ i ) printf ( " % d ▁ " , arr [ i ] ) ; } int main ( ) { int arr [ ] = { 23 , 10 , 20 , 11 , 12 , 6 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; pancakeSort ( arr , n ) ; puts ( " Sorted ▁ Array ▁ " ) ; printArray ( arr , n ) ; return 0 ; }