codeparrot/xlcost-text-to-code huggingface dataset:
Check if array elements are consecutive | Added Method 3 |  ; The function checks if the array elements are consecutive If elements are consecutive , then returns true , else returns false ; 1 ) Get the minimum element in array ; 2 ) Get the maximum element in array ; 3 ) max - min + 1 is equal to n , then only check all elements ; Create a temp array to hold visited flag of all elements . Note that , calloc is used here so that all values are initialized as false ; If we see an element again , then return false ; If visited first time , then mark the element as visited ; If all elements occur once , then return true ; if ( max - min + 1 != n ) ; UTILITY FUNCTIONS ; Driver program to test above functions
class AreConsecutive { boolean areConsecutive ( int arr [ ] , int n ) { if ( n < 1 ) return false ; int min = getMin ( arr , n ) ; int max = getMax ( arr , n ) ; if ( max - min + 1 == n ) { boolean visited [ ] = new boolean [ n ] ; int i ; for ( i = 0 ; i < n ; i ++ ) { if ( visited [ arr [ i ] - min ] != false ) return false ; visited [ arr [ i ] - min ] = true ; } return true ; } return false ; } int getMin ( int arr [ ] , int n ) { int min = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < min ) min = arr [ i ] ; } return min ; } int getMax ( int arr [ ] , int n ) { int max = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; } return max ; } public static void main ( String [ ] args ) { AreConsecutive consecutive = new AreConsecutive ( ) ; int arr [ ] = { 5 , 4 , 2 , 3 , 1 , 6 } ; int n = arr . length ; if ( consecutive . areConsecutive ( arr , n ) == true ) System . out . println ( " Array ▁ elements ▁ are ▁ consecutive " ) ; else System . out . println ( " Array ▁ elements ▁ are ▁ not ▁ consecutive " ) ; } }
Check if array elements are consecutive | Added Method 3 |  ; The function checks if the array elements are consecutive If elements are consecutive , then returns true , else returns false ; 1 ) Get the minimum element in array ; 2 ) Get the maximum element in array ; 3 ) max - min + 1 is equal to n then only check all elements ; if the value at index j is negative then there is repitition ; If we do not see a negative value then all elements are distinct ; if ( max - min + 1 != n ) ; UTILITY FUNCTIONS ; Driver program to test above functions
class AreConsecutive { boolean areConsecutive ( int arr [ ] , int n ) { if ( n < 1 ) return false ; int min = getMin ( arr , n ) ; int max = getMax ( arr , n ) ; if ( max - min + 1 == n ) { int i ; for ( i = 0 ; i < n ; i ++ ) { int j ; if ( arr [ i ] < 0 ) j = - arr [ i ] - min ; else j = arr [ i ] - min ; if ( arr [ j ] > 0 ) arr [ j ] = - arr [ j ] ; else return false ; } return true ; } return false ; } int getMin ( int arr [ ] , int n ) { int min = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < min ) min = arr [ i ] ; } return min ; } int getMax ( int arr [ ] , int n ) { int max = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; } return max ; } public static void main ( String [ ] args ) { AreConsecutive consecutive = new AreConsecutive ( ) ; int arr [ ] = { 5 , 4 , 2 , 3 , 1 , 6 } ; int n = arr . length ; if ( consecutive . areConsecutive ( arr , n ) == true ) System . out . println ( " Array ▁ elements ▁ are ▁ consecutive " ) ; else System . out . println ( " Array ▁ elements ▁ are ▁ not ▁ consecutive " ) ; } }
Find relative complement of two sorted arrays | Java program to find all those elements of arr1 [ ] that are not present in arr2 [ ] ; If current element in arr2 [ ] is greater , then arr1 [ i ] can 't be  present in arr2[j..m-1] ; Skipping smaller elements of arr2 [ ] ; Equal elements found ( skipping in both arrays ) ; Printing remaining elements of arr1 [ ] ; Driver code
class GFG { static void relativeComplement ( int arr1 [ ] , int arr2 [ ] , int n , int m ) { int i = 0 , j = 0 ; while ( i < n && j < m ) { if ( arr1 [ i ] < arr2 [ j ] ) { System . out . print ( arr1 [ i ] + " ▁ " ) ; i ++ ; } else if ( arr1 [ i ] > arr2 [ j ] ) { j ++ ; } else if ( arr1 [ i ] == arr2 [ j ] ) { i ++ ; j ++ ; } } while ( i < n ) System . out . print ( arr1 [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 3 , 6 , 10 , 12 , 15 } ; int arr2 [ ] = { 1 , 3 , 5 , 10 , 16 } ; int n = arr1 . length ; int m = arr2 . length ; relativeComplement ( arr1 , arr2 , n , m ) ; } }
Minimum increment by k operations to make all elements equal | Program to make all array equal ; function for calculating min operations ; max elements of array ; iterate for all elements ; check if element can make equal to max or not if not then return - 1 ; else update res for required operations ; return result ; Driver program
import java . io . * ; import java . util . Arrays ; class GFG { static int minOps ( int arr [ ] , int n , int k ) { Arrays . sort ( arr ) ; int max = arr [ arr . length - 1 ] ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( max - arr [ i ] ) % k != 0 ) return - 1 ; else res += ( max - arr [ i ] ) / k ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 21 , 33 , 9 , 45 , 63 } ; int n = arr . length ; int k = 6 ; System . out . println ( minOps ( arr , n , k ) ) ; } }
Minimize ( max ( A [ i ] , B [ j ] , C [ k ] ) | Java code for above approach ; assigning the length - 1 value to each of three variables ; calculating min difference from last index of lists ; checking condition ; calculating max term from list ; Moving to smaller value in the array with maximum out of three . ; Driver code
import java . util . * ; class GFG { static int solve ( int [ ] A , int [ ] B , int [ ] C ) { int i , j , k ; i = A . length - 1 ; j = B . length - 1 ; k = C . length - 1 ; int min_diff , current_diff , max_term ; min_diff = Math . abs ( Math . max ( A [ i ] , Math . max ( B [ j ] , C [ k ] ) ) - Math . min ( A [ i ] , Math . min ( B [ j ] , C [ k ] ) ) ) ; while ( i != - 1 && j != - 1 && k != - 1 ) { current_diff = Math . abs ( Math . max ( A [ i ] , Math . max ( B [ j ] , C [ k ] ) ) - Math . min ( A [ i ] , Math . min ( B [ j ] , C [ k ] ) ) ) ; if ( current_diff < min_diff ) min_diff = current_diff ; max_term = Math . max ( A [ i ] , Math . max ( B [ j ] , C [ k ] ) ) ; if ( A [ i ] == max_term ) i -= 1 ; else if ( B [ j ] == max_term ) j -= 1 ; else k -= 1 ; } return min_diff ; } public static void main ( String [ ] args ) { int [ ] D = { 5 , 8 , 10 , 15 } ; int [ ] E = { 6 , 9 , 15 , 78 , 89 } ; int [ ] F = { 2 , 3 , 6 , 6 , 8 , 8 , 10 } ; System . out . println ( solve ( D , E , F ) ) ; } }
Analysis of Algorithms | Set 2 ( Worst , Average and Best Cases ) | Java implementation of the approach ; Linearly search x in arr [ ] . If x is present then return the index , otherwise return - 1 ; Driver program to test above functions
public class GFG { static int search ( int arr [ ] , int n , int x ) { int i ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == x ) { return i ; } } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 10 , 30 , 15 } ; int x = 30 ; int n = arr . length ; System . out . printf ( " % d ▁ is ▁ present ▁ at ▁ index ▁ % d " , x , search ( arr , n , x ) ) ; } }
Binary Search | Java implementation of recursive Binary Search ; Returns index of x if it is present in arr [ l . . r ] , else return - 1 ; If the element is present at the middle itself ; 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 method to test above
class BinarySearch { 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 ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 1 , x ) ; return binarySearch ( arr , mid + 1 , r , x ) ; } return - 1 ; } public static void main ( String args [ ] ) { BinarySearch ob = new BinarySearch ( ) ; int arr [ ] = { 2 , 3 , 4 , 10 , 40 } ; int n = arr . length ; int x = 10 ; int result = ob . binarySearch ( arr , 0 , n - 1 , x ) ; if ( result == - 1 ) System . out . println ( " Element ▁ not ▁ present " ) ; else System . out . println ( " Element ▁ found ▁ at ▁ index ▁ " + result ) ; } }
Binary Search | Java implementation of iterative Binary Search ; Returns index of x if it is present in arr [ ] , else return - 1 ; Check if x is present at mid ; If x greater , ignore left half ; If x is smaller , ignore right half ; if we reach here , then element was not present ; Driver method to test above
class BinarySearch { int binarySearch ( int arr [ ] , int x ) { int l = 0 , r = arr . length - 1 ; while ( l <= r ) { int m = l + ( r - l ) / 2 ; if ( arr [ m ] == x ) return m ; if ( arr [ m ] < x ) l = m + 1 ; else r = m - 1 ; } return - 1 ; } public static void main ( String args [ ] ) { BinarySearch ob = new BinarySearch ( ) ; int arr [ ] = { 2 , 3 , 4 , 10 , 40 } ; int n = arr . length ; int x = 10 ; int result = ob . binarySearch ( arr , x ) ; if ( result == - 1 ) System . out . println ( " Element ▁ not ▁ present " ) ; else System . out . println ( " Element ▁ found ▁ at ▁ " + " index ▁ " + result ) ; } }
Jump Search | Java program to implement Jump Search . ; Finding block size to be jumped ; Finding the block where element is present ( if it is present ) ; Doing a linear search for x in block beginning with prev . ; If we reached next block or end of array , element is not present . ; If element is found ; Driver program to test function ; Find the index of ' x ' using Jump Search ; Print the index where ' x ' is located
public class JumpSearch { public static int jumpSearch ( int [ ] arr , int x ) { int n = arr . length ; int step = ( int ) Math . floor ( Math . sqrt ( n ) ) ; int prev = 0 ; while ( arr [ Math . min ( step , n ) - 1 ] < x ) { prev = step ; step += ( int ) Math . floor ( Math . sqrt ( n ) ) ; if ( prev >= n ) return - 1 ; } while ( arr [ prev ] < x ) { prev ++ ; if ( prev == Math . min ( step , n ) ) return - 1 ; } if ( arr [ prev ] == x ) return prev ; return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 , 144 , 233 , 377 , 610 } ; int x = 55 ; int index = jumpSearch ( arr , x ) ; System . out . println ( " Number " ▁ + ▁ x ▁ + ▁ " is at index " + index); } }
Interpolation Search | Java program to implement interpolation search with recursion ; If x is present in arr [ 0. . n - 1 ] , then returns index of it , else returns - 1. ; Since array is sorted , an element present in array must be in range defined by corner ; Probing the position with keeping uniform distribution in mind . ; Condition of target found ; If x is larger , x is in right sub array ; If x is smaller , x is in left sub array ; Driver Code ; Array of items on which search will be conducted . ; Element to be searched ; If element was found
import java . util . * ; class GFG { public static int interpolationSearch ( int arr [ ] , int lo , int hi , int x ) { int pos ; if ( lo <= hi && x >= arr [ lo ] && x <= arr [ hi ] ) { pos = lo + ( ( ( hi - lo ) / ( arr [ hi ] - arr [ lo ] ) ) * ( x - arr [ lo ] ) ) ; if ( arr [ pos ] == x ) return pos ; if ( arr [ pos ] < x ) return interpolationSearch ( arr , pos + 1 , hi , x ) ; if ( arr [ pos ] > x ) return interpolationSearch ( arr , lo , pos - 1 , x ) ; } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 12 , 13 , 16 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 33 , 35 , 42 , 47 } ; int n = arr . length ; int x = 18 ; int index = interpolationSearch ( arr , 0 , n - 1 , x ) ; if ( index != - 1 ) System . out . println ( " Element ▁ found ▁ at ▁ index ▁ " + index ) ; else System . out . println ( " Element ▁ not ▁ found . " ) ; } }
Exponential Search | Java program to find an element x in a sorted array using Exponential search . ; Returns position of first occurrence of x in array ; If x is present at firt location itself ; Find range for binary search by repeated doubling ; Call binary search for the found range . ; Driver code
import java . util . Arrays ; class GFG { static int exponentialSearch ( int arr [ ] , int n , int x ) { if ( arr [ 0 ] == x ) return 0 ; int i = 1 ; while ( i < n && arr [ i ] <= x ) i = i * 2 ; return Arrays . binarySearch ( arr , i / 2 , Math . min ( i , n - 1 ) , x ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 3 , 4 , 10 , 40 } ; int x = 10 ; int result = exponentialSearch ( arr , arr . length , x ) ; System . out . println ( ( result < 0 ) ? " Element ▁ is ▁ not ▁ present ▁ in ▁ array " : " Element ▁ is ▁ present ▁ at ▁ index ▁ " + result ) ; } }
Merge Sort | Java 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
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 ] ; for ( int i = 0 ; i < n1 ; ++ i ) L [ i ] = arr [ l + i ] ; for ( int j = 0 ; j < n2 ; ++ j ) R [ j ] = arr [ m + 1 + j ] ; int 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 ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 12 , 11 , 13 , 5 , 6 , 7 } ; System . out . println ( " Given ▁ Array " ) ; printArray ( arr ) ; MergeSort ob = new MergeSort ( ) ; ob . sort ( arr , 0 , arr . length - 1 ) ; System . out . println ( " Sorted array "); printArray ( arr ) ; } }
Radix Sort | Radix sort Java implementation ; 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 ; 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
import java . io . * ; import java . util . * ; class Radix { 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 ; } static void countSort ( int arr [ ] , int n , int exp ) { int output [ ] = new int [ n ] ; int i ; int count [ ] = new int [ 10 ] ; Arrays . fill ( count , 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 ] ; } 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 ) ; } static void print ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 170 , 45 , 75 , 90 , 802 , 24 , 2 , 66 } ; int n = arr . length ; radixsort ( arr , n ) ; print ( arr , n ) ; } }
Bucket Sort | Java 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
import java . util . * ; import java . util . Collections ; class GFG { static void bucketSort ( float arr [ ] , int n ) { if ( n <= 0 ) return ; @ SuppressWarnings ( " unchecked " ) Vector < Float > [ ] buckets = new Vector [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { buckets [ i ] = new Vector < 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 ++ ) { Collections . sort ( buckets [ i ] ) ; } int index = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < buckets [ i ] . size ( ) ; j ++ ) { arr [ index ++ ] = buckets [ i ] . get ( j ) ; } } } public static void main ( String args [ ] ) { 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 ) ; System . out . println ( " Sorted ▁ array ▁ is ▁ " ) ; for ( float el : arr ) { System . out . print ( el + " ▁ " ) ; } } }
ShellSort | Java 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
class ShellSort { static void printArray ( int arr [ ] ) { int n = arr . length ; for ( int i = 0 ; i < n ; ++ i ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } 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 ( String args [ ] ) { int arr [ ] = { 12 , 34 , 54 , 2 , 3 } ; System . out . println ( " Array ▁ before ▁ sorting " ) ; printArray ( arr ) ; ShellSort ob = new ShellSort ( ) ; ob . sort ( arr ) ; System . out . println ( " Array ▁ after ▁ sorting " ) ; printArray ( arr ) ; } }
Comb Sort | Java 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
class CombSort { int getNextGap ( int gap ) { gap = ( gap * 10 ) / 13 ; if ( gap < 1 ) return 1 ; return gap ; } void sort ( int arr [ ] ) { int n = arr . length ; int gap = n ; boolean 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 ( String args [ ] ) { CombSort ob = new CombSort ( ) ; int arr [ ] = { 8 , 4 , 1 , 56 , 3 , - 44 , 23 , - 6 , 28 , 0 } ; ob . sort ( arr ) ; System . out . println ( " sorted ▁ array " ) ; for ( int i = 0 ; i < arr . length ; ++ i ) System . out . print ( arr [ i ] + " ▁ " ) ; } }
Cycle Sort | Java 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
import java . util . * ; import java . lang . * ; 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 ( String [ ] args ) { int arr [ ] = { 1 , 8 , 3 , 9 , 10 , 10 , 2 , 4 } ; int n = arr . length ; cycleSort ( arr , n ) ; System . out . println ( " After ▁ sort ▁ : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }
Merge Sort for Linked Lists | Java 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
import java . io . * ; import java . lang . * ; import java . util . * ; class Node { int data ; Node next ; 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 ) { System . out . print ( 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 ) ; System . out . print ( " Sorted Linked List is : "); printList ( head ) ; } }