-
Notifications
You must be signed in to change notification settings - Fork 13
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
insertion sort #20
base: main
Are you sure you want to change the base?
insertion sort #20
Conversation
Forgot to assign folks here, thanks for submitting this! I'll take a look this week 🙏 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's either delete this file or add .DS_Store
to .gitignore
while i < array.length { | ||
arr.append(array[i]) | ||
i = i + 1 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
while i < array.length { | |
arr.append(array[i]) | |
i = i + 1 | |
} | |
arr.appendAll(array) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually I guess: var arr = array
also works
thanks a lot, I made a small comment. this can be also alternative ( as we are adding items to another array ) : pub fun insertionSort(_ array: [AnyStruct], _ f: ((AnyStruct, AnyStruct): Int)): [AnyStruct] {
var arr: [AnyStruct] = []
for element in array{
arr.append(element)
var j = arr.length - 2
while j >= 0 && f(arr[j + 1], arr[j]) < 0 {
arr[j + 1] <-> arr[j]
j = j - 1
}
}
return arr
}
|
status here? |
let values = [2, 1, 5, 6, 1, 7, 0]
insertionSort(values, fun(a: AnyStruct, b: AnyStruct): Int {
return (b as! Int) - (a as! Int)
})