📘
Grokking-Algorithms book
PostgreSQL
  • Content
  • Introduction to Algorithms
    • Introduction
    • Binary Search
    • Big O notation
    • Recap
  • Selection sort
    • How memory works
    • Arrays and linked lists
    • Selection sort
    • Recap
  • Recursion
    • Recursion
    • Base case and recursive case
    • The stack
    • Recap
  • Quicksort
    • Divide & conquer
    • Quicksort
    • Big O notation revisited
    • Recap
  • Hash tables
    • Hash functions
    • Use cases
    • Collisions
    • Performance
    • Recap
  • Breadth-first search
    • Introduction to graph
    • What is a graph
    • Breadth-first search
    • Implementing the graph
    • Implementing the algorithm
    • Recap
  • Dijkstra's algorithm
    • Working with Dijkstra's algorithm
    • Terminology
    • Trading for a piano
    • Negative-weight edges
    • Implementation
    • Recap
  • Greedy Algorithms
    • The classroom scheduling problem
    • The knapsack problem
    • The set-covering problem
    • NP-complete problems
    • Traveling salesperson, step by step
    • Recap
  • Dynamic programming
    • The knapsack problem
    • Knapsack problem FAQ
    • Longest common substring
    • Recap
  • K-nearest neighbors
    • Classifying oranges vs. grapefruit
    • Building a recommendations system
    • Introduction to machine learning
    • Recap
  • Where to go next
    • Trees
    • Inverted indexes
    • The Fourier transform
    • Parallel algorithms
    • MapReduce
    • Bloom filters and HyperLogLog
    • The SHA algorithms
    • Locality-sensitive hashing
    • Diffie-Hellman key exchange
    • Linear programming
    • Epilogue
  • Answers to exercises
    • Chapter 1
    • Chapter 2
    • Chapter 3
    • Chapter 4
    • Chapter 5
    • Chapter 6
    • Chapter 7
    • Chapter 8
    • Chapter 9
    • Chapter 10
Powered by GitBook
On this page
  • Python
  • Golang
  • Python
  • Golang

Was this helpful?

Edit on GitHub
  1. Recursion

Base case and recursive case

PreviousRecursionNextThe stack

Last updated 1 year ago

Was this helpful?

Rekursiv funktsiya o'zini chaqirganligi sababli, cheksiz tsikl bilan tugaydigan funktsiyani noto'g'ri yozish oson. Misol uchun, siz ortga hisoblashni chop etuvchi funktsiyani yozmoqchisiz, deylik:

> 3...2...1

Siz uni rekursiv tarzda yozishingiz mumkin, masalan:

Python

def countdown(i):
    print i
    countdown(i-1)

Golang

func countdown(i int) {
    fmt.Println(i)
    countdown(i-1)
}

Ushbu kodni yozing va uni ishga tushiring. Muammoni sezasiz: bu funksiya abadiy ishlaydi!

> 3...2...1...0...-1...-2...-3...

(Skriptingizni o'chirish uchun Ctrl-C tugmalarini bosing.) Rekursiv funktsiyani yozganingizda, uni qachon takrorlashni to'xtatish kerakligini aytishingiz kerak. Shuning uchun har bir rekursiv funktsiya ikki qismdan iborat: asosiy va rekursiv holat. Rekursiv holat - bu funksiya o'zini chaqirganda. Asosiy holat - bu funktsiya o'zini qayta chaqirmasa ... shuning uchun u cheksiz tsiklga kirmaydi.

Ortga hisoblash funksiyasiga asosiy registrni qo'shamiz:

Python

def countdown(i):
    print i
    if i <= 0:
        return
    else:
        countdown(i-1)

Golang

func countdown(i int) {
    fmt.Println(i)
    if i <= 0 {
        return
    } else {
        countdown(i-1)
    }
}

Endi funksiya kutilganidek ishlaydi. Bu shunga o'xshash narsaga o'tadi.

Key
Infinitive loop
Base case
Recursive