Instant Access to Data Structures and Algorithms in Swift: Implement Stacks, Queues, Dictionaries, and Lists in Your Apps 1st Edition Elshad Karimov ebook Full Chapters
Instant Access to Data Structures and Algorithms in Swift: Implement Stacks, Queues, Dictionaries, and Lists in Your Apps 1st Edition Elshad Karimov ebook Full Chapters
com
https://ebookmeta.com/product/data-structures-and-
algorithms-in-swift-implement-stacks-queues-dictionaries-
and-lists-in-your-apps-1st-edition-elshad-karimov/
OR CLICK HERE
DOWLOAD NOW
https://ebookmeta.com/product/data-structures-algorithms-in-python-
john-canning/
ebookmeta.com
https://ebookmeta.com/product/tyrannosaurus-rex-rebecca-sabelko/
ebookmeta.com
https://ebookmeta.com/product/savage-steamy-mountain-man-instalove-
romance-the-men-of-black-pine-woods-book-5-1st-edition-kelsie-
calloway/
ebookmeta.com
https://ebookmeta.com/product/phosphor-handbook-novel-phosphors-
systems-and-applications-3rd-edition-xiaojun-wang/
ebookmeta.com
https://ebookmeta.com/product/womens-economic-thought-in-the-romantic-
age-1st-edition-joanna-rostek/
ebookmeta.com
https://ebookmeta.com/product/damaged-gods-monsters-of-saint-
mark-s-1-1st-edition-ja-huss/
ebookmeta.com
Rousseau The Discourses and Other Early Political Writings
2nd Edition Rousseau
https://ebookmeta.com/product/rousseau-the-discourses-and-other-early-
political-writings-2nd-edition-rousseau/
ebookmeta.com
Data Structures
and Algorithms
in Swif t
Implement Stacks, Queues,
Dictionaries, and Lists in Your Apps
—
Elshad Karimov
www.allitebooks.com
Data Structures and
Algorithms in Swift
Implement Stacks, Queues,
Dictionaries, and Lists
in Your Apps
Elshad Karimov
www.allitebooks.com
Data Structures and Algorithms in Swift: Implement Stacks, Queues,
Dictionaries, and Lists in Your Apps
Elshad Karimov
New York, New York, USA
www.allitebooks.com
Table of Contents
About the Author���������������������������������������������������������������������������������xi
Chapter 1: Arrays����������������������������������������������������������������������������������1
Introduction������������������������������������������������������������������������������������������������1
Main Features of Arrays�����������������������������������������������������������������������������2
Retrieving Elements from an Array������������������������������������������������������������4
Adding Elements to an Array����������������������������������������������������������������������5
Removing Elements from an Array�������������������������������������������������������������6
Built-in Functions and Properties���������������������������������������������������������������6
isEmpty�������������������������������������������������������������������������������������������������6
First and Last����������������������������������������������������������������������������������������7
Reversed and Reverse��������������������������������������������������������������������������7
Count�����������������������������������������������������������������������������������������������������7
Conclusion��������������������������������������������������������������������������������������������������8
Chapter 2: Dictionaries�������������������������������������������������������������������������9
Introduction������������������������������������������������������������������������������������������������9
Accessing Values in a Dictionary��������������������������������������������������������������11
Adding/Modifying to a Dictionary�������������������������������������������������������������13
Removing a Value from a Dictionary��������������������������������������������������������14
iii
www.allitebooks.com
Table of Contents
Chapter 3: Sets�����������������������������������������������������������������������������������17
Accessing, Adding, and Removing an Element of a Set���������������������������17
Accessing an Element������������������������������������������������������������������������������17
Adding an Element�����������������������������������������������������������������������������������19
Removing Elements����������������������������������������������������������������������������������19
Set Operations������������������������������������������������������������������������������������������20
Comparison Operations�����������������������������������������������������������������������20
Membership and Equality Operations������������������������������������������������������24
Set Equality�����������������������������������������������������������������������������������������24
Set Membership����������������������������������������������������������������������������������24
Conclusion������������������������������������������������������������������������������������������������26
Chapter 4: Stacks�������������������������������������������������������������������������������27
Using Swift with Stacks���������������������������������������������������������������������������28
Stack Structures���������������������������������������������������������������������������������������31
Stack Extensions��������������������������������������������������������������������������������������32
Conclusion������������������������������������������������������������������������������������������������32
Chapter 5: Queue��������������������������������������������������������������������������������33
Implementation����������������������������������������������������������������������������������������35
Conclusion������������������������������������������������������������������������������������������������40
iv
www.allitebooks.com
Table of Contents
Chapter 8: Trees����������������������������������������������������������������������������������61
Creation����������������������������������������������������������������������������������������������������62
Insertion����������������������������������������������������������������������������������������������63
Searching Data������������������������������������������������������������������������������������64
Conclusion������������������������������������������������������������������������������������������������66
v
Table of Contents
vi
Table of Contents
Delete�������������������������������������������������������������������������������������������������������94
Deleting a Leaf������������������������������������������������������������������������������������94
Deleting a Node with One Child�����������������������������������������������������������95
Deleting a Node with Two Children�����������������������������������������������������96
Conclusion����������������������������������������������������������������������������������������������100
vii
Table of Contents
Selection Sort�����������������������������������������������������������������������������������������137
Implementation���������������������������������������������������������������������������������139
Insertion Sort������������������������������������������������������������������������������������������140
Implementation���������������������������������������������������������������������������������142
Merge Sort���������������������������������������������������������������������������������������������143
Implementation���������������������������������������������������������������������������������146
Quick Sort�����������������������������������������������������������������������������������������������148
Implementation���������������������������������������������������������������������������������151
Pivot Selection����������������������������������������������������������������������������������152
Conclusion����������������������������������������������������������������������������������������������153
viii
Table of Contents
Dijkstra’s Algorithm��������������������������������������������������������������������������������182
Implementation���������������������������������������������������������������������������������190
Algorithm�������������������������������������������������������������������������������������������191
Conclusion����������������������������������������������������������������������������������������������194
Chapter 17: Choosing the Best Algorithm����������������������������������������195
Sorting Algorithms����������������������������������������������������������������������������������196
Bubble Sort���������������������������������������������������������������������������������������196
Selection Sort������������������������������������������������������������������������������������196
Insertion Sort�������������������������������������������������������������������������������������197
Merge Sort����������������������������������������������������������������������������������������197
Quick Sort������������������������������������������������������������������������������������������197
Search Algorithms����������������������������������������������������������������������������������198
Linear Search vs. Binary Search�������������������������������������������������������198
Graph Search Algorithms (GSA)��������������������������������������������������������������200
Dijkstra’s Algorithm��������������������������������������������������������������������������������201
Index�������������������������������������������������������������������������������������������������203
ix
About the Author
Elshad Karimov is an experienced programmer with more than 8 years
experience of different programming languages and a solid background in
iOS development as well as Oracle, SQL, C#, Java, Python and HTML/CSS.
He’s familiar with the performance limits and characteristics of Swift and
the nature and function of embedded databases and system datastores.
xi
About the Technical Reviewer
Felipe Laso is a Senior Systems Engineer working at Lextech Global
Services. He’s also an aspiring game designer/programmer. You can follow
him on Twitter @iFeliLM or on his blog.
xiii
CHAPTER 1
Arrays
In this chapter, you will learn about arrays, their built-in properties, and
how to retrieve, add, and remove elements from them.
I ntroduction
An array is simply a container that can hold multiple data (values) of
any data type in an ordered list; this means that you get the elements
in the same order as you defined the items in the array. Instead of
declaring individual variables, such as number0, number1, and so on
… until number99, you declare one array variable such as numbers and
use numbers[0], numbers[1], and numbers[99] to represent individual
variables.
The simplest type of array is the linear array, which is also known as
a one-dimensional array. In Swift, arrays are a zero-based index. A one-
dimensional array can be written simply as shown in the following and
elements can be accessed using a[i], where i is an index between 0 and n:
é a0 ù
êa ú
a=ê ú
1
ê ú
ê ú
ë an û
2
Chapter 1 Arrays
Black
c[0] c[1] c[2]
Blue Green Red
Capacity : 3
Reallocation
Capacity : 6
Figure 1-2. Reallocation
3
Chapter 1 Arrays
//Output
3
4
Chapter 1 Arrays
//Output
1
2
3
4
5
myIntArray.append(11)
print(myIntArray)
//Output
[1, 2, 3, 4, 5, 11]
myIntArray.insert(12, at: 3)
print(myIntArray)
//Output
[1, 2, 3, 12, 4, 5, 11]
5
Chapter 1 Arrays
myIntArray.removeLast()
myIntArray.removeFirst()
myIntArray.remove(at: 1)
myIntArray.removeAll()
isEmpty
This property determines if an array is empty or not. It returns true if an
array does not contain any value, otherwise returns false.
false
6
Chapter 1 Arrays
print(myIntArray.first)
print(myIntArray.last)
Optional(1)
Optional(6)
As you can see, the output of these properties is optional. This means
that if the array is empty the return will be nil.
[6, 5, 3, 1]
Count
This property returns the total number of elements in an array.
print(myIntArray.count)
7
Chapter 1 Arrays
Important
While using subscript syntax to access elements of an array in Swift, you
must be sure the value lies in the index; otherwise, you will get a runtime
crash. Let’s see this in the following example:
print(myIntArray[-1])
found index
5
Conclusion
In this chapter, you have learned about the general structure of an array,
how to declare it in Swift, and how to select, add, and remove elements.
In the following chapter, you will learn about the data structure type of
dictionaries.
8
CHAPTER 2
Dictionaries
In this chapter, the dictionary type of data structures will be discussed. You
will learn how to access, add, remove, and modify elements in a dictionary.
Moreover, built-in properties and functions of dictionaries will be covered.
I ntroduction
A dictionary is another data type collection in Swift. This data structure
takes its name from real-world dictionaries where you have words
and their associated meaning; in programming, we use dictionaries to
associate a key with its value, so any value can be identified as long as we
know the key. If we look inside a real dictionary, we will find words and
each word has accompanying explanation and meaning.
Miller : a person who owns or works in a corn mill
There is a similar concept in Swift, in which we can express the
preceding dictionary entry like this where a key is a word from a real
dictionary and the value is an accompanying explanation.
var myDict = ["Miller" : "a person who owns or works in a corn mill",
"Programmer" : "a person who writes computer programs"]
We can add more than one key to the dictionary as long as it conforms
to the declaration of the dictionary. We can create dictionaries by explicitly
stating how the data is structured in a dictionary. In this, case we have the
key String data type and the value is also a String data type. If we state the
dictionary structure, the declaration will be like this:
Suppose you may want to search the capital city of a country. In that
case, you will create a dictionary with the key country and value capital
city. Now, you get the capital city from the collection by searching with
the key country. If we want to create a dictionary with different types of
values, we need to declare a heterogeneous dictionary which conforms to
the Hashable protocol. This type of dictionaries is useful when converting
JSON payloads.
// 1St
var myDictionary = Dictionary<Int, String>()
// 2nd
var myDictionary = [Int: String]()
// 3rd
var myDictionary:[Int: String] = [:]
10
Chapter 2 Dictionaries
11
Chapter 2 Dictionaries
Three
12
Chapter 2 Dictionaries
The key: 1
The key: 2
The key: 3
The value: One
The value: Three
The value: Four
Adding/Modifying to a Dictionary
To add a value to a dictionary, a subscript notation or
updateValue(_:forKey) method can be used. The subscript notation can
also be used to modify any existing value.
13
Chapter 2 Dictionaries
print(myDictionary.isEmpty)
false
First
This property is used to access the first element of a dictionary.
print(myDictionary.first)
14
Chapter 2 Dictionaries
Count
It returns the total number of elements in a dictionary.
print(myDictionary.count)
Keys
It returns all the keys inside a dictionary.
[1,2,3]
Conclusion
In this chapter, you have learned about the structure of dictionaries
and how to access, add, remove, and modify elements in it. The built-in
properties and functions provided by the Swift programming language are
also covered.
15
CHAPTER 3
Sets
It is an unordered collection (meaning you won't get the elements in the
same order as you defined) of unique, non-nil elements. It must conform
to the Hashable protocol. This means it has to provide a hashValue
property. This is important because sets are unordered and hashValue is
used to access the elements of the sets.
Access time is more efficient than arrays. When searching for an
element inside an array, the worst scenario is O(n), where n is the size of
the array, but in a set, it is always constant O(1). Unlike other collection
types while declaring sets, a set type must be specified.
// Accessing an element
for num in stringSet {
print(num)
}
Two
One
Three
//Ordered iteration
for num in stringSet.sorted() {
print(num)
}
Element found
18
Chapter 3 Sets
Adding an Element
Using the insert() method, a new element can be added to a set.
Removing Elements
There are several methods that can be used to remove the elements from
a set.
removeAll() or removeAll(keepCapacity) – To
remove all elements
//Remove an element
stringSet.remove("Four")
19
Discovering Diverse Content Through
Random Scribd Documents
“You go down to your tea and leave her ladyship’s looks alone. I
don’t know what you’re doing hanging about this landing at such an
hour of the day.”
Payne was an old servant in the Wynyard family, and he was aware
it had been generally said that “Master Owen had the looks and Miss
Leila the brains.” Master Owen was always a wild, harum-scarum
young fellow, and it wasn’t at all unlikely that he had got into one of
his scrapes. With this conviction implanted in his mind, Payne
deliberately descended the stairs, issued an edict to one of the
footmen, and retired into his lair and the evening paper.
CHAPTER II
BROTHER AND SISTER