Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Create a Selection Sort Function in JavaScript

    Tyler ClarkTyler Clark
    javascriptJavaScript

    While not the fastest sorting algorithm, the selection sort function sorts an array by first creating a new array, calling a function that loops over a list and returns the location of the max/min element, and then pushes that returned indexed value into the newly created array. After the value is added it is then removed from the original list and the process continues until there is nothing left.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: Let's define an array of items we need to sort. Inside of it, we'll give it a three, two, four, one, and six. Now, we before we create and our selection and sort function, we need to first write a function that loops through this array, and gives us the index and the largest number.

    Then once we have that, we can use our selection sort function to create a new list, call this function, and then remove it from the original list. For that first function, we'll do function findLargestValue, which will take a list.

    Then we'll do let large E equals list of zero. Let index of large equal zero. We'll do a for loop, where we start with one, and while our I is less than our list.length, we're going to increment our I. If large E is less than list at I, then we're going to update our large value with that list at I, and update our index of large as well.

    Then finally, we'll return this function, returning the index of large. To recap, this findLargestValue function takes an array of items, loops through that list, and then returns the index location of the largest value within this list.

    We start out defining the index and the item with the first item, and then if anything appears largest inside of our loop, we update it. Now, with that ready to go, we can create our selection sort function. We'll do that function, selectionSort. It takes a list.

    Inside of that, we'll create a new array called newList. We'll define our new large item. Then we'll create a while loop where as long as our list has values inside of it, we're going to update that large item, equal to calling our findLargestValue function, passing in this list.

    That's going to return, and we're going to push onto our new list the return value from our findLargestValue function. Once we've found the largest value from our list, we want to remove it, so we don't count it again as we push values into this new list that we've created.

    After we remove this, we're going to return our function, which is returning the new list that we've created. With that in place, we can call this function, inside of a console.log. We'll pass in our items to sort, which we created earlier.

    We'll see that our array is now sorted, six, four, three, two, one, which is correct. Now, let's go back and review this function one more time. The first thing we do is create a new array. This is the array that we're going to return with this function that we see printed here in the console.log.

    The second thing we do is use this while loop, where we call our findLargestValue function, passing in the current list that we have been provided. Once this findLargestValue function returns, it gives us the index location of the current largest item.

    Which we push onto this new array, and then we also remove it right after that. Because we need to call our findLargestValue function for N number of times -- or in other words, we have to call our function a total of however long our list is -- this gives our selectionSort function a beginning big O notation of O(n).

    Then if we remember our findLargestValue function also has a loop inside of it, stepping through another N times with its provided list, this makes our big O notation O(n*n), or n². selectionSort gets the job done, but it's not very fast. Quick sort and merge sort are more efficient, and have a notation of O(n)log(n).