Making it to the promised land of O(n)
struct Media {
let mediaUrl: String
let postTimeStamp: String?
let timeStamp: String //A double would be more appropriate
init(mediaUrl: String, timeStamp: String, postTimeStamp: String? = nil) {
self.mediaUrl = mediaUrl
self.timeStamp = timeStamp
self.postTimeStamp = postTimeStamp
}
}
Bear in mind that a dictionary is an unordered collection. And unless mutated, the order of elements stays the same. The worst case would be when, reading elements from the dictionary, yields elements in a reversed order"order". In this case, you'll have to read from the dictionary n*(n+1)/2 times, in order to build your values array. In other termsIn other terms, this algorithm is has O(n²) time complexity (worst case), O(n) best case, and is not the proper Counting Sort Algorithm which is O(n).
Here is an attempt to make this O(n):
let tempo = Media(mediaUrl: "", timeStamp: "")
var values = Array(repeating: tempo, count: imageUrlString.count)
var keys = Array(repeating: "", count: imageUrlString.count)
for entry in imageUrlString {
let index = Int(String(entry.key.last!))! - 1 //Force-unwrapping for brevity
(keys[index], values[index]) = entry
}