Search code examples
javaarrayssortingselection-sort

SelectionSort algorithm replace all the elements with zeros


Main class:

import java.util.Random;

public class Main{
    public static void main(String args[]){
        Main m = new Main();
        m.run();
    }
    
    private void run(){
        Random r = new Random();
        int[] array = new int[50];
        
        for(int el:array){
            el = r.nextInt(50);
            System.out.print(el + " ");
        }
        System.out.println("");
        
        long start = System.currentTimeMillis();
        SelectionSort.run(array,50);
        long finish = System.currentTimeMillis();
        long timeElapsed = finish - start;
        
        for(int el:array){
            System.out.print(el + " ");
        }
        System.out.println("");
        System.out.println(timeElapsed);
    }
}

SelectionSort class:

public class SelectionSort{
    public static void run(int[] array, int size){
        int m = 0;
        int temp = 0;
        for (int i=0; i<size-1; ++i){
            m = i;
            for (int j=i+1; j<size; ++j){
                if (array[m]>array[j]){
                    m = j;
                }
            }
            temp = array[i];
            array[i] = array[m];
            array[m] = temp;
        }
    }
}

Output:

$ java Main
35 19 29 0 42 42 21 33 9 2 48 20 5 40 17 37 35 24 32 29 2 1 32 30 32 17 23 13 5 8 32 34 12 10 29 18 10 0 12 49 8 11 10 1 26 9 12 14 18 25 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
1

Why there are all the zeros? What am I doing wrong? Really don't know what can be... I edited my code multiple times, still no solution... I'm a newbie, could you help me?

I know that java has methods for sorting, but I am doing some exercise.


Solution

  • int[] array = new int[10];
    int i = 0;
    
    for (int el : array) {
        el = r.nextInt(50);
        array[i++] = el;
        System.out.print(el + " ");
    }
    

    You forget to fill the array.


    public class Foo {
    
        public static void main(String args[]) {
            Foo m = new Foo();
            m.run();
        }
    
        private void run() {
            int[] arr = createRandomArray(50);
            System.out.println(Arrays.toString(arr));
    
            long start = System.currentTimeMillis();
            new SelectionSort().accept(arr);
            long time = System.currentTimeMillis() - start;
    
            System.out.println(Arrays.toString(arr));
            System.out.println("time (ms): " + time);
        }
    
        private static int[] createRandomArray(int length) {
            Random random = new Random();
            int[] arr = new int[length];
    
            for (int i = 0; i < arr.length; i++)
                arr[i] = random.nextInt(50);
    
            return arr;
        }
    }
    
    final class SelectionSort implements Consumer<int[]> {
    
        @Override
        public void accept(int[] arr) {
            for (int i = 0, j = i; i < arr.length - 1; i++, j = i) {
                for (int k = i + 1; k < arr.length; k++)
                    j = arr[k] < arr[j] ? k : j;
    
                swap(arr, i, j);
            }
        }
    
        private static void swap(int[] arr, int i, int j) {
            int tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
        }
    }