Recently learned the following are several kinds of sorting methods, record.

Selection sort

let sort = (numbers) => { for(let i=0; i< numbers.length -1; I++) {the console. The log (` -- `) / / this is a very essence the console log. The log (` I: ${i}`) let index = minIndex(numbers.slice(i))+ i console.log(`index: ${index}`) console.log(`min: ${numbers[index]}`) if(index! ==i){ swap(numbers, index, i) console.log(`swap ${index}: ${i}`) console.log(numbers) } } return numbers } let swap = (array, i, j) => { let temp = array[i] array[i] = array[j] array[j] = temp } let minIndex = (numbers) => { let index = 0 for(let i=1; i<numbers.length; i++){ if(numbers[i] < numbers[index]){ index = i } } return index }Copy the code

Quick sort

   let quickSort = arr => {
   if (arr.length <= 1) { return arr; } 
   let pivotIndex = Math.floor(arr.length / 2); 
   let pivot = arr.splice(pivotIndex, 1)[0];
   let left = [];
   let right = [];
   for (let i = 0; i < arr.length; i++){
   if (arr[i] < pivot) { left.push(arr[i])
   } else { right.push(arr[i]) } 
   }
   return quickSort(left).concat( 
   [pivot], quickSort(right) )
   }
  
Copy the code

Merge sort

let mergeSort = arr =>{
let k = arr.length
if(k===1){return arr}
let left = arr.slice(0, Math.floor(k/2))
let right = arr.slice(Math.floor(k/2))
return merge(mergeSort(left), mergeSort(right))
}
let merge = (a, b) => {
if(a.length === 0) return b
if(b.length === 0) return a
return a[0] > b[0] ?
 [b[0]].concat(merge(a, b.slice(1))) :
 [a[0]].concat(merge(a.slice(1), b))
}
Copy the code

Count sorting

let countSort = arr =>{ let hashTable = {}, max = 0, result = [] for(let i=0; i<arr.length; I++){// iterate over the group if(! (arr[i] in hashTable)){ hashTable[arr[i]] = 1 }else{ hashTable[arr[i]] += 1 } if(arr[i] > max) {max = arr[i]} } for(let j=0; j<=max; If (j in hashTable){for(let I = 0; i<hashTable[j]; i++){ result.push(j) } } } return result }Copy the code

Update with new data structures, linked list code, and annotations

const createList = value => { return createNode(value); }; const appendList = (list, value) => { const node = createNode(value); let x = list; while (x.next) { x = x.next; } // x.ext === null //x is the last node x.ext = node; return node; }; const removeFromList = (list, node) => { let x = list; let p = node; // initialize p to node while (x! == node && x ! == null) {// If node is not in the list, x may be null p = x; x = x.next; } if(x === null){// if(x === null){ Return false}else if(x === p){p = x.ext return p NewList = removeFromList(list, list)}else{p.ext = x.ext; Return list if not the first node, return the original list}}; const createNode = value => { return { data: value, next: null }; }; const travelList = (list, fn) => { let x = list; while (x ! == null) { fn(x); x = x.next; }}; const list = createList(10); const node2 = appendList(list, 20); const node3 = appendList(list, 30); const node4 = appendList(list, 40); travelList(list, node => { console.log(node.data); });Copy the code

Tree code

const createTree = value => { return { data: value, children: null, parent: null }; }; const addChild = (node, value) => { const newNode = { data: value, children: null, parent: node }; node.children = node.children || []; node.children.push(newNode); return newNode; }; const travel = (tree, fn) => { fn(tree); if (! tree.children) { return; } for (let i = 0; i < tree.children.length; i++) { travel(tree.children[i], fn); }}; const find = (tree, node) => { if (tree === node) { return tree; } else if (tree.children) { for (let i = 0; i < tree.children.length; i++) { const result = find(tree.children[i], node); if (result) { return result; } } return undefined; } else { return undefined; }}; const removeNode = (tree, node) => { const siblings = node.parent.children; let index = 0; for (let i = 1; i < siblings.length; i++) { if (siblings[i] === node) { index = i; } } siblings.splice(index, 1); }; const tree = createTree(10); const node2 = addChild(tree, 20); const node3 = addChild(tree, 30); addChild(tree, 40); const node5 = addChild(tree, 50); addChild(node2, 201); addChild(node2, 202); addChild(node2, 203); addChild(node2, 204); console.log(tree); const fn = node => { console.log(node.data); }; removeNode(tree, node5); console.log(tree);Copy the code

Daily learning records, please correct any mistakes.