Counting sort in SwiftSorted trie implementation in CHow best to differentiate boolean values in a long list?Asc and desc array sort methodsGenerating Phone Words in SwiftParallel integer tree sort algorithm in JavaFinding roughly matching genome sequences in Python dictionaryCompare 2 unordered, rooted trees for shape-isomorphismCalculating the number of matches for a tuple, a pair of nodes and a mappingSorting a 2-dimensional array with counting sortGeneric Dictionary Equality Comparer

What exactly is this small puffer fish doing and how did it manage to accomplish such a feat?

My adviser wants to be the first author

What options are left, if Britain cannot decide?

Gantt Chart like rectangles with log scale

SOQL: Populate a Literal List in WHERE IN Clause

How to write cleanly even if my character uses expletive language?

Min function accepting varying number of arguments in C++17

How to make healing in an exploration game interesting

How to use deus ex machina safely?

A sequence that has integer values for prime indexes only:

Can a druid choose the size of its wild shape beast?

Are there other languages, besides English, where the indefinite (or definite) article varies based on sound?

Are there verbs that are neither telic, or atelic?

It's a yearly task, alright

Is there a data structure that only stores hash codes and not the actual objects?

How difficult is it to simply disable/disengage the MCAS on Boeing 737 Max 8 & 9 Aircraft?

Most cost effective thermostat setting: consistent temperature vs. lowest temperature possible

What's the meaning of “spike” in the context of “adrenaline spike”?

Can I use USB data pins as power source

Could the Saturn V actually have launched astronauts around Venus?

How do I hide Chekhov's Gun?

Why doesn't using two cd commands in bash script execute the second command?

If I can solve Sudoku can I solve Travelling Salesman Problem(TSP)? If yes, how?

Why doesn't the EU now just force the UK to choose between referendum and no-deal?



Counting sort in Swift


Sorted trie implementation in CHow best to differentiate boolean values in a long list?Asc and desc array sort methodsGenerating Phone Words in SwiftParallel integer tree sort algorithm in JavaFinding roughly matching genome sequences in Python dictionaryCompare 2 unordered, rooted trees for shape-isomorphismCalculating the number of matches for a tuple, a pair of nodes and a mappingSorting a 2-dimensional array with counting sortGeneric Dictionary Equality Comparer













0












$begingroup$


I have the below sorting algorithm which takes an array of dictionary values as declared below:



guard var imageUrlString = anyImage.value as? [String:AnyObject] else return 


I then loop through it adding the values with the smallest Int key to a new array to be used afterward. Thus the values in this array of AnyObjects would go from 1 to n.



I was wondering if I could make it more concise.



var values = [AnyObject]()
var keys = [String]()
var Done = false
var j = 1

while !Done
for i in imageUrlString
let key = Int(String(i.key.last!))

if j == key
values.append(i.value)
keys.append(i.key)
print(i, " This is the i for in if ")
if imageUrlString.count == j
print("Done yet: yes", values[0], " ", values[3])
Done = true
break;

j+=1
else
print("No,,.")












share|improve this question









New contributor




Outsider is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$











  • $begingroup$
    If imageUrlString is empty, you'll never be Done; Force-unwrapping i.key.last calls for trouble; The logic that makes sure that values has at least 4 elements isn't clear enough.
    $endgroup$
    – ielyamani
    yesterday










  • $begingroup$
    @ielyamani imageUrlString will always have values its a prerequesit to enter the class with this sort
    $endgroup$
    – Outsider
    yesterday






  • 2




    $begingroup$
    @Outsider Could you edit your question by adding all the necessary information so that it is easily reproducible in a playground?
    $endgroup$
    – ielyamani
    yesterday










  • $begingroup$
    @ielyamani check the edit
    $endgroup$
    – Outsider
    yesterday















0












$begingroup$


I have the below sorting algorithm which takes an array of dictionary values as declared below:



guard var imageUrlString = anyImage.value as? [String:AnyObject] else return 


