Skip to content

Latest commit

 

History

History

sorting-algorithms

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Sorting Algorithms

Setting intent

Determine, as a class, what the intent of today should be

Trivia Questions

What is the value of newArr

const newArr = [...[1, 2, 3], ...[4, 5, 6]];

What is the value of color for newCar What is the value of color for secondNewCar

class Car {
  constructor(brand, model, color = "soul red") {
    this.brand = brand;
    this.model = model;
    this.color = color;
  }
}

const newCar = new Car("Mazda", "MX-30");
const newCar = new Car("Porche", "Vintage Carrera", "Yellow");

Main Problem

Here we have our Deck of Cards class that has been simplified to generate cards that just have a numeric value. We can generate a deck of shuffled cards. We want to create a method that will put the cards back in order.

Write your own sort method that sorts the cards from 2 - 11. Do not use the array method .sort()

class DeckOfCards {
  constructor() {
    this.cards = [];
    this.createDeck();
    this.shuffle(this.cards);
  }
  createDeck() {
    for (let i = 1; i <= 13; i++) {
      if (i === 1) {
        this.cards.push(11);
      } else if (i === 11) {
        this.cards.push(10);
      } else if (i === 12) {
        this.cards.push(10);
      } else if (i === 13) {
        this.cards.push(10);
      } else {
        this.cards.push(i);
      }
    }
  }
  sort(array) {
    // your code
  }
  shuffle(array) {
    let m = array.length;
    let i = 0;
    // While there remain elements to shuffle…
    while (m) {
      // Pick a remaining element…
      i = Math.floor(Math.random() * m--);

      // And swap it with the current element.
      [array[m], array[i]] = [array[i], array[m]];
    }
    return array;
  }
}

const deck = new DeckOfCards();
console.log(deck);

Next Steps

Use this visualization to learn more about different sorting algorithms

Another visualization using colors and pixels

More visuals

Watch a video about two different sorting algorithms

Lab: Accumulate Points on Code Wars & Practice with JavaScript's Sort Method