├── README.md ├── SortingAlgorithmsWithSwiftCharts.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist └── xcuserdata │ └── mike.xcuserdatad │ └── xcschemes │ └── xcschememanagement.plist ├── SortingAlgorithmsWithSwiftCharts ├── Assets.xcassets │ ├── AccentColor.colorset │ │ └── Contents.json │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── Contents.json ├── ContentView.swift ├── Preview Content │ └── Preview Assets.xcassets │ │ └── Contents.json ├── SignalGenerator.swift └── SortingAlgorithmsWithSwiftChartsApp.swift └── thumbnail2.png /README.md: -------------------------------------------------------------------------------- 1 | # Visualize sorting in Swift Charts 2 | 3 | [![Visualize sorting in Swift Charts](thumbnail2.png)](https://www.youtube.com/watch?v=UCSXF741iHI) 4 | 5 | This is a code from a YT tutorial on how to use Swift Charts in a little inconvenient way 🤓  6 | 7 | You will learn how to code a different sorting algorithms and visualize them using Swift Charts. 8 | 9 | ## Links: 10 | - [Link to the video](https://www.youtube.com/watch?v=UCSXF741iHI) 11 | - [Swift Charts](https://developer.apple.com/documentation/charts) 12 | - [Building a Signal Generator](https://developer.apple.com/documentation/avfaudio/audio_engine/building_a_signal_generator) -------------------------------------------------------------------------------- /SortingAlgorithmsWithSwiftCharts.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 56; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 27A29B5328A8100100B70BF1 /* SortingAlgorithmsWithSwiftChartsApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27A29B5228A8100100B70BF1 /* SortingAlgorithmsWithSwiftChartsApp.swift */; }; 11 | 27A29B5528A8100100B70BF1 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27A29B5428A8100100B70BF1 /* ContentView.swift */; }; 12 | 27A29B5728A8100100B70BF1 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 27A29B5628A8100100B70BF1 /* Assets.xcassets */; }; 13 | 27A29B5A28A8100100B70BF1 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 27A29B5928A8100100B70BF1 /* Preview Assets.xcassets */; }; 14 | 27A29B6128A8320300B70BF1 /* SignalGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27A29B6028A8320300B70BF1 /* SignalGenerator.swift */; }; 15 | /* End PBXBuildFile section */ 16 | 17 | /* Begin PBXFileReference section */ 18 | 27A29B4F28A8100100B70BF1 /* SortingAlgorithmsWithSwiftCharts.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SortingAlgorithmsWithSwiftCharts.app; sourceTree = BUILT_PRODUCTS_DIR; }; 19 | 27A29B5228A8100100B70BF1 /* SortingAlgorithmsWithSwiftChartsApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SortingAlgorithmsWithSwiftChartsApp.swift; sourceTree = ""; }; 20 | 27A29B5428A8100100B70BF1 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; 21 | 27A29B5628A8100100B70BF1 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 22 | 27A29B5928A8100100B70BF1 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; 23 | 27A29B6028A8320300B70BF1 /* SignalGenerator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SignalGenerator.swift; sourceTree = ""; }; 24 | /* End PBXFileReference section */ 25 | 26 | /* Begin PBXFrameworksBuildPhase section */ 27 | 27A29B4C28A8100000B70BF1 /* Frameworks */ = { 28 | isa = PBXFrameworksBuildPhase; 29 | buildActionMask = 2147483647; 30 | files = ( 31 | ); 32 | runOnlyForDeploymentPostprocessing = 0; 33 | }; 34 | /* End PBXFrameworksBuildPhase section */ 35 | 36 | /* Begin PBXGroup section */ 37 | 27A29B4628A8100000B70BF1 = { 38 | isa = PBXGroup; 39 | children = ( 40 | 27A29B5128A8100100B70BF1 /* SortingAlgorithmsWithSwiftCharts */, 41 | 27A29B5028A8100100B70BF1 /* Products */, 42 | ); 43 | sourceTree = ""; 44 | }; 45 | 27A29B5028A8100100B70BF1 /* Products */ = { 46 | isa = PBXGroup; 47 | children = ( 48 | 27A29B4F28A8100100B70BF1 /* SortingAlgorithmsWithSwiftCharts.app */, 49 | ); 50 | name = Products; 51 | sourceTree = ""; 52 | }; 53 | 27A29B5128A8100100B70BF1 /* SortingAlgorithmsWithSwiftCharts */ = { 54 | isa = PBXGroup; 55 | children = ( 56 | 27A29B5228A8100100B70BF1 /* SortingAlgorithmsWithSwiftChartsApp.swift */, 57 | 27A29B5428A8100100B70BF1 /* ContentView.swift */, 58 | 27A29B6028A8320300B70BF1 /* SignalGenerator.swift */, 59 | 27A29B5628A8100100B70BF1 /* Assets.xcassets */, 60 | 27A29B5828A8100100B70BF1 /* Preview Content */, 61 | ); 62 | path = SortingAlgorithmsWithSwiftCharts; 63 | sourceTree = ""; 64 | }; 65 | 27A29B5828A8100100B70BF1 /* Preview Content */ = { 66 | isa = PBXGroup; 67 | children = ( 68 | 27A29B5928A8100100B70BF1 /* Preview Assets.xcassets */, 69 | ); 70 | path = "Preview Content"; 71 | sourceTree = ""; 72 | }; 73 | /* End PBXGroup section */ 74 | 75 | /* Begin PBXNativeTarget section */ 76 | 27A29B4E28A8100000B70BF1 /* SortingAlgorithmsWithSwiftCharts */ = { 77 | isa = PBXNativeTarget; 78 | buildConfigurationList = 27A29B5D28A8100100B70BF1 /* Build configuration list for PBXNativeTarget "SortingAlgorithmsWithSwiftCharts" */; 79 | buildPhases = ( 80 | 27A29B4B28A8100000B70BF1 /* Sources */, 81 | 27A29B4C28A8100000B70BF1 /* Frameworks */, 82 | 27A29B4D28A8100000B70BF1 /* Resources */, 83 | ); 84 | buildRules = ( 85 | ); 86 | dependencies = ( 87 | ); 88 | name = SortingAlgorithmsWithSwiftCharts; 89 | productName = SortingAlgorithmsWithSwiftCharts; 90 | productReference = 27A29B4F28A8100100B70BF1 /* SortingAlgorithmsWithSwiftCharts.app */; 91 | productType = "com.apple.product-type.application"; 92 | }; 93 | /* End PBXNativeTarget section */ 94 | 95 | /* Begin PBXProject section */ 96 | 27A29B4728A8100000B70BF1 /* Project object */ = { 97 | isa = PBXProject; 98 | attributes = { 99 | BuildIndependentTargetsInParallel = 1; 100 | LastSwiftUpdateCheck = 1400; 101 | LastUpgradeCheck = 1400; 102 | TargetAttributes = { 103 | 27A29B4E28A8100000B70BF1 = { 104 | CreatedOnToolsVersion = 14.0; 105 | }; 106 | }; 107 | }; 108 | buildConfigurationList = 27A29B4A28A8100000B70BF1 /* Build configuration list for PBXProject "SortingAlgorithmsWithSwiftCharts" */; 109 | compatibilityVersion = "Xcode 14.0"; 110 | developmentRegion = en; 111 | hasScannedForEncodings = 0; 112 | knownRegions = ( 113 | en, 114 | Base, 115 | ); 116 | mainGroup = 27A29B4628A8100000B70BF1; 117 | productRefGroup = 27A29B5028A8100100B70BF1 /* Products */; 118 | projectDirPath = ""; 119 | projectRoot = ""; 120 | targets = ( 121 | 27A29B4E28A8100000B70BF1 /* SortingAlgorithmsWithSwiftCharts */, 122 | ); 123 | }; 124 | /* End PBXProject section */ 125 | 126 | /* Begin PBXResourcesBuildPhase section */ 127 | 27A29B4D28A8100000B70BF1 /* Resources */ = { 128 | isa = PBXResourcesBuildPhase; 129 | buildActionMask = 2147483647; 130 | files = ( 131 | 27A29B5A28A8100100B70BF1 /* Preview Assets.xcassets in Resources */, 132 | 27A29B5728A8100100B70BF1 /* Assets.xcassets in Resources */, 133 | ); 134 | runOnlyForDeploymentPostprocessing = 0; 135 | }; 136 | /* End PBXResourcesBuildPhase section */ 137 | 138 | /* Begin PBXSourcesBuildPhase section */ 139 | 27A29B4B28A8100000B70BF1 /* Sources */ = { 140 | isa = PBXSourcesBuildPhase; 141 | buildActionMask = 2147483647; 142 | files = ( 143 | 27A29B6128A8320300B70BF1 /* SignalGenerator.swift in Sources */, 144 | 27A29B5528A8100100B70BF1 /* ContentView.swift in Sources */, 145 | 27A29B5328A8100100B70BF1 /* SortingAlgorithmsWithSwiftChartsApp.swift in Sources */, 146 | ); 147 | runOnlyForDeploymentPostprocessing = 0; 148 | }; 149 | /* End PBXSourcesBuildPhase section */ 150 | 151 | /* Begin XCBuildConfiguration section */ 152 | 27A29B5B28A8100100B70BF1 /* Debug */ = { 153 | isa = XCBuildConfiguration; 154 | buildSettings = { 155 | ALWAYS_SEARCH_USER_PATHS = NO; 156 | CLANG_ANALYZER_NONNULL = YES; 157 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 158 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; 159 | CLANG_ENABLE_MODULES = YES; 160 | CLANG_ENABLE_OBJC_ARC = YES; 161 | CLANG_ENABLE_OBJC_WEAK = YES; 162 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 163 | CLANG_WARN_BOOL_CONVERSION = YES; 164 | CLANG_WARN_COMMA = YES; 165 | CLANG_WARN_CONSTANT_CONVERSION = YES; 166 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 167 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 168 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 169 | CLANG_WARN_EMPTY_BODY = YES; 170 | CLANG_WARN_ENUM_CONVERSION = YES; 171 | CLANG_WARN_INFINITE_RECURSION = YES; 172 | CLANG_WARN_INT_CONVERSION = YES; 173 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 174 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 175 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 176 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 177 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 178 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 179 | CLANG_WARN_STRICT_PROTOTYPES = YES; 180 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 181 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 182 | CLANG_WARN_UNREACHABLE_CODE = YES; 183 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 184 | COPY_PHASE_STRIP = NO; 185 | DEBUG_INFORMATION_FORMAT = dwarf; 186 | ENABLE_STRICT_OBJC_MSGSEND = YES; 187 | ENABLE_TESTABILITY = YES; 188 | GCC_C_LANGUAGE_STANDARD = gnu11; 189 | GCC_DYNAMIC_NO_PIC = NO; 190 | GCC_NO_COMMON_BLOCKS = YES; 191 | GCC_OPTIMIZATION_LEVEL = 0; 192 | GCC_PREPROCESSOR_DEFINITIONS = ( 193 | "DEBUG=1", 194 | "$(inherited)", 195 | ); 196 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 197 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 198 | GCC_WARN_UNDECLARED_SELECTOR = YES; 199 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 200 | GCC_WARN_UNUSED_FUNCTION = YES; 201 | GCC_WARN_UNUSED_VARIABLE = YES; 202 | IPHONEOS_DEPLOYMENT_TARGET = 16.0; 203 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 204 | MTL_FAST_MATH = YES; 205 | ONLY_ACTIVE_ARCH = YES; 206 | SDKROOT = iphoneos; 207 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 208 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 209 | }; 210 | name = Debug; 211 | }; 212 | 27A29B5C28A8100100B70BF1 /* Release */ = { 213 | isa = XCBuildConfiguration; 214 | buildSettings = { 215 | ALWAYS_SEARCH_USER_PATHS = NO; 216 | CLANG_ANALYZER_NONNULL = YES; 217 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 218 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; 219 | CLANG_ENABLE_MODULES = YES; 220 | CLANG_ENABLE_OBJC_ARC = YES; 221 | CLANG_ENABLE_OBJC_WEAK = YES; 222 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 223 | CLANG_WARN_BOOL_CONVERSION = YES; 224 | CLANG_WARN_COMMA = YES; 225 | CLANG_WARN_CONSTANT_CONVERSION = YES; 226 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 227 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 228 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 229 | CLANG_WARN_EMPTY_BODY = YES; 230 | CLANG_WARN_ENUM_CONVERSION = YES; 231 | CLANG_WARN_INFINITE_RECURSION = YES; 232 | CLANG_WARN_INT_CONVERSION = YES; 233 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 234 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 235 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 236 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 237 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 238 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 239 | CLANG_WARN_STRICT_PROTOTYPES = YES; 240 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 241 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 242 | CLANG_WARN_UNREACHABLE_CODE = YES; 243 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 244 | COPY_PHASE_STRIP = NO; 245 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 246 | ENABLE_NS_ASSERTIONS = NO; 247 | ENABLE_STRICT_OBJC_MSGSEND = YES; 248 | GCC_C_LANGUAGE_STANDARD = gnu11; 249 | GCC_NO_COMMON_BLOCKS = YES; 250 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 251 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 252 | GCC_WARN_UNDECLARED_SELECTOR = YES; 253 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 254 | GCC_WARN_UNUSED_FUNCTION = YES; 255 | GCC_WARN_UNUSED_VARIABLE = YES; 256 | IPHONEOS_DEPLOYMENT_TARGET = 16.0; 257 | MTL_ENABLE_DEBUG_INFO = NO; 258 | MTL_FAST_MATH = YES; 259 | SDKROOT = iphoneos; 260 | SWIFT_COMPILATION_MODE = wholemodule; 261 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 262 | VALIDATE_PRODUCT = YES; 263 | }; 264 | name = Release; 265 | }; 266 | 27A29B5E28A8100100B70BF1 /* Debug */ = { 267 | isa = XCBuildConfiguration; 268 | buildSettings = { 269 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 270 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 271 | CODE_SIGN_STYLE = Automatic; 272 | CURRENT_PROJECT_VERSION = 1; 273 | DEVELOPMENT_ASSET_PATHS = "\"SortingAlgorithmsWithSwiftCharts/Preview Content\""; 274 | ENABLE_PREVIEWS = YES; 275 | GENERATE_INFOPLIST_FILE = YES; 276 | INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; 277 | INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; 278 | INFOPLIST_KEY_UILaunchScreen_Generation = YES; 279 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 280 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 281 | LD_RUNPATH_SEARCH_PATHS = ( 282 | "$(inherited)", 283 | "@executable_path/Frameworks", 284 | ); 285 | MARKETING_VERSION = 1.0; 286 | PRODUCT_BUNDLE_IDENTIFIER = com.mikemikina.SortingAlgorithmsWithSwiftCharts; 287 | PRODUCT_NAME = "$(TARGET_NAME)"; 288 | SWIFT_EMIT_LOC_STRINGS = YES; 289 | SWIFT_VERSION = 5.0; 290 | TARGETED_DEVICE_FAMILY = "1,2"; 291 | }; 292 | name = Debug; 293 | }; 294 | 27A29B5F28A8100100B70BF1 /* Release */ = { 295 | isa = XCBuildConfiguration; 296 | buildSettings = { 297 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 298 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 299 | CODE_SIGN_STYLE = Automatic; 300 | CURRENT_PROJECT_VERSION = 1; 301 | DEVELOPMENT_ASSET_PATHS = "\"SortingAlgorithmsWithSwiftCharts/Preview Content\""; 302 | ENABLE_PREVIEWS = YES; 303 | GENERATE_INFOPLIST_FILE = YES; 304 | INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; 305 | INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; 306 | INFOPLIST_KEY_UILaunchScreen_Generation = YES; 307 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 308 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 309 | LD_RUNPATH_SEARCH_PATHS = ( 310 | "$(inherited)", 311 | "@executable_path/Frameworks", 312 | ); 313 | MARKETING_VERSION = 1.0; 314 | PRODUCT_BUNDLE_IDENTIFIER = com.mikemikina.SortingAlgorithmsWithSwiftCharts; 315 | PRODUCT_NAME = "$(TARGET_NAME)"; 316 | SWIFT_EMIT_LOC_STRINGS = YES; 317 | SWIFT_VERSION = 5.0; 318 | TARGETED_DEVICE_FAMILY = "1,2"; 319 | }; 320 | name = Release; 321 | }; 322 | /* End XCBuildConfiguration section */ 323 | 324 | /* Begin XCConfigurationList section */ 325 | 27A29B4A28A8100000B70BF1 /* Build configuration list for PBXProject "SortingAlgorithmsWithSwiftCharts" */ = { 326 | isa = XCConfigurationList; 327 | buildConfigurations = ( 328 | 27A29B5B28A8100100B70BF1 /* Debug */, 329 | 27A29B5C28A8100100B70BF1 /* Release */, 330 | ); 331 | defaultConfigurationIsVisible = 0; 332 | defaultConfigurationName = Release; 333 | }; 334 | 27A29B5D28A8100100B70BF1 /* Build configuration list for PBXNativeTarget "SortingAlgorithmsWithSwiftCharts" */ = { 335 | isa = XCConfigurationList; 336 | buildConfigurations = ( 337 | 27A29B5E28A8100100B70BF1 /* Debug */, 338 | 27A29B5F28A8100100B70BF1 /* Release */, 339 | ); 340 | defaultConfigurationIsVisible = 0; 341 | defaultConfigurationName = Release; 342 | }; 343 | /* End XCConfigurationList section */ 344 | }; 345 | rootObject = 27A29B4728A8100000B70BF1 /* Project object */; 346 | } 347 | -------------------------------------------------------------------------------- /SortingAlgorithmsWithSwiftCharts.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /SortingAlgorithmsWithSwiftCharts.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /SortingAlgorithmsWithSwiftCharts.xcodeproj/xcuserdata/mike.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | SortingAlgorithmsWithSwiftCharts.xcscheme_^#shared#^_ 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /SortingAlgorithmsWithSwiftCharts/Assets.xcassets/AccentColor.colorset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "colors" : [ 3 | { 4 | "idiom" : "universal" 5 | } 6 | ], 7 | "info" : { 8 | "author" : "xcode", 9 | "version" : 1 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /SortingAlgorithmsWithSwiftCharts/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "platform" : "ios", 6 | "size" : "1024x1024" 7 | } 8 | ], 9 | "info" : { 10 | "author" : "xcode", 11 | "version" : 1 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /SortingAlgorithmsWithSwiftCharts/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /SortingAlgorithmsWithSwiftCharts/ContentView.swift: -------------------------------------------------------------------------------- 1 | import Charts 2 | import SwiftUI 3 | 4 | struct ContentView: View { 5 | var input: [Int] { 6 | var array = [Int]() 7 | for i in 1...30 { 8 | array.append(i) 9 | } 10 | 11 | return array.shuffled() 12 | } 13 | 14 | @State var data = [Int]() 15 | @State var activeValue = 0 16 | @State var previousValue = 0 17 | @State var checkValue: Int? 18 | 19 | var body: some View { 20 | VStack { 21 | Chart { 22 | ForEach(Array(zip(data.indices, data)), id: \.0) { index, item in 23 | BarMark(x: .value("Position", index), y: .value("Value", item)) 24 | .foregroundStyle(getColor(value: item).gradient) 25 | } 26 | } 27 | .frame(width: 280, height: 250) 28 | 29 | Button { 30 | Task { 31 | try await quicksort(&data, low: 0, high: data.count - 1) 32 | 33 | activeValue = 0 34 | previousValue = 0 35 | 36 | for index in 0.. Int { 62 | let pivot = array[high] 63 | 64 | var i = low 65 | for j in low.. 1 else { 92 | return 93 | } 94 | 95 | for i in 0.. data[j] { 101 | activeValue = data[j] 102 | beep(data[j]) 103 | smallest = j 104 | try await Task.sleep(until: .now.advanced(by: .milliseconds(20)), clock: .continuous) 105 | } 106 | } 107 | 108 | if smallest != i { 109 | activeValue = data[i] 110 | previousValue = data[smallest] 111 | beep(data[smallest]) 112 | data.swapAt(smallest, i) 113 | try await Task.sleep(until: .now.advanced(by: .milliseconds(20)), clock: .continuous) 114 | } 115 | } 116 | } 117 | 118 | @MainActor 119 | func insertionSort() async throws { 120 | guard data.count >= 2 else { 121 | return 122 | } 123 | 124 | for i in 1..= 2 else { 142 | return 143 | } 144 | 145 | for i in 0.. data[j + 1] { 151 | beep(data[j + 1]) 152 | data.swapAt(j + 1, j) 153 | try await Task.sleep(until: .now.advanced(by: .milliseconds(20)), clock: .continuous) 154 | } 155 | } 156 | } 157 | } 158 | 159 | func getColor(value: Int) -> Color { 160 | if let checkValue, value <= checkValue { 161 | return .green 162 | } 163 | 164 | if value == activeValue { 165 | return .green 166 | } else if value == previousValue { 167 | return .yellow 168 | } 169 | 170 | return .blue 171 | } 172 | } 173 | 174 | struct ContentView_Previews: PreviewProvider { 175 | static var previews: some View { 176 | ContentView() 177 | } 178 | } 179 | -------------------------------------------------------------------------------- /SortingAlgorithmsWithSwiftCharts/Preview Content/Preview Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /SortingAlgorithmsWithSwiftCharts/SignalGenerator.swift: -------------------------------------------------------------------------------- 1 | import AVFoundation 2 | import Foundation 3 | 4 | class Beep { 5 | var mixer: AVAudioMixerNode? 6 | var timer = Timer() 7 | var timerStart = Timer() 8 | let engine = AVAudioEngine() 9 | 10 | func beep(_ frequency: Int) { 11 | let frequency = Float(frequency) * Float(frequency) + 800 12 | let amplitude = Float(1) 13 | let duration = 0.05 14 | 15 | let twoPi = 2 * Float.pi 16 | 17 | let sine = { (phase: Float) -> Float in 18 | sin(phase) 19 | } 20 | 21 | var signal: (Float) -> Float 22 | 23 | signal = sine 24 | 25 | let mainMixer = engine.mainMixerNode 26 | mixer = mainMixer 27 | let output = engine.outputNode 28 | let outputFormat = output.inputFormat(forBus: 0) 29 | let sampleRate = Float(outputFormat.sampleRate) 30 | // Use the output format for the input, but reduce the channel count to 1. 31 | let inputFormat = AVAudioFormat(commonFormat: outputFormat.commonFormat, 32 | sampleRate: outputFormat.sampleRate, 33 | channels: 1, 34 | interleaved: outputFormat.isInterleaved) 35 | 36 | var currentPhase: Float = 0 37 | // The interval to advance the phase each frame. 38 | let phaseIncrement = (twoPi / sampleRate) * frequency 39 | 40 | let srcNode = AVAudioSourceNode { _, _, frameCount, audioBufferList -> OSStatus in 41 | let ablPointer = UnsafeMutableAudioBufferListPointer(audioBufferList) 42 | for frame in 0 ..< Int(frameCount) { 43 | // Get the signal value for this frame at time. 44 | let value = signal(currentPhase) * amplitude 45 | // Advance the phase for the next frame. 46 | currentPhase += phaseIncrement 47 | if currentPhase >= twoPi { 48 | currentPhase -= twoPi 49 | } 50 | if currentPhase < 0.0 { 51 | currentPhase += twoPi 52 | } 53 | // Set the same value on all channels (due to the inputFormat, there's only one channel though). 54 | for buffer in ablPointer { 55 | let buf: UnsafeMutableBufferPointer = UnsafeMutableBufferPointer(buffer) 56 | buf[frame] = value 57 | } 58 | } 59 | return noErr 60 | } 61 | 62 | engine.attach(srcNode) 63 | 64 | engine.connect(srcNode, to: mainMixer, format: inputFormat) 65 | engine.connect(mainMixer, to: output, format: outputFormat) 66 | mainMixer.outputVolume = 0.15 67 | 68 | do { 69 | try engine.start() 70 | CFRunLoopRunInMode(.defaultMode, CFTimeInterval(duration), false) 71 | timer = Timer.scheduledTimer(timeInterval: 0.001, target: self, selector: #selector(process), userInfo: index, repeats: true) 72 | } catch { 73 | print("Could not start engine: \(error)") 74 | } 75 | } 76 | 77 | @objc private func process(_ timer: Timer) { 78 | guard let mixer else { 79 | return 80 | } 81 | 82 | mixer.outputVolume = mixer.outputVolume * 0.9 83 | 84 | if mixer.outputVolume <= 0.009 { 85 | timer.invalidate() 86 | engine.stop() 87 | } 88 | } 89 | } 90 | 91 | func beep(_ frequency: Int) { 92 | let beep = Beep() 93 | beep.beep(frequency) 94 | } 95 | -------------------------------------------------------------------------------- /SortingAlgorithmsWithSwiftCharts/SortingAlgorithmsWithSwiftChartsApp.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | 3 | @main 4 | struct SortingAlgorithmsWithSwiftChartsApp: App { 5 | var body: some Scene { 6 | WindowGroup { 7 | ContentView() 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /thumbnail2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mikina/SortingAlgorithmsWithSwiftCharts/37b4f56b3448a91c2c46a86aaef24db51a0f7090/thumbnail2.png --------------------------------------------------------------------------------