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

feat: add sort module to stdlib #416

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

HuangHongkai
Copy link

@HuangHongkai HuangHongkai commented Mar 7, 2023

related issue: #398
sort module source code

_sort := func(arr, left, right, less) {
	if right-left <= 0 {
		return arr
	}
	i := left

	for j := left; j < right; j++ {
		if less(j, right) {
			if i != j {
				tmp := arr[i]
				arr[i] = arr[j]
				arr[j] = tmp
			}
			i++
		}
	}

	if i != right {
		tmp := arr[i]
		arr[i] = arr[right]
		arr[right] = tmp
	}

	_sort(arr, left, i-1, less)
	_sort(arr, i+1, right, less)
	return arr
}

export {
	sort: func(arr, less) {
		return _sort(arr, 0, len(arr)-1, less)
	}
}

@geseq
Copy link
Collaborator

geseq commented Mar 18, 2023

stdlib/source_modules.go Show resolved Hide resolved
@NAICOLAS
Copy link
Contributor

@geseq Any updates on this? :)

@NAICOLAS
Copy link
Contributor

@HuangHongkai

@NAICOLAS
Copy link
Contributor

// Quicksort
_swap := func(arr, i, j) {
	tmp := arr[i]
	arr[i] = arr[j]
	arr[j] = tmp
}

_partition := func(arr, low, high, less) {
	pivot := arr[high]
	i := low - 1
	
	for j := low; j < high; j++ {
		if less(arr[j], pivot) {
			i++
			_swap(arr, i, j)
		}
	}
	
	_swap(arr, i+1, high)
	
	return i + 1
}

_quicksort := func(arr, low, high, less) {
	if low < high {
		pivotIndex := _partition(arr, low, high, less)
		_quicksort(arr, low, pivotIndex-1, less)
		_quicksort(arr, pivotIndex+1, high, less)
	}
}

qsort := func(arr, less) {
	_quicksort(arr, 0, len(arr)-1, less)
	
	return arr
}

@HuangHongkai
Copy link
Author

@geseq hello, I updated my code, please help to review

@HuangHongkai
Copy link
Author

Inline the swap function to reduce function calls

@HuangHongkai HuangHongkai requested a review from geseq June 21, 2023 07:29
stdlib/stdlib_test.go Show resolved Hide resolved
@huixiaohuang
Copy link

huixiaohuang commented Mar 19, 2024

hi @HuangHongkai, I am afraid some of the unit test cases in your commit won't work. would you like to try the sorting code below and request another PR? I believe it should work. let me know if any questions :)

_swap := func(arr, left, right) {
    if arr == undefined || len(arr) <= 1 || left < 0 || left >= len(arr) || right < 0 || right >= len(arr) || left >= right {
        return
    }
    temp := arr[right]
    arr[right] = arr[left]
    arr[left] = temp
}

_sort := func(arr, left, right, less) {
    if arr == undefined || len(arr) <= 1 || left < 0 || left >= len(arr) || right < 0 || right >= len(arr) || left >= right {
        return arr
    }
    idx := left
    for i := left; i < right; i++ {
        if less(i, right) {
            _swap(arr, idx, i)
            idx++
        }
    }
    _swap(arr, idx, right)
    _sort(arr, left, idx-1, less)
    _sort(arr, idx+1, right, less)
    return arr
}

export {
    sort: func(arr, less) {
        if arr == undefined || len(arr) <= 1 {
            return arr
        }
        return _sort(arr, 0, len(arr)-1, less)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants