Cara menggunakan javascript algorithm challenge

As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…

medium.com

When you use my referral link above 👆 to become a Medium member, all proceeds will be…

Solve kata with your coding style right in the browser and use test cases (TDD) to check it as you progress. Retrain with new, creative, and optimized approaches. Find all of the bugs in your programming practice.

We have been learning how to solve challenges for algorithms and data structure from various platforms just to improve our problem-solving techniques.

Check out the last challenge linked down below if you missed it:

JavaScript Algorithm and Data Structure Challenge — Fizz Buzz

Fizz Buzz challenge in JavaScript — solution to one of HackerRanks’ programming challenges.

javascript.plainenglish.io

Challenge:

Here we will implement selection sort. Selection sort works by selecting the minimum value in a list and swapping it with the first value in the list. It then starts at the second position, selects the smallest value in the remaining list and swaps it with the second element. It continues iterating through the list and swapping elements until it reaches the end of the list. Now the list is sorted. Selection sort has quadratic time complexity in all cases.

Instruction:

Write a function selectionSort which takes an array of integers as input and returns as an array of these integers in sorted order from the least to greatest.

Notes:

We are calling this function from behind the scenes; the test array we are using is commented out in the editor. Try logging array to see your sorting algorithm in action!

  • selectionSort should be a function
  • selectionSort should return a sorted array (least to greatest)
  • selectionSort should return an array that is unchanged except for order.
  • selectionSort should not use the built-in .sort() method
function selectionSort(array) { // change code below this line
return array;
// change code above this line
}
toSort = [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92];

Our solution

function selectionSort(array) {// change code below this line
for (let index = 0; index < array.length — 1; index++) {
let leastElement = index;
for (let j = index + 1; j < array.length; j++) {
if (array[j] < array[leastElement]) {
leastElement = j;
}
}
// const temp = array[index];
// array[index] = array[leastElement];
// array[leastElement] = temp;
// we can refactor the above code as shown below
[array[index], array[leastElement]] = [array[leastElement], array[index]];
}
return array;
// change code above this line
}toSort = [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92];
console.log(selectionSort(toSort));

Explanation

We will perform the first iteration to find a series of the smallest element in the provided array. Then we will find the next smallest number in the array which will be the length of the array minus 1.

Once we swap the first element with the smallest one in the array, then everything is in order so we don’t need to go back again. We will then perform another loop. For this inner loop remember in our initial for loop we had found the least element in the array now to find the next least number, we will need to go to the next number which in our case would be index + 1.

Simply after the first iteration, some of our numbers will already be locked into place. Also, we have declared the least element to an index to resemble the position of the least swapped number in the given array. We can then use the conditional if statement to test the relevant conditions.

We will test if the array at the j index is smaller than the array at the least index then we are going to set the least element in the index to j. this will go over until the array has been looped over.

Now we will need to swap the elements in the array. We are going to sway the current iteration with the least element. Here we can introduce a temp variable and set it to an array at index and then we go on and swap it array at the least element and likewise the array at least element to the temp variable.

As we have seen from our solution above, it is not a pure function. Why not. Try to log out the original array, we can see that it is mutating the original array and that is not good for pure functional programming.

More refactoring:

We can make a shallow copy of the array and use the shallow copied array instead of the provided array. We can implement this using the .slice() method. This array method enables us to make a shallow copy of the original array.

If you want to know more about array methods check out the article linked down below.

8 JavaScript Array Methods You Should Know

Array methods in JavaScript you need to know with examples.

javascript.plainenglish.io

function selectionSort(array) {// change code below this lineconst shallowCopy = array.slice();
for (let index = 0; index < shallowCopy.length — 1; index++) {
let leastElement = index;
for (let j = index + 1; j < shallowCopy.length; j++) {
if (shallowCopy[j] < shallowCopy[leastElement]) {
leastElement = j;
}
}
// const temp = array[index];
// array[index] = array[leastElement];
// array[leastElement] = temp;
// we can refactor the above code as shown below
[shallowCopy[index], shallowCopy[leastElement]] = [shallowCopy[leastElement], shallowCopy[index]];
}
return shallowCopy;
// change code above this line
}toSort = [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92];
console.log(selectionSort(toSort));
console.log(toSort);

Our function is now behaving like a pure function. Check out the snippet below to confirm.
[ 1, 1, 2, 2, 4, 8, 32, 43, 43, 55, 63, 92, 123, 123, 234, 345, 564]
[ 1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]

Final thoughts

Thank you for reading through this article so far, I hope you found this article helpful in solving this challenge.

Do you have a better solution or have any queries, I would love to hear from you in the comments section?