Giter VIP home page Giter VIP logo

algorithms's Introduction

Algorithms

Learing algorithms by reading books, watching videos, looking at others code and many more... The best way of doing this is by coding in practice.

What is recursion?

In computer science, recursion is a programming technique using function or algorithm that calls itself one or more times until a specified condition is met at which time the rest of each repetition is processed from the last one called to the first.

What is Divide and conquer?

In computer science, divide and conquer is an algorithm design paradigm. A divide and conquer algorithm is a strategy of solving a large problem by. breaking the problem into smaller sub-problems. solving the sub-problems, and. combining them to get the desired output.

d&c

A divide-and-conquer algorithm recursively breaks down a problem into two or more sub-problems of the same or related type, until these become simple enough to be solved directly.

Quicksort is a sorting algorithm. The algorithm picks a pivot element and rearranges the array elements so that all elements smaller than the picked pivot element move to the left side of the pivot, and all greater elements move to the right side. Finally, the algorithm recursively sorts the subarrays on the left and right of the pivot element.

  • Using recursion technique
  • Divide and conquer technique or strategy

quicksort

Steps to implement of quicksort algorithm

  • Find the pivot (lets pick it from the first or the last element of an array)
  • Now find the elements smaller than the pivot and the elements larger than the pivot.

    This is called Partitioning, now we have

    • A sub-array of all the numbers less than the pivot (not sorted yet)
    • The pivot
    • A sub-array of all the numbers greater than the pivot (not sorted yet)

Quicksort algorithm JS implementation

function quicksort(numbers){

  //base case 1, empty array
  if (numbers.length===0){
    return numbers
  }

  //base case 2, array with one element
  if (numbers.length===1){
    return numbers
  }

  //base case 3, array with two element
  if (numbers.length===3){
    if (numbers[0]<numbers[1]){
        numbers[0]=numbers[1];
        numbers[1]=numbers[0];
    }
    return numbers

  }

  //recursive case
  var pivot=numbers[0];
  var left=[];
  var right=[];

  for(let x of numbers.slice(1)){
    if (x<pivot){
        left.push(x);
    }
    //left.push(pivot);
    if (x>pivot){
        right.push(x);
    }
  }
  return quicksort(left).concat([pivot],quicksort(right));

}

Time Complexity

O(n log n)

More precise version

function quicksort(numbers) {

    if (numbers.length < 2) {
        return numbers;

    } else {
        var pivot = numbers[0];
        var left = numbers.slice(1).filter(function(x) {
            return x <= pivot;
        });
        var right = numbers.slice(1).filter(function(x) {
            return x > pivot;
        });
        return quicksort(left).concat([pivot], quicksort(right));
    }
}

Golang version of quicksort

func quicksort(numbers []int) []int {
	if len(numbers) < 2 {
		return numbers
	} else {
		pivot := numbers[0]
		left := []int{}
		right := []int{}
		for _, i := range numbers[1:] {
			if i <= pivot {
				left = append(left, i)
			} else {
				right = append(right, i)
			}
		}
		return append(append(quicksort(left), pivot), quicksort(right)...)
	}
}

algorithms's People

Contributors

mateors avatar

Stargazers

 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.