Skip to content

Instantly share code, notes, and snippets.

@JimRottinger
Last active May 9, 2019 03:10
Show Gist options
  • Save JimRottinger/1383965a17b2caec51e48a80a5e9983e to your computer and use it in GitHub Desktop.
Save JimRottinger/1383965a17b2caec51e48a80a5e9983e to your computer and use it in GitHub Desktop.
const checkLoopInvariant = (newArray, originalArray, index) => {
newArray = newArray.slice(0, index)
originalArray = originalArray.sort((a,b) => a > b ? 1 : - 1).slice(0,index)
if (index === 0) return true //no elements sorted yet
for (let i = 0; i < newArray.length; i++) {
if (!originalArray.includes(newArray[i])) {
console.warn(`Error! ${newArray[i]} not one of the ${index} smallest elements`)
}
if (i < newArray.length - 1 && newArray[i] > newArray[i+1]) {
console.warn(`Error! New Array is not properly sorted`)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment