codeparrot/xlcost-text-to-code huggingface dataset:
Merge Sort | C # program for Merge Sort ; Merges two subarrays of [ ] arr . First subarray is arr [ l . . m ] Second subarray is arr [ m + 1. . r ] ; Find sizes of two subarrays to be merged ; Create temp arrays ; Copy data to temp arrays ; Merge the temp arrays Initial indexes of first and second subarrays ; Initial index of merged subarry array ; Copy remaining elements of L [ ] if any ; Copy remaining elements of R [ ] if any ; Main function that sorts arr [ l . . r ] using merge ( ) ; Find the middle point ; Sort first and second halves ; Merge the sorted halves ; A utility function to print array of size n ; Driver code
using System ; class MergeSort { void merge ( int [ ] arr , int l , int m , int r ) { int n1 = m - l + 1 ; int n2 = r - m ; int [ ] L = new int [ n1 ] ; int [ ] R = new int [ n2 ] ; int i , j ; for ( i = 0 ; i < n1 ; ++ i ) L [ i ] = arr [ l + i ] ; for ( j = 0 ; j < n2 ; ++ j ) R [ j ] = arr [ m + 1 + j ] ; i = 0 ; j = 0 ; int k = l ; while ( i < n1 && j < n2 ) { if ( L [ i ] <= R [ j ] ) { arr [ k ] = L [ i ] ; i ++ ; } else { arr [ k ] = R [ j ] ; j ++ ; } k ++ ; } while ( i < n1 ) { arr [ k ] = L [ i ] ; i ++ ; k ++ ; } while ( j < n2 ) { arr [ k ] = R [ j ] ; j ++ ; k ++ ; } } void sort ( int [ ] arr , int l , int r ) { if ( l < r ) { int m = l + ( r - l ) / 2 ; sort ( arr , l , m ) ; sort ( arr , m + 1 , r ) ; merge ( arr , l , m , r ) ; } } static void printArray ( int [ ] arr ) { int n = arr . Length ; for ( int i = 0 ; i < n ; ++ i ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 12 , 11 , 13 , 5 , 6 , 7 } ; Console . WriteLine ( " Given ▁ Array " ) ; printArray ( arr ) ; MergeSort ob = new MergeSort ( ) ; ob . sort ( arr , 0 , arr . Length - 1 ) ; Console . WriteLine ( " STRNEWLINE Sorted ▁ array " ) ; printArray ( arr ) ; } }
Radix Sort | C # implementation of Radix Sort ; A utility function to get maximum value in arr [ ] ; A function to do counting sort of arr [ ] according to the digit represented by exp . ; output array ; initializing all elements of count to 0 ; Store count of occurrences in count [ ] ; Change count [ i ] so that count [ i ] now contains actual position of this digit in output [ ] ; Build the output array ; Copy the output array to arr [ ] , so that arr [ ] now contains sorted numbers according to current digit ; The main function to that sorts arr [ ] of size n using Radix Sort ; Find the maximum number to know number of digits ; Do counting sort for every digit . Note that instead of passing digit number , exp is passed . exp is 10 ^ i where i is current digit number ; A utility function to print an array ; Driver Code ; Function Call
using System ; class GFG { public static int getMax ( int [ ] arr , int n ) { int mx = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] > mx ) mx = arr [ i ] ; return mx ; } public static void countSort ( int [ ] arr , int n , int exp ) { int [ ] output = new int [ n ] ; int i ; int [ ] count = new int [ 10 ] ; for ( i = 0 ; i < 10 ; i ++ ) count [ i ] = 0 ; for ( i = 0 ; i < n ; i ++ ) count [ ( arr [ i ] / exp ) % 10 ] ++ ; for ( i = 1 ; i < 10 ; i ++ ) count [ i ] += count [ i - 1 ] ; for ( i = n - 1 ; i >= 0 ; i -- ) { output [ count [ ( arr [ i ] / exp ) % 10 ] - 1 ] = arr [ i ] ; count [ ( arr [ i ] / exp ) % 10 ] -- ; } for ( i = 0 ; i < n ; i ++ ) arr [ i ] = output [ i ] ; } public static void radixsort ( int [ ] arr , int n ) { int m = getMax ( arr , n ) ; for ( int exp = 1 ; m / exp > 0 ; exp *= 10 ) countSort ( arr , n , exp ) ; } public static void print ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 170 , 45 , 75 , 90 , 802 , 24 , 2 , 66 } ; int n = arr . Length ; radixsort ( arr , n ) ; print ( arr , n ) ; } }
Bucket Sort | C # program to sort an array using bucket sort ; Function to sort arr [ ] of size n using bucket sort ; 1 ) Create n empty buckets ; 2 ) Put array elements in different buckets ; 3 ) Sort individual buckets ; 4 ) Concatenate all buckets into arr [ ] ; Driver code
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static void bucketSort ( float [ ] arr , int n ) { if ( n <= 0 ) return ; List < float > [ ] buckets = new List < float > [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { buckets [ i ] = new List < float > ( ) ; } for ( int i = 0 ; i < n ; i ++ ) { float idx = arr [ i ] * n ; buckets [ ( int ) idx ] . Add ( arr [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { buckets [ i ] . Sort ( ) ; } int index = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < buckets [ i ] . Count ; j ++ ) { arr [ index ++ ] = buckets [ i ] [ j ] ; } } } public static void Main ( ) { float [ ] arr = { ( float ) 0.897 , ( float ) 0.565 , ( float ) 0.656 , ( float ) 0.1234 , ( float ) 0.665 , ( float ) 0.3434 } ; int n = arr . Length ; bucketSort ( arr , n ) ; Console . WriteLine ( " Sorted ▁ array ▁ is ▁ " ) ; foreach ( float el in arr ) { Console . Write ( el + " ▁ " ) ; } } }
ShellSort | C # implementation of ShellSort ; An utility function to print array of size n ; function to sort arr using shellSort ; Start with a big gap , then reduce the gap ; Do a gapped insertion sort for this gap size . The first gap elements a [ 0. . gap - 1 ] are already in gapped order keep adding one more element until the entire array is gap sorted ; add a [ i ] to the elements that have been gap sorted save a [ i ] in temp and make a hole at position i ; shift earlier gap - sorted elements up until the correct location for a [ i ] is found ; put temp ( the original a [ i ] ) in its correct location ; Driver method
using System ; class ShellSort { static void printArray ( int [ ] arr ) { int n = arr . Length ; for ( int i = 0 ; i < n ; ++ i ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } int sort ( int [ ] arr ) { int n = arr . Length ; for ( int gap = n / 2 ; gap > 0 ; gap /= 2 ) { for ( int i = gap ; i < n ; i += 1 ) { int temp = arr [ i ] ; int j ; for ( j = i ; j >= gap && arr [ j - gap ] > temp ; j -= gap ) arr [ j ] = arr [ j - gap ] ; arr [ j ] = temp ; } } return 0 ; } public static void Main ( ) { int [ ] arr = { 12 , 34 , 54 , 2 , 3 } ; Console . Write ( " Array ▁ before ▁ sorting ▁ : STRNEWLINE " ) ; printArray ( arr ) ; ShellSort ob = new ShellSort ( ) ; ob . sort ( arr ) ; Console . Write ( " Array ▁ after ▁ sorting ▁ : STRNEWLINE " ) ; printArray ( arr ) ; } }
Comb Sort | C # program for implementation of Comb Sort ; To find gap between elements ; Shrink gap by Shrink factor ; Function to sort arr [ ] using Comb Sort ; initialize gap ; Initialize swapped as true to make sure that loop runs ; Keep running while gap is more than 1 and last iteration caused a swap ; Find next gap ; Initialize swapped as false so that we can check if swap happened or not ; Compare all elements with current gap ; Swap arr [ i ] and arr [ i + gap ] ; Set swapped ; Driver method
using System ; class GFG { static int getNextGap ( int gap ) { gap = ( gap * 10 ) / 13 ; if ( gap < 1 ) return 1 ; return gap ; } static void sort ( int [ ] arr ) { int n = arr . Length ; int gap = n ; bool swapped = true ; while ( gap != 1 swapped == true ) { gap = getNextGap ( gap ) ; swapped = false ; for ( int i = 0 ; i < n - gap ; i ++ ) { if ( arr [ i ] > arr [ i + gap ] ) { int temp = arr [ i ] ; arr [ i ] = arr [ i + gap ] ; arr [ i + gap ] = temp ; swapped = true ; } } } } public static void Main ( ) { int [ ] arr = { 8 , 4 , 1 , 56 , 3 , - 44 , 23 , - 6 , 28 , 0 } ; sort ( arr ) ; Console . WriteLine ( " sorted ▁ array " ) ; for ( int i = 0 ; i < arr . Length ; ++ i ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Cycle Sort | C # program to implement cycle sort ; Function sort the array using Cycle sort ; count number of memory writes ; traverse array elements and put it to on the right place ; initialize item as starting point ; Find position where we put the item . We basically count all smaller elements on right side of item . ; If item is already in correct position ; ignore all duplicate elements ; put the item to it 's right position ; Rotate rest of the cycle ; Find position where we put the element ; ignore all duplicate elements ; put the item to it 's right position ; Driver program to test above function
using System ; class GFG { public static void cycleSort ( int [ ] arr , int n ) { int writes = 0 ; for ( int cycle_start = 0 ; cycle_start <= n - 2 ; cycle_start ++ ) { int item = arr [ cycle_start ] ; int pos = cycle_start ; for ( int i = cycle_start + 1 ; i < n ; i ++ ) if ( arr [ i ] < item ) pos ++ ; if ( pos == cycle_start ) continue ; while ( item == arr [ pos ] ) pos += 1 ; if ( pos != cycle_start ) { int temp = item ; item = arr [ pos ] ; arr [ pos ] = temp ; writes ++ ; } while ( pos != cycle_start ) { pos = cycle_start ; for ( int i = cycle_start + 1 ; i < n ; i ++ ) if ( arr [ i ] < item ) pos += 1 ; while ( item == arr [ pos ] ) pos += 1 ; if ( item != arr [ pos ] ) { int temp = item ; item = arr [ pos ] ; arr [ pos ] = temp ; writes ++ ; } } } } public static void Main ( ) { int [ ] arr = { 1 , 8 , 3 , 9 , 10 , 10 , 2 , 4 } ; int n = arr . Length ; cycleSort ( arr , n ) ; Console . Write ( " After ▁ sort ▁ : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Merge Sort for Linked Lists | C # program for the above approach ; Node Class ; Function to merge sort ; Function to merge two linked lists ; While head1 is not null and head2 is not null ; While head1 is not null ; While head2 is not null ; Find mid using The Tortoise and The Hare approach ; Function to print list ; Driver Code ; Apply merge Sort
using System ; public class Node { public int data ; public Node next ; public Node ( int key ) { this . data = key ; next = null ; } } class GFG { static Node mergeSort ( Node head ) { if ( head . next == null ) return head ; Node mid = findMid ( head ) ; Node head2 = mid . next ; mid . next = null ; Node newHead1 = mergeSort ( head ) ; Node newHead2 = mergeSort ( head2 ) ; Node finalHead = merge ( newHead1 , newHead2 ) ; return finalHead ; } static Node merge ( Node head1 , Node head2 ) { Node merged = new Node ( - 1 ) ; Node temp = merged ; while ( head1 != null && head2 != null ) { if ( head1 . data < head2 . data ) { temp . next = head1 ; head1 = head1 . next ; } else { temp . next = head2 ; head2 = head2 . next ; } temp = temp . next ; } while ( head1 != null ) { temp . next = head1 ; head1 = head1 . next ; temp = temp . next ; } while ( head2 != null ) { temp . next = head2 ; head2 = head2 . next ; temp = temp . next ; } return merged . next ; } static Node findMid ( Node head ) { Node slow = head , fast = head . next ; while ( fast != null && fast . next != null ) { slow = slow . next ; fast = fast . next . next ; } return slow ; } static void printList ( Node head ) { while ( head != null ) { Console . Write ( head . data + " ▁ " ) ; head = head . next ; } } public static void Main ( String [ ] args ) { Node head = new Node ( 7 ) ; Node temp = head ; temp . next = new Node ( 10 ) ; temp = temp . next ; temp . next = new Node ( 5 ) ; temp = temp . next ; temp . next = new Node ( 20 ) ; temp = temp . next ; temp . next = new Node ( 3 ) ; temp = temp . next ; temp . next = new Node ( 2 ) ; temp = temp . next ; head = mergeSort ( head ) ; Console . Write ( " STRNEWLINE Sorted ▁ Linked ▁ List ▁ is : ▁ STRNEWLINE " ) ; printList ( head ) ; } }
Iterative Quick Sort | C # program for implementation of QuickSort ; This function takes last element as pivot , places the pivot element at its correct position in sorted array , and places all smaller ( smaller than pivot ) to left of pivot and all greater elements to right of pivot ; The main function that implements QuickSort ( ) arr [ ] -- > Array to be sorted , low -- > Starting index , high -- > Ending index ; pi is partitioning index , arr [ pi ] is now at right place ; Driver code
using System ; class GFG { static int partition ( int [ ] arr , int low , int high ) { int temp ; int pivot = arr [ high ] ; int i = ( low - 1 ) ; for ( int j = low ; j <= high - 1 ; j ++ ) { if ( arr [ j ] <= pivot ) { i ++ ; temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } } temp = arr [ i + 1 ] ; arr [ i + 1 ] = arr [ high ] ; arr [ high ] = temp ; return i + 1 ; } static void qSort ( int [ ] arr , int low , int high ) { if ( low < high ) { int pi = partition ( arr , low , high ) ; qSort ( arr , low , pi - 1 ) ; qSort ( arr , pi + 1 , high ) ; } } public static void Main ( ) { int n = 5 ; int [ ] arr = { 4 , 2 , 6 , 9 , 2 } ; qSort ( arr , 0 , n - 1 ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Iterative Quick Sort | C # program for implementation of QuickSort ; This function takes last element as pivot , places the pivot element at its correct position in sorted array , and places all smaller ( smaller than pivot ) to left of pivot and all greater elements to right of pivot ; A [ ] -- > Array to be sorted , l -- > Starting index , h -- > Ending index ; Create an auxiliary stack ; initialize top of stack ; push initial values of l and h to stack ; Keep popping from stack while is not empty ; Pop h and l ; Set pivot element at its correct position in sorted array ; If there are elements on left side of pivot , then push left side to stack ; If there are elements on right side of pivot , then push right side to stack ; Driver code ; Function calling
using System ; class GFG { static int partition ( int [ ] arr , int low , int high ) { int temp ; int pivot = arr [ high ] ; int i = ( low - 1 ) ; for ( int j = low ; j <= high - 1 ; j ++ ) { if ( arr [ j ] <= pivot ) { i ++ ; temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } } temp = arr [ i + 1 ] ; arr [ i + 1 ] = arr [ high ] ; arr [ high ] = temp ; return i + 1 ; } static void quickSortIterative ( int [ ] arr , int l , int h ) { int [ ] stack = new int [ h - l + 1 ] ; int top = - 1 ; stack [ ++ top ] = l ; stack [ ++ top ] = h ; while ( top >= 0 ) { h = stack [ top -- ] ; l = stack [ top -- ] ; int p = partition ( arr , l , h ) ; if ( p - 1 > l ) { stack [ ++ top ] = l ; stack [ ++ top ] = p - 1 ; } if ( p + 1 < h ) { stack [ ++ top ] = p + 1 ; stack [ ++ top ] = h ; } } } public static void Main ( ) { int [ ] arr = { 4 , 3 , 5 , 2 , 1 , 3 , 2 , 3 } ; int n = 8 ; quickSortIterative ( arr , 0 , n - 1 ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Sort n numbers in range from 0 to n ^ 2 | C # program to sort an array of size n where elements are in range from 0 to n ^ 2 1. ; A function to do counting sort of arr [ ] according to the digit represented by exp . ; output array ; Store count of occurrences in count [ ] ; Change count [ i ] so that count [ i ] now contains actual position of this digit in output [ ] ; Build the output array ; Copy the output array to arr [ ] , so that arr [ ] now contains sorted numbers according to current digit ; The main function to that sorts arr [ ] of size n using Radix Sort ; Do counting sort for first digit in base n . Note that instead of passing digit number , exp ( n ^ 0 = 1 ) is passed . ; Do counting sort for second digit in base n . Note that instead of passing digit number , exp ( n ^ 1 = n ) is passed . ; A utility function to print an array ; Driver Code ; Since array size is 7 , elements should be from 0 to 48
using System ; class GFG { static void countSort ( int [ ] arr , int n , int exp ) { int [ ] output = new int [ n ] ; int [ ] count = new int [ n ] ; int i ; for ( i = 0 ; i < n ; i ++ ) count [ i ] = 0 ; for ( i = 0 ; i < n ; i ++ ) count [ ( arr [ i ] / exp ) % n ] ++ ; for ( i = 1 ; i < n ; i ++ ) count [ i ] += count [ i - 1 ] ; for ( i = n - 1 ; i >= 0 ; i -- ) { output [ count [ ( arr [ i ] / exp ) % n ] - 1 ] = arr [ i ] ; count [ ( arr [ i ] / exp ) % n ] -- ; } for ( i = 0 ; i < n ; i ++ ) arr [ i ] = output [ i ] ; } static void sort ( int [ ] arr , int n ) { countSort ( arr , n , 1 ) ; countSort ( arr , n , n ) ; } static void printArr ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static public void Main ( ) { int [ ] arr = { 40 , 12 , 45 , 32 , 33 , 1 , 22 } ; int n = arr . Length ; Console . WriteLine ( " Given ▁ array " ) ; printArr ( arr , n ) ; sort ( arr , n ) ; Console . WriteLine ( " STRNEWLINE Sorted ▁ array " ) ; printArr ( arr , n ) ; } }
Search in an almost sorted array | C # program to find an element in an almost sorted array ; A recursive binary search based function . It returns index of x in given array arr [ l . . r ] is present , otherwise - 1 ; If the element is present at one of the middle 3 positions ; If element is smaller than mid , then it can only be present in left subarray ; Else the element can only be present in right subarray ; We reach here when element is not present in array ; Driver code
using System ; class GFG { int binarySearch ( int [ ] arr , int l , int r , int x ) { if ( r >= l ) { int mid = l + ( r - l ) / 2 ; if ( arr [ mid ] == x ) return mid ; if ( mid > l && arr [ mid - 1 ] == x ) return ( mid - 1 ) ; if ( mid < r && arr [ mid + 1 ] == x ) return ( mid + 1 ) ; if ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 2 , x ) ; return binarySearch ( arr , mid + 2 , r , x ) ; } return - 1 ; } public static void Main ( ) { GFG ob = new GFG ( ) ; int [ ] arr = { 3 , 2 , 10 , 4 , 40 } ; int n = arr . Length ; int x = 4 ; int result = ob . binarySearch ( arr , 0 , n - 1 , x ) ; if ( result == - 1 ) Console . Write ( " Element ▁ is ▁ not ▁ present ▁ in ▁ array " ) ; else Console . Write ( " Element ▁ is ▁ present ▁ at ▁ index ▁ " + result ) ; } }
Find the closest pair from two sorted arrays | C # program to find closest pair in an array ; ar1 [ 0. . m - 1 ] and ar2 [ 0. . n - 1 ] are two given sorted arrays / and x is given number . This function prints the pair from both arrays such that the sum of the pair is closest to x . ; Initialize the diff between pair sum and x . ; res_l and res_r are result indexes from ar1 [ ] and ar2 [ ] respectively ; Start from left side of ar1 [ ] and right side of ar2 [ ] ; If this pair is closer to x than the previously found closest , then update res_l , res_r and diff ; If sum of this pair is more than x , move to smaller side ; move to the greater side ; Print the result ; Driver program to test above functions
using System ; class GFG { static void printClosest ( int [ ] ar1 , int [ ] ar2 , int m , int n , int x ) { int diff = int . MaxValue ; int res_l = 0 , res_r = 0 ; int l = 0 , r = n - 1 ; while ( l < m && r >= 0 ) { if ( Math . Abs ( ar1 [ l ] + ar2 [ r ] - x ) < diff ) { res_l = l ; res_r = r ; diff = Math . Abs ( ar1 [ l ] + ar2 [ r ] - x ) ; } if ( ar1 [ l ] + ar2 [ r ] > x ) r -- ; else l ++ ; } Console . Write ( " The ▁ closest ▁ pair ▁ is ▁ [ " + ar1 [ res_l ] + " , ▁ " + ar2 [ res_r ] + " ] " ) ; } public static void Main ( ) { int [ ] ar1 = { 1 , 4 , 5 , 7 } ; int [ ] ar2 = { 10 , 20 , 30 , 40 } ; int m = ar1 . Length ; int n = ar2 . Length ; int x = 38 ; printClosest ( ar1 , ar2 , m , n , x ) ; } }
Given a sorted array and a number x , find the pair in array whose sum is closest to x | C # program to find pair with sum closest to x ; Prints the pair with sum cloest to x ; To store indexes of result pair ; Initialize left and right indexes and difference between pair sum and x ; While there are elements between l and r ; Check if this pair is closer than the closest pair so far ; If this pair has more sum , move to smaller values . ; Move to larger values ; Driver program to test above function
using System ; class GFG { static void printClosest ( int [ ] arr , int n , int x ) { int res_l = 0 , res_r = 0 ; int l = 0 , r = n - 1 , diff = int . MaxValue ; while ( r > l ) { if ( Math . Abs ( arr [ l ] + arr [ r ] - x ) < diff ) { res_l = l ; res_r = r ; diff = Math . Abs ( arr [ l ] + arr [ r ] - x ) ; } if ( arr [ l ] + arr [ r ] > x ) r -- ; else l ++ ; } Console . Write ( " ▁ The ▁ closest ▁ pair ▁ is ▁ " + arr [ res_l ] + " ▁ and ▁ " + arr [ res_r ] ) ; } public static void Main ( ) { int [ ] arr = { 10 , 22 , 28 , 29 , 30 , 40 } ; int x = 54 ; int n = arr . Length ; printClosest ( arr , n , x ) ; } }
Count 1 's in a sorted binary array | C # program to count 1 's in a sorted array ; Returns counts of 1 's in arr[low..high].  The array is assumed to be sorted in  non-increasing order  ; get the middle index ; check if the element at middle index is last 1 ; If element is not last 1 , recur for right side ; else recur for left side ; Driver code
using System ; class GFG { static int countOnes ( int [ ] arr , int low , int high ) { if ( high >= low ) { int mid = low + ( high - low ) / 2 ; if ( ( mid == high arr [ mid + 1 ] == 0 ) && ( arr [ mid ] == 1 ) ) return mid + 1 ; if ( arr [ mid ] == 1 ) return countOnes ( arr , ( mid + 1 ) , high ) ; return countOnes ( arr , low , ( mid - 1 ) ) ; } return 0 ; } public static void Main ( ) { int [ ] arr = { 1 , 1 , 1 , 1 , 0 , 0 , 0 } ; int n = arr . Length ; Console . WriteLine ( " Count ▁ of ▁ 1 ' s ▁ in ▁ given ▁ " + " array ▁ is ▁ " + countOnes ( arr , 0 , n - 1 ) ) ; } }
Minimum adjacent swaps to move maximum and minimum to corners | C # program to count Minimum number of swaps so that the largest element is at beginning and the smallest element is at last ; Function performing calculations ; Index of leftmost largest element ; Index of rightmost smallest element ; Driver Code
using System ; class GFG { public static void minimumSwaps ( int [ ] a , int n ) { int maxx = - 1 , l = 0 , minn = a [ 0 ] , r = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > maxx ) { maxx = a [ i ] ; l = i ; } if ( a [ i ] <= minn ) { minn = a [ i ] ; r = i ; } } if ( r < l ) Console . WriteLine ( l + ( n - r - 2 ) ) ; else Console . WriteLine ( l + ( n - r - 1 ) ) ; } public static void Main ( ) { int [ ] a = { 5 , 6 , 1 , 3 } ; int n = a . Length ; minimumSwaps ( a , n ) ; } }
Activity Selection Problem | Greedy Algo | The following implementation assumes that the activities are already sorted according to their finish time ; Prints a maximum set of activities that can be done by a single person , one at a time . n -- > Total number of activities s [ ] -- > An array that contains start time of all activities f [ ] -- > An array that contains finish time of all activities ; The first activity always gets selected ; Consider rest of the activities ; If this activity has start time greater than or equal to the finish time of previously selected activity , then select it ; Driver Code
using System ; class GFG { public static void printMaxActivities ( int [ ] s , int [ ] f , int n ) { int i , j ; Console . Write ( " Following ▁ activities ▁ are ▁ selected ▁ : ▁ " ) ; i = 0 ; Console . Write ( i + " ▁ " ) ; for ( j = 1 ; j < n ; j ++ ) { if ( s [ j ] >= f [ i ] ) { Console . Write ( j + " ▁ " ) ; i = j ; } } } public static void Main ( ) { int [ ] s = { 1 , 3 , 0 , 5 , 8 , 5 } ; int [ ] f = { 2 , 4 , 6 , 7 , 9 , 9 } ; int n = s . Length ; printMaxActivities ( s , f , n ) ; } }
Longest Increasing Subsequence | DP | Dynamic Programming C # implementation of LIS problem ; lis ( ) returns the length of the longest increasing subsequence in arr [ ] of size n ; Initialize LIS values for all indexes ; Compute optimized LIS values in bottom up manner ; Pick maximum of all LIS values ; Driver code
using System ; class LIS { static int lis ( int [ ] arr , int n ) { int [ ] lis = new int [ n ] ; int i , j , max = 0 ; for ( i = 0 ; i < n ; i ++ ) lis [ i ] = 1 ; for ( i = 1 ; i < n ; i ++ ) for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; for ( i = 0 ; i < n ; i ++ ) if ( max < lis [ i ] ) max = lis [ i ] ; return max ; } public static void Main ( ) { int [ ] arr = { 10 , 22 , 9 , 33 , 21 , 50 , 41 , 60 } ; int n = arr . Length ; Console . WriteLine ( " Length ▁ of ▁ lis ▁ is ▁ " + lis ( arr , n ) + " STRNEWLINE " ) ; } }