Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BigO #20

Open
japananh opened this issue Oct 13, 2023 · 0 comments
Open

BigO #20

japananh opened this issue Oct 13, 2023 · 0 comments
Assignees

Comments

@japananh
Copy link
Owner

BigO

Time complexity

Space complexity

  • Space complexity is a measure of the amount of memory or auxiliary space required by an algorithm as a function of the input size. It helps to analyze how efficiently an algorithm uses memory.
  • Use cases: Space complexity is particularly relevant when you need to optimize memory usage, which is important in many real-world scenarios.
  • Time complexity: O(n log n)
  • Space complexity: O(log n)
  • Demo:
package main

import "fmt"

func quickSort(arr []int) {
	if len(arr) <= 1 {
		return
	}

	pivot := arr[0]
	less := make([]int, 0)
	greater := make([]int, 0)

	for _, num := range arr[1:] {
		if num <= pivot {
			less = append(less, num)
		} else {
			greater = append(greater, num)
		}
	}

	quickSort(less)
	quickSort(greater)

	copy(arr, less)
	arr[len(less)] = pivot
	copy(arr[len(less)+1:], greater)
}

func main() {
	arr := []int{3, 6, 8, 10, 1, 2, 1}
	quickSort(arr)
	fmt.Println(arr)
}
@japananh japananh self-assigned this Oct 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant