Counting Sort 1

Sort by

recency

|

667 Discussions

|

  • + 0 comments

    import java.io.; import java.math.; import java.security.; import java.text.; import java.util.; import java.util.concurrent.; import java.util.function.; import java.util.regex.; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList;

    class Result {

    /*
     * Complete the 'plusMinus' function below.
     *
     * The function accepts INTEGER_ARRAY arr as parameter.
     */
    
    public static void plusMinus(List<Integer> arr) {
    // Write your code here
        int size = arr.size();
        double negNumbers = 0;
        double posNumbers = 0;
        double numZeros = 0;
        for (int num : arr) {
            if (num < 0) {
                negNumbers++;
            } else if (num > 0) {
                posNumbers++;
            } else {
                numZeros++;
            }
        }
        System.out.println(String.format("%.5f", posNumbers/size));
        System.out.println(String.format("%.5f", negNumbers/size));
        System.out.println(String.format("%.5f", numZeros/size));
    }
    

    }

    public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

        int n = Integer.parseInt(bufferedReader.readLine().trim());
    
        List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
            .map(Integer::parseInt)
            .collect(toList());
    
        Result.plusMinus(arr);
    
        bufferedReader.close();
    }
    

    }

  • + 0 comments

    Java

    public static List countingSort(List arr) { // Write your code here List the100 = new ArrayList<>();

    int init = 0;

    while(init<100){

    the100.add(0);
    
    init++;
    

    }

    for(Integer val: arr){

    int temp = the100.get(val);
    
    temp++;
    
    the100.set(val,temp);
    

    }

    return the100; }

    }

  • + 0 comments

    Swift ; ıf you r swift dev just follow or contact me

    func countingSort(arr: [Int]) -> [Int] {
        
        var countingArray = [Int](repeating: 0, count: 100)
        
        for num in arr{
            countingArray[num] += 1
        }
        
        return countingArray
    }
    
  • + 1 comment

    JS:

    /*
     * Complete the 'countingSort' function below.
     *
     * The function is expected to return an INTEGER_ARRAY.
     * The function accepts INTEGER_ARRAY arr as parameter.
     */
    
    function countingSort(arr) {
        const zeroes = Array(100).fill(0)
        for (const num of arr) zeroes[num]++
        
        return zeroes
    }
    
  • + 0 comments

    My answer with Typescript, stuck a while cause dk 100 fixed size of result

    function countingSort(arr: number[]): number[] {
        let size = 100 // arr.length
        let frequency = new Array<number>(size).fill(0)
    
        for (let num of arr) frequency[num] = (frequency[num] || 0) + 1
    
        return frequency
    }