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

insertion sort #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added cadence/.DS_Store
Copy link
Contributor

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

Binary file not shown.
25 changes: 25 additions & 0 deletions cadence/contracts/ArrayUtils.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,29 @@ pub contract ArrayUtils {
return res
}

pub fun insertionSort(_ array: [AnyStruct], _ f: ((AnyStruct, AnyStruct): Int)): [AnyStruct] {

var arr: [AnyStruct] = []
var i = 0
while i < array.length {
arr.append(array[i])
i = i + 1
}
Comment on lines +63 to +66
Copy link
Contributor

@bluesign bluesign Jun 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
while i < array.length {
arr.append(array[i])
i = i + 1
}
arr.appendAll(array)

Copy link
Contributor

@bluesign bluesign Jun 26, 2023

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


let n = arr.length
i = 1
while i < n {
let key = arr[i]
var j = i - 1

while j >= 0 && f(arr[j], key) < 0 {
arr[j + 1] = arr[j]
j = j - 1
}
arr[j + 1] = key
i = i + 1
}

return arr
}
}