2

I have two arrays - a, with 5 values and s which is empty. What I have to do is to find the smallest value in a, add it to s and after adding it to s, to delete it from a. Here is my code:

            class Sorting {
                constructor () {
                    let getArr = document.getElementById('t');
                }

            findMin () {
                let a = [23, 30, 9, 10, 26];
                let s = [];

                for (let i = 0; i < a.length; i++) {
                    let min = i;
                    for (let j = i + 1; j < a.length; j++) {
                    if  (a[j] < a[min]) {
                    min = j;
                    }
                }
                
                s.push(a[min]);

                a.splice(min, 1);
                
                    if (i !== min) {
                    let x = a[i];
                    a[i] = a[min];
                    a[min] = x;                    
                    }
            
                }
                
                console.log(s)
                console.log(a)
            }
        }
            
        function main() {
        let p = new Sorting ();
        p.findMin();
    }

What I'm unable to do is to make my program delete the element from a properly after adding it to s.

If I delete the line a.splice(min, 1) and just leave s.push(a[min]), I get all the values from a into s. If I leave them both however, that's what I get as a result:

s: [9, 23, 30]

a: [10, 26]

And I want to get:

s: [9, 10, 23, 26, 30]

a: []

Can anyone please tell me why I get this and how can I fix it? Any help would be greatly appreciated.

4
  • 1
    please add the wanted result. Commented May 16, 2021 at 17:27
  • So you just want a sorted copy of the array? Commented May 16, 2021 at 17:40
  • Yes, a sorted copy in a new array (s) and to have array a empty as well. Commented May 16, 2021 at 17:47
  • What exactly stops you from makings = a.sort((a,b) => a - b); a = [];? Commented May 16, 2021 at 19:00

3 Answers 3

1

You could simply check the value and splice the array with the found index and take this array for pushing.

const
    a = [23, 30, 9, 10, 26],
    s = [];

while (a.length) {
    let i = 0;
    for (let j = 1; j < a.length; j++) {
        if (a[j] < a[i]) i = j;
    }
    s.push(...a.splice(i, 1));
}

console.log(a);
console.log(s);

Sign up to request clarification or add additional context in comments.

Comments

1
let a = [25,2,5,28,10,32];
let s = [];
var previousVal;
var index;

//least value find
for (let i = 0; i< a.length - 1; i++){
    if(i == 0){
        previousVal = a[i];
    }
    var result = previousVal - a[i+1];
    if(result > 0){
        //previousVal is grater than a[i+1]
        previousVal = a[i+1];
        index = i+1;
    }
}

console.log("Index = " + index + " and Number = " + previousVal);
a.splice(index, 1);
s.push(previousVal);    
console.log(a);
console.log(s);

Hope this helps. I approached using different method to find the least value first and index in the array then removed that from array 'a' and put it in array 's'. You could remove 'previousValue' at the end and just use 'index'. Just push it before splicing. Like,

s.push(a[index]);
a.splice(index,1);

Comments

0

How about slightly different approach with this problem? somthing like

let a = [23, 30, 9, 10, 26];
let s = [];
let sorted = a.slice().sort((a, b) => a - b);
s[0] = sorted[0]; //our s is now smallest number
a.splice(a.findIndex(elem => elem == s[0]), 1)
console.log(a);
console.log(s);

Here we finding smallest number in array by sorting it from smallest to largest. To delete it we finding index of our smallest number with .findIndex() and delete it with .splice

1 Comment

this is kind of clever, but after sorting you could just splice thw whole array and takie it as new array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.