I then loop through it adding the values with the smallest Int key to a new array to be used afterward. Thus the values in this array of AnyObjects would go from 1 to n.



I was wondering if I could make it more concise.



var values = [AnyObject]()
var keys = [String]()
var Done = false
var j = 1

while !Done
for i in imageUrlString
let key = Int(String(i.key.last!))

if j == key
values.append(i.value)
keys.append(i.key)
print(i, " This is the i for in if ")
if imageUrlString.count == j
print("Done yet: yes", values[0], " ", values[3])
Done = true
break;

j+=1
else
print("No,,.")












share|improve this question









New contributor




Outsider is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$











  • $begingroup$
    If imageUrlString is empty, you'll never be Done; Force-unwrapping i.key.last calls for trouble; The logic that makes sure that values has at least 4 elements isn't clear enough.
    $endgroup$
    – ielyamani
    yesterday










  • $begingroup$
    @ielyamani imageUrlString will always have values its a prerequesit to enter the class with this sort
    $endgroup$
    – Outsider
    yesterday






  • 2




    $begingroup$
    @Outsider Could you edit your question by adding all the necessary information so that it is easily reproducible in a playground?
    $endgroup$
    – ielyamani
    yesterday










  • $begingroup$
    @ielyamani check the edit
    $endgroup$
    – Outsider
    yesterday













0












0








0


1



$begingroup$


I have the below sorting algorithm which takes an array of dictionary values as declared below:



guard var imageUrlString = anyImage.value as? [String:AnyObject] else return 


I then loop through it adding the values with the smallest Int key to a new array to be used afterward. Thus the values in this array of AnyObjects would go from 1 to n.



I was wondering if I could make it more concise.



var values = [AnyObject]()
var keys = [String]()
var Done = false
var j = 1

while !Done
for i in imageUrlString
let key = Int(String(i.key.last!))

if j == key
values.append(i.value)
keys.append(i.key)
print(i, " This is the i for in if ")
if imageUrlString.count == j
print("Done yet: yes", values[0], " ", values[3])
Done = true
break;

j+=1
else
print("No,,.")












share|improve this question









New contributor




Outsider is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




I have the below sorting algorithm which takes an array of dictionary values as declared below:



guard var imageUrlString = anyImage.value as? [String:AnyObject] else return 


I then loop through it adding the values with the smallest Int key to a new array to be used afterward. Thus the values in this array of AnyObjects would go from 1 to n.



I was wondering if I could make it more concise.



var values = [AnyObject]()
var keys = [String]()
var Done = false
var j = 1

while !Done
for i in imageUrlString
let key = Int(String(i.key.last!))

if j == key
values.append(i.value)
keys.append(i.key)
print(i, " This is the i for in if ")
if imageUrlString.count == j
print("Done yet: yes", values[0], " ", values[3])
Done = true
break;

j+=1
else
print("No,,.")









sorting swift ios dictionary






share|improve this question









New contributor




Outsider is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Outsider is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 6 hours ago







Outsider













New contributor




Outsider is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









OutsiderOutsider

32




32




New contributor




Outsider is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Outsider is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Outsider is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • $begingroup$
    If imageUrlString is empty, you'll never be Done; Force-unwrapping i.key.last calls for trouble; The logic that makes sure that values has at least 4 elements isn't clear enough.
    $endgroup$
    – ielyamani
    yesterday










  • $begingroup$
    @ielyamani imageUrlString will always have values its a prerequesit to enter the class with this sort
    $endgroup$
    – Outsider
    yesterday






  • 2




    $begingroup$
    @Outsider Could you edit your question by adding all the necessary information so that it is easily reproducible in a playground?
    $endgroup$
    – ielyamani
    yesterday










  • $begingroup$
    @ielyamani check the edit
    $endgroup$
    – Outsider
    yesterday
















  • $begingroup$
    If imageUrlString is empty, you'll never be Done; Force-unwrapping i.key.last calls for trouble; The logic that makes sure that values has at least 4 elements isn't clear enough.
    $endgroup$
    – ielyamani
    yesterday










  • $begingroup$
    @ielyamani imageUrlString will always have values its a prerequesit to enter the class with this sort
    $endgroup$
    – Outsider
    yesterday






  • 2




    $begingroup$
    @Outsider Could you edit your question by adding all the necessary information so that it is easily reproducible in a playground?
    $endgroup$
    – ielyamani
    yesterday










  • $begingroup$
    @ielyamani check the edit
    $endgroup$
    – Outsider
    yesterday















