codeparrot/xlcost-text-to-code huggingface dataset:
Count triplets in a sorted doubly linked list whose product is equal to a given value x | C # implementation to count triplets in a sorted doubly linked list whose sum is equal to a given value ' x ' ; Represents node of a doubly linked list ; function to count triplets in a sorted doubly linked list whose sum is equal to a given value ' x ' ; generate all possible triplets ; if elements in the current triplet sum up to ' x ' ; increment count ; required count of triplets ; A utility function to insert a new node at the beginning of doubly linked list ; allocate node ; Driver code ; start with an empty doubly linked list ; insert values in sorted order
using System ; public class Node { public int data ; public Node prev , next ; public Node ( int val ) { data = val ; prev = null ; next = null ; } } class GFG { static int countTriplets ( Node head , int x ) { Node ptr1 , ptr2 , ptr3 ; int count = 0 ; for ( ptr1 = head ; ptr1 != null ; ptr1 = ptr1 . next ) for ( ptr2 = ptr1 . next ; ptr2 != null ; ptr2 = ptr2 . next ) for ( ptr3 = ptr2 . next ; ptr3 != null ; ptr3 = ptr3 . next ) if ( ( ptr1 . data * ptr2 . data * ptr3 . data ) == x ) count ++ ; return count ; } static Node insert ( Node head , int val ) { Node temp = new Node ( val ) ; if ( head == null ) head = temp ; else { temp . next = head ; head . prev = temp ; head = temp ; } return head ; } public static void Main ( String [ ] args ) { Node head = null ; head = insert ( head , 9 ) ; head = insert ( head , 8 ) ; head = insert ( head , 6 ) ; head = insert ( head , 5 ) ; head = insert ( head , 4 ) ; head = insert ( head , 2 ) ; head = insert ( head , 1 ) ; int x = 8 ; Console . WriteLine ( " count ▁ = ▁ " + countTriplets ( head , x ) ) ; } }
Sort only non | C # program to sort only non primes ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Function to print the array such that only non primes are sorted ; vector v will store all non prime numbers ; If not prime , insert into vector ; sorting vector of non primes ; print the required array ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static bool [ ] prime = new bool [ 100006 ] ; static void SieveOfEratosthenes ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) prime [ i ] = true ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } } static void sortedArray ( int [ ] arr , int n ) { SieveOfEratosthenes ( 100005 ) ; List < int > v = new List < int > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( prime [ arr [ i ] ] == false ) v . Add ( arr [ i ] ) ; } v . Sort ( ) ; int j = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( prime [ arr [ i ] ] == true ) Console . Write ( arr [ i ] + " ▁ " ) ; else { Console . Write ( v [ j ] + " ▁ " ) ; j ++ ; } } } public static void Main ( ) { int n = 6 ; int [ ] arr = { 100 , 11 , 500 , 2 , 17 , 1 } ; sortedArray ( arr , n ) ; } }
Sum of all nodes in a binary tree | C # Program to print sum of all the elements of a binary tree ; utility that allocates a new Node with the given key ; Function to find sum of all the elements ; Driver Code
using System ; public class GFG { public class Node { public int key ; public Node left , right ; } public static Node newNode ( int key ) { Node node = new Node ( ) ; node . key = key ; node . left = node . right = null ; return ( node ) ; } public static int addBT ( Node root ) { if ( root == null ) { return 0 ; } return ( root . key + addBT ( root . left ) + addBT ( root . right ) ) ; } public static void Main ( string [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; root . right . left . right = newNode ( 8 ) ; int sum = addBT ( root ) ; Console . WriteLine ( " Sum ▁ of ▁ all ▁ the ▁ elements ▁ is : ▁ " + sum ) ; } }
Maximum sum of absolute difference of any permutation | C # implementation of above algorithm ; final sequence stored in the vector ; sort the original array so that we can retrieve the large elements from the end of array elements ; In this loop first we will insert one smallest element not entered till that time in final sequence and then enter a highest element ( not entered till that time ) in final sequence so that we have large difference value . This process is repeated till all array has completely entered in sequence . Here , we have loop till n / 2 because we are inserting two elements at a time in loop . ; If there are odd elements , push the middle element at the end . ; variable to store the maximum sum of absolute difference ; In this loop absolute difference of elements for the final sequence is calculated . ; absolute difference of last element and 1 st element ; return the value ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int MaxSumDifference ( int [ ] a , int n ) { List < int > finalSequence = new List < int > ( ) ; Array . Sort ( a ) ; for ( int i = 0 ; i < n / 2 ; ++ i ) { finalSequence . Add ( a [ i ] ) ; finalSequence . Add ( a [ n - i - 1 ] ) ; } if ( n % 2 != 0 ) finalSequence . Add ( a [ n / 2 ] ) ; int MaximumSum = 0 ; for ( int i = 0 ; i < n - 1 ; ++ i ) { MaximumSum = MaximumSum + Math . Abs ( finalSequence [ i ] - finalSequence [ i + 1 ] ) ; } MaximumSum = MaximumSum + Math . Abs ( finalSequence [ n - 1 ] - finalSequence [ 0 ] ) ; return MaximumSum ; } public static void Main ( ) { int [ ] a = { 1 , 2 , 4 , 8 } ; int n = a . Length ; Console . WriteLine ( MaxSumDifference ( a , n ) ) ; } }
Minimum swaps required to bring all elements less than or equal to k together | C # program to find minimum swaps required to club all elements less than or equals to k together ; Utility function to find minimum swaps required to club all elements less than or equals to k together ; Find count of elements which are less than equals to k ; Find unwanted elements in current window of size ' count ' ; Initialize answer with ' bad ' value of current window ; Decrement count of previous window ; Increment count of current window ; Update ans if count of ' bad ' is less in current window ; Driver code
using System ; class GFG { static int minSwap ( int [ ] arr , int n , int k ) { int count = 0 ; for ( int i = 0 ; i < n ; ++ i ) if ( arr [ i ] <= k ) ++ count ; int bad = 0 ; for ( int i = 0 ; i < count ; ++ i ) if ( arr [ i ] > k ) ++ bad ; int ans = bad ; for ( int i = 0 , j = count ; j < n ; ++ i , ++ j ) { if ( arr [ i ] > k ) -- bad ; if ( arr [ j ] > k ) ++ bad ; ans = Math . Min ( ans , bad ) ; } return ans ; } public static void Main ( ) { int [ ] arr = { 2 , 1 , 5 , 6 , 3 } ; int n = arr . Length ; int k = 3 ; Console . WriteLine ( minSwap ( arr , n , k ) ) ; int [ ] arr1 = { 2 , 7 , 9 , 5 , 8 , 7 , 4 } ; n = arr1 . Length ; k = 5 ; Console . WriteLine ( minSwap ( arr1 , n , k ) ) ; } }
Rearrange positive and negative numbers using inbuilt sort function | C # implementation of the above approach ; Rearrange the array with all negative integers on left and positive integers on right use recursion to split the array with first element as one half and the rest array as another and then merge it with head of the array in each step ; exit condition ; rearrange the array except the first element in each recursive call ; If the first element of the array is positive , then right - rotate the array by one place first and then reverse the merged array . ; Driver code
using System ; class GFG { static void printArray ( int [ ] array , int length ) { Console . Write ( " [ " ) ; for ( int i = 0 ; i < length ; i ++ ) { Console . Write ( array [ i ] ) ; if ( i < ( length - 1 ) ) Console . Write ( " , " ) ; else Console . Write ( " ] STRNEWLINE " ) ; } } static void reverse ( int [ ] array , int start , int end ) { while ( start < end ) { int temp = array [ start ] ; array [ start ] = array [ end ] ; array [ end ] = temp ; start ++ ; end -- ; } } static void rearrange ( int [ ] array , int start , int end ) { if ( start == end ) return ; rearrange ( array , ( start + 1 ) , end ) ; if ( array [ start ] >= 0 ) { reverse ( array , ( start + 1 ) , end ) ; reverse ( array , start , end ) ; } } public static void Main ( string [ ] args ) { int [ ] array = { - 12 , - 11 , - 13 , - 5 , - 6 , 7 , 5 , 3 , 6 } ; int length = array . Length ; int countNegative = 0 ; for ( int i = 0 ; i < length ; i ++ ) { if ( array [ i ] < 0 ) countNegative ++ ; } Console . Write ( " array : ▁ " ) ; printArray ( array , length ) ; rearrange ( array , 0 , ( length - 1 ) ) ; reverse ( array , countNegative , ( length - 1 ) ) ; Console . Write ( " rearranged ▁ array : ▁ " ) ; printArray ( array , length ) ; } }
Efficiently merging two sorted arrays with O ( 1 ) extra space |  ; Length of first arr1 ; Length of second arr2 ; Now traverse the array1 and if arr2 first element is less than arr1 then swap ; Swap ; We will store the firstElement of array2 and left shift all the element and store the firstElement in arr2 [ k - 1 ] ; Read the arr1 ; Read the arr2 ; Driver Code
using System ; class GFG { public static void mergeArray ( int [ ] arr1 , int [ ] arr2 ) { int n = arr1 . Length ; int m = arr2 . Length ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr1 [ i ] > arr2 [ 0 ] ) { int temp = arr1 [ i ] ; arr1 [ i ] = arr2 [ 0 ] ; arr2 [ 0 ] = temp ; int firstElement = arr2 [ 0 ] ; int k ; for ( k = 1 ; k < m && arr2 [ k ] < firstElement ; k ++ ) { arr2 [ k - 1 ] = arr2 [ k ] ; } arr2 [ k - 1 ] = firstElement ; } } foreach ( int i in arr1 ) { Console . Write ( i + " ▁ " ) ; } Console . WriteLine ( ) ; foreach ( int i in arr2 ) { Console . Write ( i + " ▁ " ) ; } } static public void Main ( ) { int [ ] arr1 = { 1 , 3 , 5 , 7 } ; int [ ] arr2 = { 0 , 2 , 6 , 8 , 9 } ; mergeArray ( arr1 , arr2 ) ; } }
Maximum product of subsequence of size k | C # program to find maximum possible product of sub - sequence of size k from given array of n integers ; Function to find maximum possible product ; sorting given input array ; variable to store final product of all element of sub - sequence of size k ; CASE I If max element is 0 and k is odd then max product will be 0 ; CASE II If all elements are negative and k is odd then max product will be product of rightmost - subarray of size k ; else i is current left pointer index ; j is current right pointer index ; CASE III if k is odd and rightmost element in sorted array is positive then it must come in subsequence Multiplying A [ j ] with product and correspondingly changing j ; CASE IV Now k is even Now we deal with pairs Each time a pair is multiplied to product i . e . . two elements are added to subsequence each time Effectively k becomes half Hence , k >>= 1 means k /= 2 ; Now finding k corresponding pairs to get maximum possible value of product ; product from left pointers ; product from right pointers ; Taking the max product from two choices Correspondingly changing the pointer 's position ; Finally return product ; driver program
using System ; class GFG { static int maxProductSubarrayOfSizeK ( int [ ] A , int n , int k ) { Array . Sort ( A ) ; int product = 1 ; int i ; if ( A [ n - 1 ] == 0 && k % 2 != 0 ) return 0 ; if ( A [ n - 1 ] <= 0 && k % 2 != 0 ) { for ( i = n - 1 ; i >= n - k ; i -- ) product *= A [ i ] ; return product ; } i = 0 ; int j = n - 1 ; if ( k % 2 != 0 ) { product *= A [ j ] ; j -- ; k -- ; } k >>= 1 ; for ( int itr = 0 ; itr < k ; itr ++ ) { int left_product = A [ i ] * A [ i + 1 ] ; int right_product = A [ j ] * A [ j - 1 ] ; if ( left_product > right_product ) { product *= left_product ; i += 2 ; } else { product *= right_product ; j -= 2 ; } } return product ; } public static void Main ( ) { int [ ] A = { 1 , 2 , - 1 , - 3 , - 6 , 4 } ; int n = A . Length ; int k = 4 ; Console . WriteLine ( maxProductSubarrayOfSizeK ( A , n , k ) ) ; } }
Reorder an array according to given indexes | C # to find positions of zeroes flipping which produces maximum number of consecutive 1 's ; Method to reorder elements of arr [ ] according to index [ ] ; arr [ i ] should be present at index [ i ] index ; Copy temp [ ] to arr [ ] ; Driver method to test the above function
using System ; public class Test { static int [ ] arr = new int [ ] { 50 , 40 , 70 , 60 , 90 } ; static int [ ] index = new int [ ] { 3 , 0 , 4 , 1 , 2 } ; static void reorder ( ) { int [ ] temp = new int [ arr . Length ] ; for ( int i = 0 ; i < arr . Length ; i ++ ) temp [ index [ i ] ] = arr [ i ] ; for ( int i = 0 ; i < arr . Length ; i ++ ) { arr [ i ] = temp [ i ] ; index [ i ] = i ; } } public static void Main ( ) { reorder ( ) ; Console . WriteLine ( " Reordered ▁ array ▁ is : ▁ " ) ; Console . WriteLine ( string . Join ( " , " , arr ) ) ; Console . WriteLine ( " Modified ▁ Index ▁ array ▁ is : " ) ; Console . WriteLine ( string . Join ( " , " , index ) ) ; } }
Program to sort an array of strings using Selection Sort | C # program to implement selection sort on array of strings ; Sorts an array of strings ; One by one move boundary of unsorted subarray ; Find the minimum element in unsorted array ; If arr [ j ] is smaller than minStr ; Make arr [ j ] as minStr and update min_idx ; Swapping the minimum element found with the first element . ; Driver Code ; Printing the array before sorting ; Printing the array after sorting
using System ; class GFG { static void selectionSort ( string [ ] arr , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { int min_index = i ; string minStr = arr [ i ] ; for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] . CompareTo ( minStr ) != 0 ) { minStr = arr [ j ] ; min_index = j ; } } if ( min_index != i ) { string temp = arr [ min_index ] ; arr [ min_index ] = arr [ i ] ; arr [ i ] = temp ; } } } static void Main ( ) { string [ ] arr = { " GeeksforGeeks " , " Practice . GeeksforGeeks " , " GeeksQuiz " } ; int n = arr . Length ; Console . WriteLine ( " Given ▁ array ▁ is " ) ; for ( int i = 0 ; i < n ; i ++ ) { Console . WriteLine ( i + " : ▁ " + arr [ i ] ) ; } Console . WriteLine ( ) ; selectionSort ( arr , n ) ; Console . WriteLine ( " Sorted ▁ array ▁ is " ) ; for ( int i = 0 ; i < n ; i ++ ) { Console . WriteLine ( i + " : ▁ " + arr [ i ] ) ; } } }
Reorder an array according to given indexes | A O ( n ) time and O ( 1 ) extra space C # program to sort an array according to given indexes ; Method to reorder elements of arr [ ] according to index [ ] ; Fix all elements one by one ; While index [ i ] and arr [ i ] are not fixed ; Store values of the target ( or correct ) position before placing arr [ i ] there ; Place arr [ i ] at its target ( or correct ) position . Also copy corrected index for new position ; Copy old target values to arr [ i ] and index [ i ] ; Driver method to test the above function
using System ; public class Test { static int [ ] arr = new int [ ] { 50 , 40 , 70 , 60 , 90 } ; static int [ ] index = new int [ ] { 3 , 0 , 4 , 1 , 2 } ; static void reorder ( ) { for ( int i = 0 ; i < arr . Length ; i ++ ) { while ( index [ i ] != i ) { int oldTargetI = index [ index [ i ] ] ; char oldTargetE = ( char ) arr [ index [ i ] ] ; arr [ index [ i ] ] = arr [ i ] ; index [ index [ i ] ] = index [ i ] ; index [ i ] = oldTargetI ; arr [ i ] = oldTargetE ; } } } public static void Main ( ) { reorder ( ) ; Console . WriteLine ( " Reordered ▁ array ▁ is : ▁ " ) ; Console . WriteLine ( String . Join ( " ▁ " , arr ) ) ; Console . WriteLine ( " Modified ▁ Index ▁ array ▁ is : " ) ; Console . WriteLine ( String . Join ( " ▁ " , index ) ) ; } }
Stooge Sort | C # program to implement stooge sort ; Function to implement stooge sort ; If first element is smaller than last , swap them ; If there are more than 2 elements in the array ; Recursively sort first 2 / 3 elements ; Recursively sort last 2 / 3 elements ; Recursively sort first 2 / 3 elements again to confirm ; Driver Code ; Calling Stooge Sort function to sort the array ; Display the sorted array
using System ; class GFG { static void stoogesort ( int [ ] arr , int l , int h ) { if ( l >= h ) return ; if ( arr [ l ] > arr [ h ] ) { int t = arr [ l ] ; arr [ l ] = arr [ h ] ; arr [ h ] = t ; } if ( h - l + 1 > 2 ) { int t = ( h - l + 1 ) / 3 ; stoogesort ( arr , l , h - t ) ; stoogesort ( arr , l + t , h ) ; stoogesort ( arr , l , h - t ) ; } } public static void Main ( ) { int [ ] arr = { 2 , 4 , 5 , 3 , 1 } ; int n = arr . Length ; stoogesort ( arr , 0 , n - 1 ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Reorder an array according to given indexes | C # code to reorder an array according to given indices ; left child in 0 based indexing ; right child in 1 based indexing ; Find largest index from root , left and right child ; Swap arr whenever index is swapped ; Build heap ; Swap the largest element of index ( first element ) with the last element ; Swap arr whenever index is swapped ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int heapSize ; public static void heapify ( int [ ] arr , int [ ] index , int i ) { int largest = i ; int left = 2 * i + 1 ; int right = 2 * i + 2 ; if ( left < heapSize && index [ left ] > index [ largest ] ) { largest = left ; } if ( right < heapSize && index [ right ] > index [ largest ] ) { largest = right ; } if ( largest != i ) { int temp = arr [ largest ] ; arr [ largest ] = arr [ i ] ; arr [ i ] = temp ; temp = index [ largest ] ; index [ largest ] = index [ i ] ; index [ i ] = temp ; heapify ( arr , index , largest ) ; } } public static void heapSort ( int [ ] arr , int [ ] index , int n ) { for ( int i = ( n - 1 ) / 2 ; i >= 0 ; i -- ) { heapify ( arr , index , i ) ; } for ( int i = n - 1 ; i > 0 ; i -- ) { int temp = index [ 0 ] ; index [ 0 ] = index [ i ] ; index [ i ] = temp ; temp = arr [ 0 ] ; arr [ 0 ] = arr [ i ] ; arr [ i ] = temp ; heapSize -- ; heapify ( arr , index , 0 ) ; } } static void Main ( ) { int [ ] arr = { 50 , 40 , 70 , 60 , 90 } ; int [ ] index = { 3 , 0 , 4 , 1 , 2 } ; int n = arr . Length ; heapSize = n ; heapSort ( arr , index , n ) ; Console . WriteLine ( " Reordered ▁ array ▁ is : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; Console . WriteLine ( " Modified ▁ Index ▁ array ▁ is : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( index [ i ] + " ▁ " ) ; } }
Sum of all the parent nodes having child node x | C # implementation to find the sum of all the parent nodes having child node x ; sum ; Node of a binary tree ; function to get a new node ; allocate memory for the node ; put in the data ; function to find the sum of all the parent nodes having child node x ; if root == NULL ; if left or right child of root is ' x ' , then add the root ' s ▁ data ▁ to ▁ ' sum '  ; recursively find the required parent nodes in the left and right subtree ; utility function to find the sum of all the parent nodes having child node x ; required sum of parent nodes ; Driver Code ; binary tree formation 4 ; / \ ; 2 5 ; / \ / \ ; 7 2 2 3
using System ; public class GFG { public static int sum = 0 ; public class Node { public int data ; public Node left , right ; } public static Node getNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . left = newNode . right = null ; return newNode ; } public static void sumOfParentOfX ( Node root , int x ) { if ( root == null ) { return ; } if ( ( root . left != null && root . left . data == x ) || ( root . right != null && root . right . data == x ) ) { sum += root . data ; } sumOfParentOfX ( root . left , x ) ; sumOfParentOfX ( root . right , x ) ; } public static int sumOfParentOfXUtil ( Node root , int x ) { sum = 0 ; sumOfParentOfX ( root , x ) ; return sum ; } public static void Main ( string [ ] args ) { Node root = getNode ( 4 ) ; root . left = getNode ( 2 ) ; root . right = getNode ( 5 ) ; root . left . left = getNode ( 7 ) ; root . left . right = getNode ( 2 ) ; root . right . left = getNode ( 2 ) ; root . right . right = getNode ( 3 ) ; int x = 2 ; Console . WriteLine ( " Sum ▁ = ▁ " + sumOfParentOfXUtil ( root , x ) ) ; } }
Sort even | Program to sort even - placed elements in increasing and odd - placed in decreasing order with constant space complexity ; first odd index ; last index ; if last index is odd ; decrement j to even index ; swapping till half of array ; Sort first half in increasing ; Sort second half in decreasing ; Reverse the second half ; Driver Code
using System ; class GFG { static void bitonicGenerator ( int [ ] arr , int n ) { int i = 1 ; int j = n - 1 ; if ( j % 2 != 0 ) j -- ; while ( i < j ) { arr = swap ( arr , i , j ) ; i += 2 ; j -= 2 ; } Array . Sort ( arr , 0 , ( n + 1 ) / 2 ) ; Array . Sort ( arr , ( n + 1 ) / 2 , n - ( ( n + 1 ) / 2 ) ) ; int low = ( n + 1 ) / 2 , high = n - 1 ; while ( low < high ) { int temp = arr [ low ] ; arr [ low ] = arr [ high ] ; arr [ high ] = temp ; low ++ ; high -- ; } } static int [ ] swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 } ; int n = arr . Length ; bitonicGenerator ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Count minimum number of subsets ( or subsequences ) with consecutive numbers | C # program to find number of subset containing consecutive numbers ; Returns count of subsets with consecutive numbers ; Sort the array so that elements which are consecutive in nature became consecutive in the array . ; Initialize result ; Check if there is beginning of another subset of consecutive number ; Driven Program
using System ; class GFG { static int numofsubset ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int count = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] + 1 != arr [ i + 1 ] ) count ++ ; } return count ; } public static void Main ( ) { int [ ] arr = { 100 , 56 , 5 , 6 , 102 , 58 , 101 , 57 , 7 , 103 , 59 } ; int n = arr . Length ; Console . WriteLine ( numofsubset ( arr , n ) ) ; } }
Gnome Sort | C # Program to implement Gnome Sort ; A function to sort the algorithm using gnome sort ; Driver program to test above functions .
using System ; class GFG { static void gnomeSort ( int [ ] arr , int n ) { int index = 0 ; while ( index < n ) { if ( index == 0 ) index ++ ; if ( arr [ index ] >= arr [ index - 1 ] ) index ++ ; else { int temp = 0 ; temp = arr [ index ] ; arr [ index ] = arr [ index - 1 ] ; arr [ index - 1 ] = temp ; index -- ; } } return ; } public static void Main ( ) { int [ ] arr = { 34 , 2 , 10 , - 9 } ; gnomeSort ( arr , arr . Length ) ; Console . Write ( " Sorted ▁ sequence ▁ after ▁ applying ▁ Gnome ▁ sort : ▁ " ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }