-
Notifications
You must be signed in to change notification settings - Fork 1
feat: use loops instead of recursion #21
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
15c297f
wip using loops instead of recursion, update safey comments, add tests
michaeljklein 03df53a
wip debugging quicksort_explicit (loop) failure
michaeljklein 20dbb64
found the bug! wip cleaning up and responding to review comments
michaeljklein b8f6ed2
add 100-value test, fix non-explicit quicksort, cleanup, nargo fmt
michaeljklein 0083a52
inline Swap trait
michaeljklein 39b4232
use 'for' loops for backwards compatibility and add TODO's for them
michaeljklein fcb4582
use non-docstring loops for backwards compatibility
michaeljklein ec89fff
Update src/lib.nr
michaeljklein aa44f9a
Update src/quicksort/quicksort.nr
michaeljklein 0ac9d9d
Update src/quicksort/quicksort.nr
michaeljklein 87be0f2
simplify inlined swap function and 'nargo fmt'
michaeljklein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| pub mod quicksort; | ||
| pub mod quicksort_explicit; | ||
| pub mod quicksort_recursive; | ||
| pub mod quicksort_recursive_explicit; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| pub trait Swap { | ||
| fn swap(&mut self, i: u32, j: u32); | ||
| } | ||
|
|
||
| impl<T, let N: u32> Swap for [T; N] { | ||
| fn swap(&mut self, i: u32, j: u32) { | ||
| let temp = self[i]; | ||
| self[i] = self[j]; | ||
| self[j] = temp; | ||
| } | ||
| } | ||
|
|
||
| unconstrained fn partition<T, let N: u32>(arr: &mut [T; N], low: u32, high: u32) -> u32 | ||
| where | ||
| T: std::cmp::Ord, | ||
| { | ||
| let pivot = high; | ||
| let mut i = low; | ||
| for j in low..high { | ||
| if (arr[j] < arr[pivot]) { | ||
| arr.swap(i, j); | ||
| i += 1; | ||
| } | ||
| } | ||
| arr.swap(i, pivot); | ||
| i | ||
| } | ||
|
|
||
| unconstrained fn quicksort_recursive<T, let N: u32>(arr: &mut [T; N], low: u32, high: u32) | ||
| where | ||
| T: std::cmp::Ord, | ||
| { | ||
| if low < high { | ||
| let pivot_index = partition(arr, low, high); | ||
| if pivot_index > 0 { | ||
| quicksort_recursive(arr, low, pivot_index - 1); | ||
| } | ||
| quicksort_recursive(arr, pivot_index + 1, high); | ||
| } | ||
| } | ||
|
|
||
| pub unconstrained fn quicksort<T, let N: u32>(_arr: [T; N]) -> [T; N] | ||
| where | ||
| T: std::cmp::Ord, | ||
| { | ||
| let mut arr: [T; N] = _arr; | ||
| if arr.len() <= 1 {} else { | ||
| quicksort_recursive(&mut arr, 0, arr.len() - 1); | ||
| } | ||
| arr | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| pub trait Swap { | ||
| fn swap(&mut self, i: u32, j: u32); | ||
| } | ||
|
|
||
| impl<T, let N: u32> Swap for [T; N] { | ||
| fn swap(&mut self, i: u32, j: u32) { | ||
| let temp = self[i]; | ||
| self[i] = self[j]; | ||
| self[j] = temp; | ||
| } | ||
| } | ||
michaeljklein marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| unconstrained fn partition<T, let N: u32>( | ||
| arr: &mut [T; N], | ||
| low: u32, | ||
| high: u32, | ||
| sortfn: unconstrained fn(T, T) -> bool, | ||
| ) -> u32 { | ||
| let pivot = high; | ||
| let mut i = low; | ||
| for j in low..high { | ||
| if (sortfn(arr[j], arr[pivot])) { | ||
| arr.swap(i, j); | ||
| i += 1; | ||
| } | ||
| } | ||
| arr.swap(i, pivot); | ||
| i | ||
| } | ||
|
|
||
| unconstrained fn quicksort_recursive<T, let N: u32>( | ||
| arr: &mut [T; N], | ||
| low: u32, | ||
| high: u32, | ||
| sortfn: unconstrained fn(T, T) -> bool, | ||
| ) { | ||
| if low < high { | ||
| let pivot_index = partition(arr, low, high, sortfn); | ||
| if pivot_index > 0 { | ||
| quicksort_recursive(arr, low, pivot_index - 1, sortfn); | ||
| } | ||
| quicksort_recursive(arr, pivot_index + 1, high, sortfn); | ||
| } | ||
| } | ||
|
|
||
| pub unconstrained fn quicksort<T, let N: u32>( | ||
| _arr: [T; N], | ||
| sortfn: unconstrained fn(T, T) -> bool, | ||
| ) -> [T; N] { | ||
| let mut arr: [T; N] = _arr; | ||
| if arr.len() <= 1 {} else { | ||
| quicksort_recursive(&mut arr, 0, arr.len() - 1, sortfn); | ||
| } | ||
| arr | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.