$begingroup$
If imageUrlString is empty, you'll never be Done; Force-unwrapping i.key.last calls for trouble; The logic that makes sure that values has at least 4 elements isn't clear enough.
$endgroup$
– ielyamani
yesterday




$begingroup$
If imageUrlString is empty, you'll never be Done; Force-unwrapping i.key.last calls for trouble; The logic that makes sure that values has at least 4 elements isn't clear enough.
$endgroup$
– ielyamani
yesterday












$begingroup$
@ielyamani imageUrlString will always have values its a prerequesit to enter the class with this sort
$endgroup$
– Outsider
yesterday




$begingroup$
@ielyamani imageUrlString will always have values its a prerequesit to enter the class with this sort
$endgroup$
– Outsider
yesterday




2




2




$begingroup$
@Outsider Could you edit your question by adding all the necessary information so that it is easily reproducible in a playground?
$endgroup$
– ielyamani
yesterday




$begingroup$
@Outsider Could you edit your question by adding all the necessary information so that it is easily reproducible in a playground?
$endgroup$
– ielyamani
yesterday












$begingroup$
@ielyamani check the edit
$endgroup$
– Outsider
yesterday




$begingroup$
@ielyamani check the edit
$endgroup$
– Outsider
yesterday










2 Answers
2






active

oldest

votes


















0












$begingroup$

Making it to the promised land of O(n)



To reproduce your code in a playground, a Media struct could be defined this way:



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




Let's suppose the value of imageUrlString is this:



let imageUrlString: [String: Media] =
["media1": Media(mediaUrl: "URL", timeStamp: "573889179.6991431", postTimeStamp: "573889189.73954"),
"media4": Media(mediaUrl: "URL", timeStamp: "573889185.750419"),
"media2": Media(mediaUrl: "URL", timeStamp: "573889181.49576"),
"media3": Media(mediaUrl: "URL", timeStamp: "573889183.89598")]

var values = [Media]()


Your code works by relying on the chance of having the last character read from the imageUrlString dictionary, equal the order of the element you want to append to the values array.



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




Robustness



The code in question relies on external facts that are not checked in code. For example:



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


Breaking an outer loop



Instead of mutating the variable Done (which shouldn't be uppercased since it's an instance, not a class/type/struct/enum/etc), you can break from a nested loop this way:



OuterLoop: while true 
for i in imageUrlString
...
if imageUrlString.count == j
break OuterLoop

...





Straight forward sorting



In Swift 5, Timsort is the algorithm that is going to be used by the standard library while sorting. It has better time complexity than Introsort and is more versatile and less memory greedy than O(n) sorting algorithms.



So, why not just use it to sort the entries in imageUrlString by timeStamp or a some other more reliable criteria?



let values = imageUrlString.values
.sorted(by: $0.timeStamp < $1.timeStamp )


(If you're sure that timeStamps represent real numbers, you could cast them to Double before comparing them)






share|improve this answer











$endgroup$








  • 1




    $begingroup$
    Appreciate the in-depth answer! :]
    $endgroup$
    – Outsider
    yesterday


















0












$begingroup$

Consider:



