GithubHelp home page GithubHelp logo

hello-world's Introduction

Algos

Sorting Algorithms

A: List or an array of elements.

Bubble Sort

    Size = length(A)
    for i = 0 to Size-1 do
      for j = 0 to Size-i-1 do
        if A[j] > A[j+1] do
          swap A[j] and A[j+1]
        end if
      end for
    end for
    return A

Worst Case: ะž(n^2)
Best Case: O(n)
Average Case: O(n^2)

Insertion Sort

    Size = length(A)
    for i = 1 to Size do
      val = A[i]
      hole = i
      while hole > 0 and A[hole - 1] > val then
        A[hole] = A[hole - 1]
        hole = hole - 1
      end while
    end for
    return A

Worst Case: O(n^2)
Best Case: O(n)
Average Case: O(n^2)

Merge Sort

    n1 = mid - leftstart + 1
    n2 = right
    left = []
    right = []
    for i = 0 to n1 do
      left.append(A[leftstart + i])   #To add elements to left array.
    end for
    for i = 0 to n2 do
      right.append(A[mid + 1 + i])    #To add elements to right array.
    end for
    k = leftstart, i = 0, j = 0
    while i < n1 and j < n2 then
      if left[i] <= right[j] then
        A[k] = left[i]
        k = k + 1, i = i + 1
      else
        A[k] = right[j]
        k = k + 1, j = j + 1
      end else
      end if
    end while
    while i < n1 do
      A[k] = left[i]
      k = k + 1, i = i + 1
    end while
    while j < n2 do
      A[k] = right[j]
      k = k + 1, j = j + 1
    end while


  merge_sort(A, leftstart, rightend)
    if leftstart >= rightend then
      return
    end if
    mid = (leftstart + rightend) / 2
    merge_sort(A, leftstart, mid)
    merge_sort(A, mid + 1, rightend)

    mergeHalves(A, leftstart, mid, rightend)

Worst Case: O(n logn)
Best Case: O(n logn) Typical, O(n) natural variant.
Average Case: O(n logn)

Quick Sort

    pivot = A[end]
    partitionIndex = start
    for i = start to end do
      if A[i] <= pivot then
        swap(A[i], A[partitionIndex])
        partitionIndex = partitionIndex + 1
      end if
    end for
    swap(A[partitionIndex], A[end])
    return A


  quick_sort(A, start, end)
    if start < end do
      partitionIndex = partition(A, start, end)
      quick_sort(A, start, partitionIndex - 1)
      quick_sort(A, partitionIndex + 1, end)
    end if
    return A

Worst Case: O(n^2)
Best Case: O(n logn)
Average Case: O(n logn)

Selection Sort

    Size = length(A)
    for i = 0 to Size - 1 do
      minIndex = i
      for j = i + 1 to Size - 1 do
        if A[j] > A[minIndex] then
          minIndex = j
        end if
      end for
    swap A[i] and A[minIndex]
    return A

Worst Case: O(n^2)
Best Case: O(n^2)
Average Case: O(n^2)

hello-world's People

Contributors

ank7tz avatar

Watchers

James Cloos avatar

Forkers

hritiks

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.