Selection Sort

This is a ChelseaJS visualization of the Selection Sort algorithm.
Selection Sort is a simple sorting algorithm that works by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array.
Time Complexity: O(n2)

Selection sort can be good at checking if everything is already sorted. It is also good to use when memory space is limited. This is because unlike other sorting algorithms, selection sort doesn't go around swapping things until the very end, resulting in less temporary storage space used.

Code

            
    elem=document.querySelector(".example-container");
    setCanvas(elem);
    arr=[];
    for(i=0;i<WIDTH/2;i++){
        arr.push(random(0,HEIGHT));
    }
    k=0;
    
    function draw(){
        clearCanvas();
        
        min_idx = k;
        for(j=k+1;j<arr.length;j++){                //find minimum element
            if(arr[min_idx]>arr[j]){                         
                min_idx=j;
            }
        }
            
        [arr[min_idx] , arr[k]] = [arr[k],arr[min_idx]];   //es6 destructuring to swap elements
        
        for(i=0;i<arr.length;i++){
            x=mapRange(i,0,arr.length,0,WIDTH);
            new line(x,HEIGHT,x,HEIGHT-arr[i],'#695fe6',2);
        }
            
        if(k<WIDTH/2-1){
            k++;
        }
            
        requestAnimationFrame(draw);
    }
    draw()