let dictionary = ["3": "c", "1": "a", "2": "b”]


If you want to get separate arrays of keys and values, sorted by the order of the Int values of the keys, you can do:



let (keys, values) = dictionary.map (Int($0.key) ?? 0, $0.value) // get `Int` keys
.sorted $0.0 < $1.0 // now sort by those `Int` values
.reduce(into: ([Int](), [String]())) arrays, entry in // divide that up into two arrays
arrays.0.append(entry.0)
arrays.1.append(entry.1)



That results in a keys of [1, 2, 3] and values of ["a", "b", "c"].



But a key observation is that you actually have to explicitly sort your results. Dictionaries, unlike arrays, are unordered. E.g. regardless of what order the keys appeared in a dictionary literal or JSON, when you iterate through the key-value pairs in a dictionary, the order may be different. You have to convert to an array and sort it yourself if order is important.




The above arbitrarily uses 0 for any keys (if any) that couldn’t be converted to an Int. If you just wanted to drop those entries, you could alternatively do:



let (keys, values) = dictionary.compactMap entry in Int(entry.key).map ($0, entry.value) 
.sorted $0.0 < $1.0 // now sort by those `Int` values
.reduce(into: ([Int](), [String]())) arrays, entry in // divide that up into two arrays
arrays.0.append(entry.0)
arrays.1.append(entry.1)






share|improve this answer









$endgroup$












    Your Answer





    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    Outsider is a new contributor. Be nice, and check out our Code of Conduct.









    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215392%2fcounting-sort-in-swift%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0












    $begingroup$

    Making it to the promised land of O(n)



    To reproduce your code in a playground, a Media struct could be defined this way:



    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




    Let's suppose the value of imageUrlString is this:



    let imageUrlString: [String: Media] =
    ["media1": Media(mediaUrl: "URL", timeStamp: "573889179.6991431", postTimeStamp: "573889189.73954"),
    "media4": Media(mediaUrl: "URL", timeStamp: "573889185.750419"),
    "media2": Media(mediaUrl: "URL", timeStamp: "573889181.49576"),
    "media3": Media(mediaUrl: "URL", timeStamp: "573889183.89598")]

    var values = [Media]()


    Your code works by relying on the chance of having the last character read from the imageUrlString dictionary, equal the order of the element you want to append to the values array.



    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




    Robustness



    The code in question relies on external facts that are not checked in code. For example:



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


    Breaking an outer loop



    Instead of mutating the variable Done (which shouldn't be uppercased since it's an instance, not a class/type/struct/enum/etc), you can break from a nested loop this way:



    OuterLoop: while true 
    for i in imageUrlString
    ...
    if imageUrlString.count == j
    break OuterLoop

    ...





    Straight forward sorting



    In Swift 5, Timsort is the algorithm that is going to be used by the standard library while sorting. It has better time complexity than Introsort and is more versatile and less memory greedy than O(n) sorting algorithms.



    So, why not just use it to sort the entries in imageUrlString by timeStamp or a some other more reliable criteria?



    let values = imageUrlString.values
    .sorted(by: $0.timeStamp < $1.timeStamp )


    (If you're sure that timeStamps represent real numbers, you could cast them to Double before comparing them)






    share|improve this answer











    $endgroup$








    • 1




      $begingroup$
      Appreciate the in-depth answer! :]
      $endgroup$
      – Outsider
      yesterday















    0












    $begingroup$

    Making it to the promised land of O(n)



    To reproduce your code in a playground, a Media struct could be defined this way:



    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




    Let's suppose the value of imageUrlString is this:



    let imageUrlString: [String: Media] =
    ["media1": Media(mediaUrl: "URL", timeStamp: "573889179.6991431", postTimeStamp: "573889189.73954"),
    "media4": Media(mediaUrl: "URL", timeStamp: "573889185.750419"),
    "media2": Media(mediaUrl: "URL", timeStamp: "573889181.49576"),
    "media3": Media(mediaUrl: "URL", timeStamp: "573889183.89598")]

    var values = [Media]()


    Your code works by relying on the chance of having the last character read from the imageUrlString dictionary, equal the order of the element you want to append to the values array.



    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




    Robustness



    The code in question relies on external facts that are not checked in code. For example:



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


    Breaking an outer loop



    Instead of mutating the variable Done (which shouldn't be uppercased since it's an instance, not a class/type/struct/enum/etc), you can break from a nested loop this way:



    OuterLoop: while true 
    for i in imageUrlString
    ...
    if imageUrlString.count == j
    break OuterLoop

    ...





    Straight forward sorting



    In Swift 5, Timsort is the algorithm that is going to be used by the standard library while sorting. It has better time complexity than Introsort and is more versatile and less memory greedy than O(n) sorting algorithms.



    So, why not just use it to sort the entries in imageUrlString by timeStamp or a some other more reliable criteria?



    let values = imageUrlString.values
    .sorted(by: $0.timeStamp < $1.timeStamp )


    (If you're sure that timeStamps represent real numbers, you could cast them to Double before comparing them)






    share|improve this answer











    $endgroup$








    • 1




      $begingroup$
      Appreciate the in-depth answer! :]
      $endgroup$
      – Outsider
      yesterday













    0












    0








    0





    $begingroup$

    Making it to the promised land of O(n)



    To reproduce your code in a playground, a Media struct could be defined this way:



    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




    Let's suppose the value of imageUrlString is this:



    let imageUrlString: [String: Media] =
    ["media1": Media(mediaUrl: "URL", timeStamp: "573889179.6991431", postTimeStamp: "573889189.73954"),
    "media4": Media(mediaUrl: "URL", timeStamp: "573889185.750419"),
    "media2": Media(mediaUrl: "URL", timeStamp: "573889181.49576"),
    "media3": Media(mediaUrl: "URL", timeStamp: "573889183.89598")]

    var values = [Media]()


    Your code works by relying on the chance of having the last character read from the imageUrlString dictionary, equal the order of the element you want to append to the values array.



    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




    Robustness



    The code in question relies on external facts that are not checked in code. For example:



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


    Breaking an outer loop



    Instead of mutating the variable Done (which shouldn't be uppercased since it's an instance, not a class/type/struct/enum/etc), you can break from a nested loop this way:



    OuterLoop: while true 
    for i in imageUrlString
    ...
    if imageUrlString.count == j
    break OuterLoop

    ...





    Straight forward sorting



    In Swift 5, Timsort is the algorithm that is going to be used by the standard library while sorting. It has better time complexity than Introsort and is more versatile and less memory greedy than O(n) sorting algorithms.



    So, why not just use it to sort the entries in imageUrlString by timeStamp or a some other more reliable criteria?



    let values = imageUrlString.values
    .sorted(by: $0.timeStamp < $1.timeStamp )


    (If you're sure that timeStamps represent real numbers, you could cast them to Double before comparing them)






    share|improve this answer











    $endgroup$



    Making it to the promised land of O(n)



    To reproduce your code in a playground, a Media struct could be defined this way:



    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




    Let's suppose the value of imageUrlString is this:



    let imageUrlString: [String: Media] =
    ["media1": Media(mediaUrl: "URL", timeStamp: "573889179.6991431", postTimeStamp: "573889189.73954"),
    "media4": Media(mediaUrl: "URL", timeStamp: "573889185.750419"),
    "media2": Media(mediaUrl: "URL", timeStamp: "573889181.49576"),
    "media3": Media(mediaUrl: "URL", timeStamp: "573889183.89598")]

    var values = [Media]()


    Your code works by relying on the chance of having the last character read from the imageUrlString dictionary, equal the order of the element you want to append to the values array.



    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




    Robustness



    The code in question relies on external facts that are not checked in code. For example:



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


    Breaking an outer loop



    Instead of mutating the variable Done (which shouldn't be uppercased since it's an instance, not a class/type/struct/enum/etc), you can break from a nested loop this way:



    OuterLoop: while true 
    for i in imageUrlString
    ...
    if imageUrlString.count == j
    break OuterLoop

    ...





    Straight forward sorting



    In Swift 5, Timsort is the algorithm that is going to be used by the standard library while sorting. It has better time complexity than Introsort and is more versatile and less memory greedy than O(n) sorting algorithms.



    So, why not just use it to sort the entries in imageUrlString by timeStamp or a some other more reliable criteria?



    let values = imageUrlString.values
    .sorted(by: $0.timeStamp < $1.timeStamp )


    (If you're sure that timeStamps represent real numbers, you could cast them to Double before comparing them)







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited yesterday

























    answered yesterday









    ielyamaniielyamani

    353113




    353113







    • 1




      $begingroup$
      Appreciate the in-depth answer! :]
      $endgroup$
      – Outsider
      yesterday












    • 1




      $begingroup$
      Appreciate the in-depth answer! :]
      $endgroup$
      – Outsider
      yesterday







    1




    1




    $begingroup$
    Appreciate the in-depth answer! :]
    $endgroup$
    – Outsider
    yesterday




    $begingroup$
    Appreciate the in-depth answer! :]
    $endgroup$
    – Outsider
    yesterday













    0












    $begingroup$

    Consider:



    let dictionary = ["3": "c", "1": "a", "2": "b”]


    If you want to get separate arrays of keys and values, sorted by the order of the Int values of the keys, you can do:



    let (keys, values) = dictionary.map (Int($0.key) ?? 0, $0.value) // get `Int` keys
    .sorted $0.0 < $1.0 // now sort by those `Int` values
    .reduce(into: ([Int](), [String]())) arrays, entry in // divide that up into two arrays
    arrays.0.append(entry.0)
    arrays.1.append(entry.1)



    That results in a keys of [1, 2, 3] and values of ["a", "b", "c"].



    But a key observation is that you actually have to explicitly sort your results. Dictionaries, unlike arrays, are unordered. E.g. regardless of what order the keys appeared in a dictionary literal or JSON, when you iterate through the key-value pairs in a dictionary, the order may be different. You have to convert to an array and sort it yourself if order is important.




    The above arbitrarily uses 0 for any keys (if any) that couldn’t be converted to an Int. If you just wanted to drop those entries, you could alternatively do:



    let (keys, values) = dictionary.compactMap entry in Int(entry.key).map ($0, entry.value) 
    .sorted $0.0 < $1.0 // now sort by those `Int` values
    .reduce(into: ([Int](), [String]())) arrays, entry in // divide that up into two arrays
    arrays.0.append(entry.0)
    arrays.1.append(entry.1)






    share|improve this answer









    $endgroup$

















      0












      $begingroup$

      Consider:



      let dictionary = ["3": "c", "1": "a", "2": "b”]


      If you want to get separate arrays of keys and values, sorted by the order of the Int values of the keys, you can do:



      let (keys, values) = dictionary.map (Int($0.key) ?? 0, $0.value) // get `Int` keys
      .sorted $0.0 < $1.0 // now sort by those `Int` values
      .reduce(into: ([Int](), [String]())) arrays, entry in // divide that up into two arrays
      arrays.0.append(entry.0)
      arrays.1.append(entry.1)



      That results in a keys of [1, 2, 3] and values of ["a", "b", "c"].



      But a key observation is that you actually have to explicitly sort your results. Dictionaries, unlike arrays, are unordered. E.g. regardless of what order the keys appeared in a dictionary literal or JSON, when you iterate through the key-value pairs in a dictionary, the order may be different. You have to convert to an array and sort it yourself if order is important.




      The above arbitrarily uses 0 for any keys (if any) that couldn’t be converted to an Int. If you just wanted to drop those entries, you could alternatively do:



      let (keys, values) = dictionary.compactMap entry in Int(entry.key).map ($0, entry.value) 
      .sorted $0.0 < $1.0 // now sort by those `Int` values
      .reduce(into: ([Int](), [String]())) arrays, entry in // divide that up into two arrays
      arrays.0.append(entry.0)
      arrays.1.append(entry.1)






      share|improve this answer









      $endgroup$















        0












        0








        0





        $begingroup$

        Consider:



        let dictionary = ["3": "c", "1": "a", "2": "b”]


        If you want to get separate arrays of keys and values, sorted by the order of the Int values of the keys, you can do:



        let (keys, values) = dictionary.map (Int($0.key) ?? 0, $0.value) // get `Int` keys
        .sorted $0.0 < $1.0 // now sort by those `Int` values
        .reduce(into: ([Int](), [String]())) arrays, entry in // divide that up into two arrays
        arrays.0.append(entry.0)
        arrays.1.append(entry.1)



        That results in a keys of [1, 2, 3] and values of ["a", "b", "c"].



        But a key observation is that you actually have to explicitly sort your results. Dictionaries, unlike arrays, are unordered. E.g. regardless of what order the keys appeared in a dictionary literal or JSON, when you iterate through the key-value pairs in a dictionary, the order may be different. You have to convert to an array and sort it yourself if order is important.




        The above arbitrarily uses 0 for any keys (if any) that couldn’t be converted to an Int. If you just wanted to drop those entries, you could alternatively do:



        let (keys, values) = dictionary.compactMap entry in Int(entry.key).map ($0, entry.value) 
        .sorted $0.0 < $1.0 // now sort by those `Int` values
        .reduce(into: ([Int](), [String]())) arrays, entry in // divide that up into two arrays
        arrays.0.append(entry.0)
        arrays.1.append(entry.1)






        share|improve this answer









        $endgroup$



        Consider:



        let dictionary = ["3": "c", "1": "a", "2": "b”]


        If you want to get separate arrays of keys and values, sorted by the order of the Int values of the keys, you can do:



        let (keys, values) = dictionary.map (Int($0.key) ?? 0, $0.value) // get `Int` keys
        .sorted $0.0 < $1.0 // now sort by those `Int` values
        .reduce(into: ([Int](), [String]())) arrays, entry in // divide that up into two arrays
        arrays.0.append(entry.0)
        arrays.1.append(entry.1)



        That results in a keys of [1, 2, 3] and values of ["a", "b", "c"].



        But a key observation is that you actually have to explicitly sort your results. Dictionaries, unlike arrays, are unordered. E.g. regardless of what order the keys appeared in a dictionary literal or JSON, when you iterate through the key-value pairs in a dictionary, the order may be different. You have to convert to an array and sort it yourself if order is important.




        The above arbitrarily uses 0 for any keys (if any) that couldn’t be converted to an Int. If you just wanted to drop those entries, you could alternatively do:



        let (keys, values) = dictionary.compactMap entry in Int(entry.key).map ($0, entry.value) 
        .sorted $0.0 < $1.0 // now sort by those `Int` values
        .reduce(into: ([Int](), [String]())) arrays, entry in // divide that up into two arrays
        arrays.0.append(entry.0)
        arrays.1.append(entry.1)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered yesterday









        RobRob

        38339




        38339




















            Outsider is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded


















            Outsider is a new contributor. Be nice, and check out our Code of Conduct.












            Outsider is a new contributor. Be nice, and check out our Code of Conduct.











            Outsider is a new contributor. Be nice, and check out our Code of Conduct.














            Thanks for contributing an answer to Code Review Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215392%2fcounting-sort-in-swift%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

            Prove that NP is closed under karp reduction?Space(n) not closed under Karp reductions - what about NTime(n)?Class P is closed under rotation?Prove or disprove that $NL$ is closed under polynomial many-one reductions$mathbfNC_2$ is closed under log-space reductionOn Karp reductionwhen can I know if a class (complexity) is closed under reduction (cook/karp)Check if class $PSPACE$ is closed under polyonomially space reductionIs NPSPACE also closed under polynomial-time reduction and under log-space reduction?Prove PSPACE is closed under complement?Prove PSPACE is closed under union?

            Is my guitar’s action too high? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)Strings too stiff on a recently purchased acoustic guitar | Cort AD880CEIs the action of my guitar really high?Μy little finger is too weak to play guitarWith guitar, how long should I give my fingers to strengthen / callous?When playing a fret the guitar sounds mutedPlaying (Barre) chords up the guitar neckI think my guitar strings are wound too tight and I can't play barre chordsF barre chord on an SG guitarHow to find to the right strings of a barre chord by feel?High action on higher fret on my steel acoustic guitar