-1

I have array like this value = [250, 200, 300, 150, 300]

I use this code.

        for (var j = 0; j < value.length - 1; j += 1)
        {
            if (value[j] > value[j + 1])
            {
                var temp = value[j + 1];
                value[j + 1] = value[j];
                value[j] = temp;
            }
        }

But,it's not working. It's results value = [200, 250, 150, 300, 300]

I want to acheive this without using inbuilt function.

6
  • 3
    with one loop, you do not get a sorted array. Commented Nov 29, 2016 at 12:29
  • Single loop sorting is not possible. You can either go with internal sorting method developer.mozilla.org/en/docs/Web/JavaScript/Reference/… or use sorting algorithms khan4019.github.io/front-end-Interview-Questions/sort.html Commented Nov 29, 2016 at 12:31
  • Why wouldn't you just do value.sort() Commented Nov 29, 2016 at 12:34
  • 2
    Look at this answer in SO which has a lot of links to different sorting algorithms, porperly explained and in pseudocode. Commented Nov 29, 2016 at 12:37
  • 1
    @adeneo because the teacher wants them to implement it that way.... Commented Nov 29, 2016 at 12:38

3 Answers 3

1

use the below code.

var value = [250, 200, 300, 150, 300];

for (var i = 0; i < value.length; i++) {
  var swapped = false
  for (var j = 0; j < value.length; j++) {
    if (value[j] > value[j + 1]) {
      temp = value[j + 1];
      value[j + 1] = value[j];
      value[j] = temp;
      swapped = true;
    }
  }
  if (!swapped) {
    break;
  }
}
console.log(value)

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

3 Comments

Please explain your attempt. Just putting working code is not good enough
it needs 2 loop as defined in bubble sort algorithm to check each pass parellely as defined in algorithm tutorialspoint.com/data_structures_algorithms/…
I know the algo for bubble sort. What I meant was, explain your attempt in your answer, so that if someone without enough knowledge reads your answer, he understand whats happening.
1
var numbers = [10, 3, 5, 1, 88, 6, 12, 28, 16]
for (var i = 0; i < numbers.length; i++) {
   for (var j = 0; j < numbers.length; j++) {
      if (numbers[i] < numbers[j]) {
          var temp = numbers[j];
          numbers[j] = numbers[i];
          numbers[i] = temp;
      }
   }
}

console.log(numbers);

1 Comment

Welcome to SO! Please don't post code-only answers but add a little textual explanation about how and why your approach works and what makes it different from the other answers given. You may also have a look at our "How to write a good answer" entry.
-1

You should have two loops one inside other to sort array

value = [250, 200, 300, 150, 300]
for (var i = 0; i < value.length; i++)
  for (var j = i; j < value.length - 1; j++) {
    if (value[i] > value[j]) {
      var temp = value[j];
      value[j] = value[i];
      value[i] = temp;
    }
  }

console.log(value)

Comments

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.