codeparrot/xlcost-text-to-code huggingface dataset:
Perform K of Q queries to maximize the sum of the array elements | Java implementation of the approach ; pair class ; Function to perform K queries out of Q to maximize the final sum ; Get the initial sum of the array ; Stores the contriution of every query ; comparator ; Sort the contribution of queries in descending order ; Get the K most contributions ; Driver code
import java . util . * ; class GFG { static class pair { int first , second ; pair ( int f , int s ) { first = f ; second = s ; } } static int getFinalSum ( int a [ ] , int n , pair queries [ ] , int q , int k ) { int answer = 0 ; for ( int i = 0 ; i < n ; i ++ ) answer += a [ i ] ; Vector < Integer > contribution = new Vector < Integer > ( ) ; for ( int i = 0 ; i < q ; i ++ ) { contribution . add ( queries [ i ] . second - queries [ i ] . first + 1 ) ; } Comparator < Integer > Comp = new Comparator < Integer > ( ) { public int compare ( Integer e1 , Integer e2 ) { if ( e1 > e2 ) return - 1 ; else return 1 ; } } ; Collections . sort ( contribution , Comp ) ; int i = 0 ; while ( i < k ) { answer += ( int ) contribution . get ( i ) ; i ++ ; } return answer ; } public static void main ( String args [ ] ) { int a [ ] = { 1 , 1 , 2 , 2 , 2 , 3 } ; int n = a . length ; pair queries [ ] = new pair [ 5 ] ; queries [ 0 ] = new pair ( 0 , 4 ) ; queries [ 1 ] = new pair ( 1 , 2 ) ; queries [ 2 ] = new pair ( 2 , 5 ) ; queries [ 3 ] = new pair ( 2 , 3 ) ; queries [ 4 ] = new pair ( 2 , 4 ) ; int q = queries . length ; int k = 3 ; System . out . println ( getFinalSum ( a , n , queries , q , k ) ) ; } }
Queries to return the absolute difference between L | Java implementation of the approach ; pair class ; Function to return the result for a particular query ; Get the difference between the indices of L - th and the R - th smallest element ; Return the answer ; Function that performs all the queries ; Store the array numbers and their indices ; Sort pair ; Sort the array elements ; Answer all the queries ; Driver code
import java . util . * ; import java . util . Comparator ; class GFG { static class pair { int first , second ; pair ( int f , int s ) { first = f ; second = s ; } } static int answerQuery ( pair arr [ ] , int l , int r ) { int answer = Math . abs ( arr [ l - 1 ] . second - arr [ r - 1 ] . second ) ; return answer ; } static void solveQueries ( int a [ ] , int n , int q [ ] [ ] , int m ) { pair arr [ ] = new pair [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = new pair ( 0 , 0 ) ; arr [ i ] . first = a [ i ] ; arr [ i ] . second = i ; } Comparator < pair > comp = new Comparator < pair > ( ) { public int compare ( pair e1 , pair e2 ) { if ( e1 . first < e2 . first ) { return - 1 ; } else if ( e1 . first > e2 . first ) { return 1 ; } else { return 0 ; } } } ; Arrays . sort ( arr , comp ) ; for ( int i = 0 ; i < m ; i ++ ) System . out . println ( answerQuery ( arr , q [ i ] [ 0 ] , q [ i ] [ 1 ] ) ) ; } public static void main ( String args [ ] ) { int a [ ] = { 1 , 5 , 4 , 2 , 8 , 6 , 7 } ; int n = a . length ; int q [ ] [ ] = { { 2 , 5 } , { 1 , 3 } , { 1 , 5 } , { 3 , 6 } } ; int m = q . length ; solveQueries ( a , n , q , m ) ; } }
Print all full nodes in a Binary Tree | A binary tree node ; Traverses given tree in Inorder fashion and prints all nodes that have both children as non - empty . ; Driver program to test above function
class Node { int data ; Node left , right ; Node ( int data ) { left = right = null ; this . data = data ; } } ; public class FullNodes { public static void findFullNode ( Node root ) { if ( root != null ) { findFullNode ( root . left ) ; if ( root . left != null && root . right != null ) System . out . print ( root . data + " ▁ " ) ; findFullNode ( root . right ) ; } } public static void main ( String args [ ] ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . right . left = new Node ( 5 ) ; root . right . right = new Node ( 6 ) ; root . right . left . right = new Node ( 7 ) ; root . right . right . right = new Node ( 8 ) ; root . right . left . right . left = new Node ( 9 ) ; findFullNode ( root ) ; } }
Find the k largest numbers after deleting the given elements |  ; Find k maximum element from arr [ 0. . m - 1 ] after deleting elements from del [ 0. . n - 1 ] ; Hash Map of the numbers to be deleted ; Increment the count of del [ i ] ; Initializing the largestElement ; Search if the element is present ; Decrement its frequency ; If the frequency becomes 0 , erase it from the map ; Else compare it largestElement ; Print top k elements in the heap ; Pop the top element ; Driver code
import java . io . * ; import java . util . * ; class GFG { static void findElementsAfterDel ( int arr [ ] , int m , int del [ ] , int n , int k ) { Map < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( mp . containsKey ( del [ i ] ) ) { mp . put ( del [ i ] , mp . get ( del [ i ] ) + 1 ) ; } else { mp . put ( del [ i ] , 1 ) ; } } PriorityQueue < Integer > heap = new PriorityQueue < Integer > ( Collections . reverseOrder ( ) ) ; for ( int i = 0 ; i < m ; ++ i ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) - 1 ) ; if ( mp . get ( arr [ i ] ) == 0 ) { mp . remove ( arr [ i ] ) ; } } else { heap . add ( arr [ i ] ) ; } } for ( int i = 0 ; i < k ; ++ i ) { System . out . print ( heap . poll ( ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int array [ ] = { 5 , 12 , 33 , 4 , 56 , 12 , 20 } ; int m = array . length ; int del [ ] = { 12 , 56 , 5 } ; int n = del . length ; int k = 3 ; findElementsAfterDel ( array , m , del , n , k ) ; } }
K | Java implementation of the above approach ; Set to store the unique substring ; String to create each substring ; Adding to set ; Converting set into a list ; Sorting the strings int the list into lexicographical order ; Printing kth substring ; Driver Code
import java . util . * ; class GFG { public static void kThLexString ( String st , int k , int n ) { Set < String > z = new HashSet < String > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { String pp = " " ; for ( int j = i ; j < i + k ; j ++ ) { if ( j >= n ) break ; pp += st . charAt ( j ) ; z . add ( pp ) ; } } Vector < String > fin = new Vector < String > ( ) ; fin . addAll ( z ) ; Collections . sort ( fin ) ; System . out . print ( fin . get ( k - 1 ) ) ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; int k = 5 ; int n = s . length ( ) ; kThLexString ( s , k , n ) ; } }
Rearrange array such that arr [ i ] >= arr [ j ] if i is even and arr [ i ] <= arr [ j ] if i is odd and j < i | Java program to rearrange the array as per the given condition ; function to rearrange the array ; total even positions ; total odd positions ; copy original array in an auxiliary array ; sort the auxiliary array ; fill up odd position in original array ; fill up even positions in original array ; display array ; Driver function
import java . util . * ; import java . lang . * ; public class GfG { public static void rearrangeArr ( int arr [ ] , int n ) { int evenPos = n / 2 ; int oddPos = n - evenPos ; int [ ] tempArr = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) tempArr [ i ] = arr [ i ] ; Arrays . sort ( tempArr ) ; int j = oddPos - 1 ; for ( int i = 0 ; i < n ; i += 2 ) { arr [ i ] = tempArr [ j ] ; j -- ; } j = oddPos ; for ( int i = 1 ; i < n ; i += 2 ) { arr [ i ] = tempArr [ j ] ; j ++ ; } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String argc [ ] ) { int [ ] arr = new int [ ] { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ; int size = 7 ; rearrangeArr ( arr , size ) ; } }
Count triplets in a sorted doubly linked list whose product is equal to a given value x | Java 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
import java . util . * ; 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 ; System . out . println ( " count ▁ = ▁ " + countTriplets ( head , x ) ) ; } }
Sort only non | Java 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
import java . util . * ; class solution { static boolean prime [ ] = new boolean [ 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 ) ; Vector < Integer > v = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( prime [ arr [ i ] ] == false ) v . add ( arr [ i ] ) ; } Collections . sort ( v ) ; int j = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( prime [ arr [ i ] ] == true ) System . out . print ( arr [ i ] + " ▁ " ) ; else { System . out . print ( v . get ( j ) + " ▁ " ) ; j ++ ; } } } public static void main ( String args [ ] ) { int n = 6 ; int arr [ ] = { 100 , 11 , 500 , 2 , 17 , 1 } ; sortedArray ( arr , n ) ; } }
Sum of all nodes in a binary tree | Java 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
class GFG { static class Node { int key ; Node left , right ; } static Node newNode ( int key ) { Node node = new Node ( ) ; node . key = key ; node . left = node . right = null ; return ( node ) ; } 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 ) ; System . out . println ( " Sum ▁ of ▁ all ▁ the ▁ elements ▁ is : ▁ " + sum ) ; } }
Maximum sum of absolute difference of any permutation | Java 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
import java . io . * ; import java . util . * ; public class GFG { static int MaxSumDifference ( Integer [ ] a , int n ) { List < Integer > finalSequence = new ArrayList < Integer > ( ) ; Arrays . 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 . get ( i ) - finalSequence . get ( i + 1 ) ) ; } MaximumSum = MaximumSum + Math . abs ( finalSequence . get ( n - 1 ) - finalSequence . get ( 0 ) ) ; return MaximumSum ; } public static void main ( String args [ ] ) { Integer [ ] a = { 1 , 2 , 4 , 8 } ; int n = a . length ; System . out . print ( MaxSumDifference ( a , n ) ) ; } }
Minimum swaps required to bring all elements less than or equal to k together | Java 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
import java . lang . * ; 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 ( String [ ] args ) { int arr [ ] = { 2 , 1 , 5 , 6 , 3 } ; int n = arr . length ; int k = 3 ; System . out . print ( minSwap ( arr , n , k ) + "NEW_LINE"); int arr1 [ ] = { 2 , 7 , 9 , 5 , 8 , 7 , 4 } ; n = arr1 . length ; k = 5 ; System . out . print ( minSwap ( arr1 , n , k ) ) ; } }
Rearrange positive and negative numbers using inbuilt sort function | Java program to implement 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
import java . io . * ; class GFG { static void printArray ( int [ ] array , int length ) { System . out . print ( " [ " ) ; for ( int i = 0 ; i < length ; i ++ ) { System . out . print ( array [ i ] ) ; if ( i < ( length - 1 ) ) System . out . print ( " , " ) ; else System . out . print ( "]NEW_LINE"); } } 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 ++ ; } System . out . print ( " array : ▁ " ) ; printArray ( array , length ) ; rearrange ( array , 0 , ( length - 1 ) ) ; reverse ( array , countNegative , ( length - 1 ) ) ; System . out . print ( " 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
import java . io . * ; 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 ; } } for ( int i : arr1 ) { System . out . print ( i + " ▁ " ) ; } System . out . println ( ) ; for ( int i : arr2 ) { System . out . print ( i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] arr1 = { 1 , 3 , 5 , 7 } ; int [ ] arr2 = { 0 , 2 , 6 , 8 , 9 } ; mergeArray ( arr1 , arr2 ) ; } }
Sorting array of strings ( or words ) using Trie | Java program to sort an array of strings using Trie ; index is set when node is a leaf node , otherwise - 1 ; ; to make new trie ; function to insert in trie ; taking ascii value to find index of child node ; making new path if not already ; go to next node ; Mark leaf ( end of word ) and store index of word in arr [ ] ; function for preorder traversal ; if leaf node then print key ; insert all keys of dictionary into trie ; print keys in lexicographic order ; Driver code
import java . util . * ; class GFG { static final int MAX_CHAR = 26 ; static class Trie { int index ; Trie child [ ] = new Trie [ MAX_CHAR ] ; Trie ( ) { for ( int i = 0 ; i < MAX_CHAR ; i ++ ) child [ i ] = null ; index = - 1 ; } } static void insert ( Trie root , String str , int index ) { Trie node = root ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { int ind = str . charAt ( i ) - ' a ' ; if ( node . child [ ind ] == null ) node . child [ ind ] = new Trie ( ) ; node = node . child [ ind ] ; } node . index = index ; } static boolean preorder ( Trie node , String arr [ ] ) { if ( node == null ) { return false ; } for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { if ( node . child [ i ] != null ) { if ( node . child [ i ] . index != - 1 ) { System . out . print ( arr [ node . child [ i ] . index ] + " ▁ " ) ; } preorder ( node . child [ i ] , arr ) ; } } return false ; } static void printSorted ( String arr [ ] , int n ) { Trie root = new Trie ( ) ; for ( int i = 0 ; i < n ; ++ i ) { insert ( root , arr [ i ] , i ) ; } preorder ( root , arr ) ; } public static void main ( String [ ] args ) { String arr [ ] = { " abc " , " xy " , " bcd " } ; int n = arr . length ; printSorted ( arr , n ) ; } }
Maximum product of subsequence of size k | Java 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 ie . . 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
import java . io . * ; import java . util . * ; class GFG { static int maxProductSubarrayOfSizeK ( int A [ ] , int n , int k ) { Arrays . sort ( A ) ; int product = 1 ; if ( A [ n - 1 ] == 0 && k % 2 != 0 ) return 0 ; if ( A [ n - 1 ] <= 0 && k % 2 != 0 ) { for ( int i = n - 1 ; i >= n - k ; i -- ) product *= A [ i ] ; return product ; } int 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 ( String [ ] args ) { int A [ ] = { 1 , 2 , - 1 , - 3 , - 6 , 4 } ; int n = A . length ; int k = 4 ; System . out . println ( maxProductSubarrayOfSizeK ( A , n , k ) ) ; } }
Reorder an array according to given indexes | Java 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
import java . util . Arrays ; 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 ( String [ ] args ) { reorder ( ) ; System . out . println ( " Reordered ▁ array ▁ is : ▁ " ) ; System . out . println ( Arrays . toString ( arr ) ) ; System . out . println ( " Modified ▁ Index ▁ array ▁ is : " ) ; System . out . println ( Arrays . toString ( index ) ) ; } }
Program to sort an array of strings using Selection Sort | Java 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
import java . util . * ; import java . io . * ; class Main { 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 ; } } } public static void main ( String args [ ] ) { String arr [ ] = { " GeeksforGeeks " , " Practice . GeeksforGeeks " , " GeeksQuiz " } ; int n = arr . length ; System . out . println ( " Given ▁ array ▁ is " ) ; for ( int i = 0 ; i < n ; i ++ ) { System . out . println ( i + " : ▁ " + arr [ i ] ) ; } System . out . println ( ) ; selectionSort ( arr , n ) ; System . out . println ( " Sorted ▁ array ▁ is " ) ; for ( int i = 0 ; i < n ; i ++ ) { System . out . println ( i + " : ▁ " + arr [ i ] ) ; } } }