GithubHelp home page GithubHelp logo

curly-octo-telegram's Introduction

Reverse Insertion Sort

Consider the code for insertion sort we covered in class:

function insertionSort(arr) {
  for(var i = 1; i < arr.length; i++) {
    var val = arr[i];
    var j;
    for(j = i; j > 0 && arr[j-1] > val; j--) {
      arr[j] = arr[j-1];
    }
    arr[j] = val;
  }
  return arr;
}

Change this function such that it works from the end of the array rather than the beginning, insertionSortReverse() -- only the direction of iterating over the elements is reversed, the array is still sorted the same way (ascending). Add your code in code.js. Test your new function; I've provided some basic testing code that uses jsverify in code.test.js.

Average-Case Time Complexity of Insertion Sort

In the lectures, we covered that insertion sort has best-case time complexity of $\Theta(n)$ and worst-case time complexity of $\Theta(n^2)$. What is the average-case time complexity ($\Theta$)?

Hint: Think about what happens in each iteration of the loop, and how often the loop is executed. Keep in mind that for asymptotic analysis we don't care about constant factors.

Describe your reasoning and the conclusion you've come to. Your reasoning is most important -- you can easily find the answer, but you need to demonstrate that you've understood the concept. Add your answer to this markdown file.

curly-octo-telegram's People

Contributors

richmiles avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.