Skip to main content
deleted 12 characters in body
Source Link
ielyamani
  • 889
  • 1
  • 5
  • 18
  • If imageUrlString is empty, Done will never be mutated and thus the outer loop will be infinite;
  • The order of the elements in the result array relies on the last character in a string;
  • The last character in all the keys has to exist and be numerical for j to be incremented. Otherwise, you're in for another infinite looploop;
  • Breaking the outer loop relies on the digits at the end of the keys go from 1 to at least imageUrlString.count.
  • If imageUrlString is empty, Done will never be mutated and thus the outer loop will be infinite;
  • The order of the elements in the result array relies on the last character in a string;
  • The last character in all the keys has to exist and be numerical for j to be incremented. Otherwise, you're in for another infinite loop
  • Breaking the outer loop relies on the digits at the end of the keys go from 1 to at least imageUrlString.count.
  • If imageUrlString is empty, Done will never be mutated and thus the outer loop will be infinite;
  • The order of the elements in the result array relies on the last character in a string;
  • The last character in all the keys has to exist and be numerical for j to be incremented. Otherwise, you're in for another infinite loop;
  • Breaking the outer loop relies on the digits at the end of the keys go from 1 to at least imageUrlString.count.
deleted 12 characters in body
Source Link
ielyamani
  • 889
  • 1
  • 5
  • 18

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
}
struct Media {
    let mediaUrl: String
    let postTimeStamp: String?
    let timeStamp: String
    
    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. 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 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).

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". 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 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
}
deleted 12 characters in body
Source Link
ielyamani
  • 889
  • 1
  • 5
  • 18

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. 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 terms, this algorithm is has O(n²) time complexity (worst case), O(n) best case, and is not to be confused with the proper Counting Sort Algorithm thatwhich is O(n).

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. 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 terms, this algorithm is has O(n²) time complexity (worst case), O(n) best case, and is not to be confused with the Counting Sort Algorithm that is O(n).

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. 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 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).

Source Link
ielyamani
  • 889
  • 1
  • 5
  • 18
Loading