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
$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,,.")
sorting swift ios dictionary
New contributor
$endgroup$
add a comment |
$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,,.")
sorting swift ios dictionary
New contributor
$endgroup$
$begingroup$
IfimageUrlString
is empty, you'll never beDone
; Force-unwrappingi.key.last
calls for trouble; The logic that makes sure thatvalues
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
add a comment |
$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,,.")
sorting swift ios dictionary
New contributor
$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
sorting swift ios dictionary
New contributor
New contributor
edited 6 hours ago
Outsider
New contributor
asked yesterday
OutsiderOutsider
32
32
New contributor
New contributor
$begingroup$
IfimageUrlString
is empty, you'll never beDone
; Force-unwrappingi.key.last
calls for trouble; The logic that makes sure thatvalues
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
add a comment |
$begingroup$
IfimageUrlString
is empty, you'll never beDone
; Force-unwrappingi.key.last
calls for trouble; The logic that makes sure thatvalues
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
add a comment |
2 Answers
2
active
oldest
votes
$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 timeStamp
s represent real numbers, you could cast them to Double before comparing them)
$endgroup$
1
$begingroup$
Appreciate the in-depth answer! :]
$endgroup$
– Outsider
yesterday
add a comment |
$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)
$endgroup$
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
$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 timeStamp
s represent real numbers, you could cast them to Double before comparing them)
$endgroup$
1
$begingroup$
Appreciate the in-depth answer! :]
$endgroup$
– Outsider
yesterday
add a comment |
$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 timeStamp
s represent real numbers, you could cast them to Double before comparing them)
$endgroup$
1
$begingroup$
Appreciate the in-depth answer! :]
$endgroup$
– Outsider
yesterday
add a comment |
$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 timeStamp
s represent real numbers, you could cast them to Double before comparing them)
$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 timeStamp
s represent real numbers, you could cast them to Double before comparing them)
edited yesterday
answered yesterday
ielyamaniielyamani
353113
353113
1
$begingroup$
Appreciate the in-depth answer! :]
$endgroup$
– Outsider
yesterday
add a comment |
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
add a comment |
$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)
$endgroup$
add a comment |
$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)
$endgroup$
add a comment |
$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)
$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)
answered yesterday
RobRob
38339
38339
add a comment |
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
$begingroup$
If
imageUrlString
is empty, you'll never beDone
; Force-unwrappingi.key.last
calls for trouble; The logic that makes sure thatvalues
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