快速排序算法实现
发布时间:2020-12-14 05:22:05 所属栏目:百科 来源:网络整理
导读:不幸的是,我没有在互联网上找到任何东西,但我确定可以找到 – 我想知道 Swift的排序算法是如何实现的.它是使用mergesort还是quicksort或其他的东西?感谢链接或答案:) 更新:Swift现在是开源的,而且在 https://github.com/apple/swift/blob/master/stdlib/pu
不幸的是,我没有在互联网上找到任何东西,但我确定可以找到 – 我想知道
Swift的排序算法是如何实现的.它是使用mergesort还是quicksort或其他的东西?感谢链接或答案:)
更新:Swift现在是开源的,而且在
> https://github.com/apple/swift/blob/master/stdlib/public/core/CollectionAlgorithms.swift.gyb 可以清楚地看到,使用introsort进行排序 旧答案:定义可比较的结构并在断点中设置< ;: struct MyStruct : Comparable { let val : Int } func ==(x: MyStruct,y: MyStruct) -> Bool { println("(x.val) == (y.val)") return x.val == y.val } func <(x: MyStruct,y: MyStruct) -> Bool { println("(x.val) < (y.val)") return x.val < y.val // <--- SET BREAKPOINT HERE } var array = [MyStruct]() for _ in 1 ... 30 { array.append(MyStruct(val: Int(arc4random_uniform(1000)))) } sort(&array) 显示以下堆栈回溯: (lldb) bt * thread #1: tid = 0x5a00,0x00000001001cb806 sort`sort. Swift.Bool + 454 at main.swift:22,queue = 'com.apple.main-thread',stop reason = breakpoint 1.1 * frame #0: 0x00000001001cb806 sort`sort. Swift.Bool + 454 at main.swift:22 frame #1: 0x00000001001cb62b sort`protocol witness for Swift._Comparable.(Swift._Comparable.Self.Type)(Swift._Comparable.Self,Swift._Comparable.Self) -> Swift.Bool in conformance sort.MyStruct : Swift._Comparable + 27 at main.swift:20 frame #2: 0x00000001000f5a98 sort`Swift._partition (inout A,Swift.Range) -> A.Index + 3224 frame #3: 0x00000001000f756a sort`Swift._introSortImpl (inout A,Swift.Range,Swift.Int) -> () + 2138 frame #4: 0x00000001000f6c01 sort`Swift._introSort (inout A,Swift.Range) -> () + 1233 frame #5: 0x00000001000fc47f sort`Swift.sort (inout A) -> () + 607 frame #6: 0x000000010013ea77 sort`partial apply forwarder for Swift.(sort (inout Swift.Array) -> ()).(closure #1) + 183 frame #7: 0x000000010013eaf8 sort`partial apply forwarder for reabstraction thunk helper from @callee_owned (@inout Swift.UnsafeMutableBufferPointer) -> (@unowned ()) to @callee_owned (@inout Swift.UnsafeMutableBufferPointer) -> (@out ()) + 56 frame #8: 0x0000000100046c4b sort`Swift.Array.withUnsafeMutableBufferPointer (inout Swift.Array)((inout Swift.UnsafeMutableBufferPointer) -> B) -> B + 475 frame #9: 0x00000001000fc5ad sort`Swift.sort (inout Swift.Array) -> () + 157 frame #10: 0x00000001001cb465 sort`top_level_code + 1237 at main.swift:29 frame #11: 0x00000001001cbdca sort`main + 42 at main.swift:0 frame #12: 0x00007fff8aa9a5fd libdyld.dylib`start + 1 然后 (lldb) bt * thread #1: tid = 0x5a00,Swift._Comparable.Self) -> Swift.Bool in conformance sort.MyStruct : Swift._Comparable + 27 at main.swift:20 frame #2: 0x00000001000f449e sort`Swift._insertionSort (inout A,Swift.Range) -> () + 2958 frame #3: 0x00000001000f730e sort`Swift._introSortImpl (inout A,Swift.Int) -> () + 1534 frame #4: 0x00000001000f797d sort`Swift._introSortImpl (inout A,Swift.Int) -> () + 3181 frame #5: 0x00000001000f6c01 sort`Swift._introSort (inout A,Swift.Range) -> () + 1233 frame #6: 0x00000001000fc47f sort`Swift.sort (inout A) -> () + 607 frame #7: 0x000000010013ea77 sort`partial apply forwarder for Swift.(sort (inout Swift.Array) -> ()).(closure #1) + 183 frame #8: 0x000000010013eaf8 sort`partial apply forwarder for reabstraction thunk helper from @callee_owned (@inout Swift.UnsafeMutableBufferPointer) -> (@unowned ()) to @callee_owned (@inout Swift.UnsafeMutableBufferPointer) -> (@out ()) + 56 frame #9: 0x0000000100046c4b sort`Swift.Array.withUnsafeMutableBufferPointer (inout Swift.Array)((inout Swift.UnsafeMutableBufferPointer) -> B) -> B + 475 frame #10: 0x00000001000fc5ad sort`Swift.sort (inout Swift.Array) -> () + 157 frame #11: 0x00000001001cb465 sort`top_level_code + 1237 at main.swift:29 frame #12: 0x00000001001cbdca sort`main + 42 at main.swift:0 frame #13: 0x00007fff8aa9a5fd libdyld.dylib`start + 1 这证实了Airspeed’s answer的猜想,即使用内毒素 如果数组具有少于20个元素,那么只有插入排序似乎被使用. 当然,实施可能会在将来发生变化. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |