# Randomize shit

```const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const shuffledArray = array.sort((a, b) => 0.5 - Math.random());

// using Array map and Math.random
const shuffledArr = array => array.map(a => ({ sort: Math.random(), value: a })).sort((a, b) => a.sort - b.sort).map(a => a.value);

```
```const shuffleArray = array => {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
```
```const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const shuffledArray = array.sort((a, b) => 0.5 - Math.random());

// using Array map and Math.random
const shuffledArr = array => array.map(a => ({ sort: Math.random(), value: a })).sort((a, b) => a.sort - b.sort).map(a => a.value);

```
```function shuffle(array) {
const curIdx = array.length;

// While there remain elements to shuffle...
while (0 !== cur_idx) {
// Pick a remaining element...
const randIdx = Math.floor(Math.random() * curIdx);
curIdx -= 1;

// And swap it with the current element.
const originalIdx = array[curIdx];
array[curIdx] = array[randIdx];
array[randIdx] = originalIdx;
}
return array;
}

// Usage of shuffle
const arr = [1, 2, 3, 4, 5, 6];
arr = shuffle(arr);
console.log(arr);

// shorter
function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var originalIdx = array[i];
array[i] = array[j];
array[j] = originalIdx;
}
}

//es6
function shuffle_array(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}```
```Example: Shuffle Deck of Cards

// program to shuffle the deck of cards

// declare card elements
const suits = ["Spades", "Diamonds", "Club", "Heart"];
const values = [
"Ace",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"Jack",
"Queen",
"King",
];

// empty array to contain cards
let deck = [];

// create a deck of cards
for (let i = 0; i < suits.length; i++) {
for (let x = 0; x < values.length; x++) {
let card = { Value: values[x], Suit: suits[i] };
deck.push(card);
}
}

// shuffle the cards
for (let i = deck.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * i);
let temp = deck[i];
deck[i] = deck[j];
deck[j] = temp;
}

console.log('The first five cards are:');

// display 5 results
for (let i = 0; i < 5; i++) {
console.log(`\${deck[i].Value} of \${deck[i].Suit}`)
}

//output
The first five cards are:
4 of Club
5 of Diamonds
Jack of Diamonds
2 of Club
```function randomEl(array) {
const idx = Math.floor(Math.random() * array.length);
return array[idx];
}```
```const _mergeArrays = (a, b) => {

const c = []

while (a.length && b.length) {

c.push(a[0] > b[0] ? b.shift() : a.shift())

}

//if we still have values, let's add them at the end of `c`

while (a.length) {

c.push(a.shift())

}

while (b.length) {

c.push(b.shift())

}

return c

}

const mergeSort = (a) => {

if (a.length < 2) return a

const middle = Math.floor(a.length / 2)

const a_l = a.slice(0, middle)

const a_r = a.slice(middle, a.length)

const sorted_l = mergeSort(a_l)

const sorted_r = mergeSort(a_r)

return _mergeArrays(sorted_l, sorted_r)

}```
```function partition(arr, startIndex, endIndex) {

const pivotVal = arr[endIndex]; // the pivot element

let index = startIndex;

// begin iterate and swap

for (let i = index; i < endIndex; i++) {

if (arr[i] < pivotVal) {

[arr[i], arr[index]] = [arr[index], arr[i]];

index += 1;

}

}

// move `pivotVal` to the middle index and return middle index

[arr[index], arr[endIndex]] = [arr[endIndex], arr[index]];

return index;

}

function quickSort(arr, startIndex, endIndex) {

// Base case or terminating case

if (startIndex >= endIndex) {

return;

}

// Returns midIndex / pivot index

let midIndex = partition(arr, startIndex, endIndex);

// Recursively apply the same logic to the left and right subarrays

quickSort(arr, startIndex, midIndex - 1);

quickSort(arr, midIndex + 1, endIndex);

}

let arr = [-2, 4, 6, 3, 7, 2];

quickSort(arr, 0, arr.length - 1);

console.log(arr); // [-2, 2, 3, 4, 6, 7]```