0

How to get the position of smallest element in an array and swap with that value with neighbor value for "selection sort" using javascript?

var list = [];
Math.max.apply(Math, list); //maximum
Math.min.apply(Math, list); //minimum 


function getInput() {
  var str_one = document.getElementById("one").value;
  var str_two = document.getElementById("two").value;
  var str_three = document.getElementById("three").value;
  var str_four = document.getElementById("four").value;
  var str_five = document.getElementById("five").value;
  var str_six = document.getElementById("six").value;
  list.push(str_one, str_two, str_three, str_four, str_five, str_six);
  $('#qstnDiv').html(list + ',');
}

function output() {

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Untitled Document</title>
  <script type="text/javascript" src="js/script.js"></script>
  <script src="js/jquery-latest.min.js"></script>
  <link type="text/css" href="style.css" rel="stylesheet" />
</head>

<body>
  <form onsubmit="store(this); return false">
    1:<input type="text" id="one" /><br /> 2:
    <input type="text" id="two" /><br /> 3:
    <input type="text" id="three" /><br /> 4:
    <input type="text" id="four" /><br /> 5:
    <input type="text" id="five" /><br /> 6:
    <input type="text" id="six" /><br />
    <input type="button" value="Click Me" onclick="getInput(); output();" />
  </form><br />
  <p id="in">Input</p><br />
  <div id="qstnDiv"></div><br />
  <p id="out">Output</p><br />
  <div id="anwrDiv"></div>

</body>

</html>

I want sort the numbers form the text box using selection sort so please help me

6
  • 1
    Why does it need to be selection sort? Javascript already provides a simple enough .sort() function Commented Oct 4, 2013 at 5:54
  • actually i wanna do the sort function step by step that's why... please help me to find the solution. Commented Oct 4, 2013 at 6:02
  • en.wikipedia.org/wiki/Selection_sort Commented Oct 4, 2013 at 6:03
  • could you please send me the Javascript code for selection sort? Commented Oct 4, 2013 at 6:06
  • bhb linked a pseudo code for selection sort, now rewrite it in JS should not be very difficult. Commented Oct 4, 2013 at 8:31

3 Answers 3

1

there is a specific function for this.

myArray.sort(function(a,b){return b-a});

this is ascending. if you change the order of "a" and "b" it become discending. if you need to access into an object you have to write

myArray.sort(function(a,b){return b.propertyName-a.propertyName});
Sign up to request clarification or add additional context in comments.

Comments

0

Very explicit selection sort!

function selectionSort(arr){
    for (let i = 0; i < arr.length; i++){
        let indexOfMin = i // set index of min to the 
        let j = i // set the index to run the while loop for calculating min
        let min = arr[i] // set the initial value of minimum

        // find the minimum from i to end of array
        while (j < arr.length){
            if (arr[j] < min){
                min = arr[j]
                indexOfMin = j
            }
            j++
        }

        if (indexOfMin !== i){ // only swap if the index of minimum and curr item is different
            let tmp = arr[i]
            arr[i] = arr[indexOfMin]
            arr[indexOfMin] = tmp
            console.log(arr)
        }
    }
    return arr
}

sortedList = selectionSort([5,3,4,1,-3,2])

Comments

0

Selection sort: swapping doesn't occur like in bubble sort, rather it happens once per iteration setting the min value in array.

const selectionSort = (arr) => {
    for(let i=0; i<arr.length; i++) {
       let lowest = i;
        for(let j=i+1; j<arr.length; j++) {
            if(arr[lowest] > arr[j]) {
                lowest = j;
            }
        }
        if(i != lowest) {
           // SWAP!!
           [arr[lowest], arr[i]] = [arr[i], arr[lowest]]; 
        }
    }
    return arr;
}

console.log(selectionSort([8,1,2,3,4,5,6]));

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.