英文:
How to get insertion sort to work with dates?
问题
对于一个关于算法的作业,我正在尝试使用插入排序对一个节点内存地址的数组进行排序。它似乎部分工作了。
type bookingInfoNode struct {
car string
date string
bookingTime int
userName string
pickUp string
dropOff string
contactInfo int
remarks string
bookingId string
prev *bookingInfoNode
next *bookingInfoNode
}
func sortBookingsByDate(arr []*bookingInfoNode, n int) []*bookingInfoNode {
for i := 1; i < n; i++ {
data := arr[i]
last := i
dataDate, _:= time.Parse("02/01/2006", data.date)
lastDate, _:= time.Parse("02/01/2006", arr[last-1].date)
for (last>0) && (lastDate.After(dataDate)) {
arr[last] = arr[last-1]
last--
}
arr[last] = data
}
return arr
}
英文:
For an assignment on algorithms, I'm trying to sort an array of node memory addresses using insertion sort. It seems to partially work?
type bookingInfoNode struct {
car string
date string
bookingTime int
userName string
pickUp string
dropOff string
contactInfo int
remarks string
bookingId string
prev *bookingInfoNode
next *bookingInfoNode
}
func sortBookingsByDate(arr []*bookingInfoNode, n int) []*bookingInfoNode {
for i := 1; i < n; i++ {
data := arr[i]
last := i
dataDate, _:= time.Parse("02/01/2006", data.date)
lastDate, _:= time.Parse("02/01/2006", arr[last-1].date)
for (last>0) && (lastDate.After(dataDate)) {
arr[last] = arr[last-1]
last--
}
arr[last] = data
}
return arr
}
答案1
得分: 1
在大多数情况下,无论是哪种语言,除非你有一个特定的用例,需要一个特定的实现来提高性能(时间/空间方面),否则你不需要自己编写排序算法。
Go标准库中包含一个sort.Slice
函数,可以帮助你对任意切片进行排序。它在内部实现了快速排序。
你的代码片段可以重写如下:
func sortBookingsByDate(arr []*bookingInfoNode) []*bookingInfoNode {
sort.Slice(arr, func(i, j int) bool {
a, _ := time.Parse("02/01/2006", arr[i].date)
b, _ := time.Parse("02/01/2006", arr[j].date)
return a.Before(b)
}
return arr
}
为了节省CPU周期,你还可以将time.Parse
的输出缓存到一个map[*bookingInfoNode]time.Time
映射中,这样你只需要解析每个日期字符串一次。你也可以在排序之前预先解析它们。
英文:
In most cases, and in most languages, you don't have to code sorting algorithms yourself unless you have a use case where a specific implementation might bring you better performances (time/space-wise)
The Go standard library contains a sort.Slice
function to help you sort arbitrary slices. It implements quicksort under the hood.
Your code snippet could then be rewritten as such:
func sortBookingsByDate(arr []*bookingInfoNode) []*bookingInfoNode {
sort.Slice(arr, func(i, j int) bool {
a, _ := time.Parse("02/01/2006", arr[i].date)
b, _ := time.Parse("02/01/2006", arr[j].date)
return a.Before(b)
}
return arr
}
To save CPU cycles, you could also cache the time.Parse
outputs into a map[*bookingInfoNode]time.Time
map. So you only ever parse each date string once. You could also pre-parse it ahead of the sort.
答案2
得分: 0
经过一些调试,我意识到我的lastDate变量在内部for循环的每次迭代中没有更新。所以我将它放在了内部for循环的条件语句中,并创建了一个包装函数来解决time.Parse的错误。
func getTimeFromParse(d time.Time, _ error) time.Time {
return d
}
func sortBookingsByDate(arr []*bookingInfoNode, n int) []*bookingInfoNode {
for i := 1; i < n; i++ {
data := arr[i]
last := i
dataDate, _ := time.Parse(timeFormat, data.date)
for (last > 0) && (getTimeFromParse(time.Parse(timeFormat, arr[last-1].date)).After(dataDate)) {
arr[last] = arr[last-1]
last--
}
arr[last] = data
}
return arr
}
英文:
After some debugging, I realized that my lastDate variable was not updating with every iteration of the inner for loop. So I placed it as one of my conditionals in my inner for loop and created a wrapper function to get rid of the error from time.Parse
func getTimeFromParse(d time.Time, _ error) time.Time {
return d
}
func sortBookingsByDate(arr []*bookingInfoNode, n int) []*bookingInfoNode {
for i := 1; i < n; i++ {
data := arr[i]
last := i
dataDate, _ := time.Parse(timeFormat, data.date)
for (last > 0) && (getTimeFromParse(time.Parse(timeFormat, arr[last-1].date)).After(dataDate)) {
arr[last] = arr[last-1]
last--
}
arr[last] = data
}
return arr
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论