text: Sort prime numbers of an array in descending order | C # implementation of the approach ; false here indicates that it is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function that sorts all the prime numbers from the array in descending ; this vector will contain prime numbers to sort ; if the element is prime ; update the array elements ; Driver code ; print the results .
code: using System ; using System . Collections . Generic ; class GFG { static bool [ ] prime = new bool [ 100005 ] ; static void SieveOfEratosthenes ( int n ) { for ( int i = 0 ; i < 100005 ; i ++ ) prime [ i ] = true ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i < n ; i += p ) { prime [ i ] = false ; } } } } static void sortPrimes ( int [ ] arr , int n ) { SieveOfEratosthenes ( 100005 ) ; List < int > v = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { v . Add ( arr [ i ] ) ; } } v . Sort ( ) ; v . Reverse ( ) ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { arr [ i ] = v [ j ++ ] ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 3 , 2 , 6 , 100 , 17 } ; int n = arr . Length ; sortPrimes ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } }