Skip to content

Commit c495abf

Browse files
committed
Merge pull request kodecocodes#103 from aliHafizji/counting_sort
Add counting sort
2 parents 8cb25af + 12eb87d commit c495abf

File tree

7 files changed

+444
-0
lines changed

7 files changed

+444
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//: Playground - noun: a place where people can play
2+
3+
import UIKit
4+
import Foundation
5+
6+
enum CountingSortError: ErrorType {
7+
case ArrayEmpty
8+
}
9+
10+
11+
func countingSort(array: [Int]) throws-> Array<Int> {
12+
13+
guard array.count > 0 else {
14+
throw CountingSortError.ArrayEmpty
15+
}
16+
17+
// Step 1:
18+
// Create an array to store the count of each element
19+
let maxElement = array.maxElement() ?? 0
20+
21+
var countArray = [Int](count: Int(maxElement + 1), repeatedValue: 0)
22+
23+
for element in array {
24+
countArray[element] += 1
25+
}
26+
27+
// Step 2
28+
// Set each value to be the sum of the previous two values
29+
for index in 1 ..< countArray.count {
30+
let sum = countArray[index] + countArray[index-1]
31+
countArray[index] = sum
32+
}
33+
34+
// Step 3
35+
// Place the element in the final array as per the number of elements before it
36+
var sortedArray = [Int](count: array.count, repeatedValue: 0)
37+
for element in array {
38+
countArray[element] = countArray[element] - 1
39+
sortedArray[countArray[element]] = element
40+
}
41+
return sortedArray
42+
}
43+
44+
try countingSort([10, 9, 8, 7, 1, 2, 3, 4, 5])
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// Sort.swift
3+
// test
4+
//
5+
// Created by Kauserali on 11/04/16.
6+
// Copyright © 2016 Ali Hafizji. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
enum CountingSortError: ErrorType {
12+
case ArrayEmpty
13+
}
14+
15+
16+
func countingSort(array: [Int]) throws-> Array<Int> {
17+
18+
guard array.count > 0 else {
19+
throw CountingSortError.ArrayEmpty
20+
}
21+
22+
// Step 1:
23+
// Create an array to store the count of each element
24+
let maxElement = array.maxElement() ?? 0
25+
26+
var countArray = [Int](count: Int(maxElement + 1), repeatedValue: 0)
27+
28+
for element in array {
29+
countArray[element] += 1
30+
}
31+
32+
// Step 2
33+
// Set each value to be the sum of the previous two values
34+
for index in 1 ..< countArray.count {
35+
let sum = countArray[index] + countArray[index-1]
36+
countArray[index] = sum
37+
}
38+
39+
// Step 3
40+
// Place the element in the final array as per the number of elements before it
41+
var sortedArray = [Int](count: array.count, repeatedValue: 0)
42+
for element in array {
43+
countArray[element] = countArray[element] - 1
44+
sortedArray[countArray[element]] = element
45+
}
46+
return sortedArray
47+
}
Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
// !$*UTF8*$!
2+
{
3+
archiveVersion = 1;
4+
classes = {
5+
};
6+
objectVersion = 46;
7+
objects = {
8+
9+
/* Begin PBXBuildFile section */
10+
617F9B441CBBB9080085A8F4 /* CountingSortTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 617F9B431CBBB9080085A8F4 /* CountingSortTest.swift */; };
11+
617F9B491CBBB9500085A8F4 /* CountingSort.swift in Sources */ = {isa = PBXBuildFile; fileRef = 617F9B3B1CBBB8BD0085A8F4 /* CountingSort.swift */; };
12+
/* End PBXBuildFile section */
13+
14+
/* Begin PBXFileReference section */
15+
617F9B3B1CBBB8BD0085A8F4 /* CountingSort.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CountingSort.swift; sourceTree = "<group>"; };
16+
617F9B401CBBB9080085A8F4 /* CountingSort.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CountingSort.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
17+
617F9B431CBBB9080085A8F4 /* CountingSortTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CountingSortTest.swift; sourceTree = "<group>"; };
18+
617F9B451CBBB9080085A8F4 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
19+
/* End PBXFileReference section */
20+
21+
/* Begin PBXFrameworksBuildPhase section */
22+
617F9B3D1CBBB9080085A8F4 /* Frameworks */ = {
23+
isa = PBXFrameworksBuildPhase;
24+
buildActionMask = 2147483647;
25+
files = (
26+
);
27+
runOnlyForDeploymentPostprocessing = 0;
28+
};
29+
/* End PBXFrameworksBuildPhase section */
30+
31+
/* Begin PBXGroup section */
32+
617F9B341CBBB8A90085A8F4 = {
33+
isa = PBXGroup;
34+
children = (
35+
617F9B3B1CBBB8BD0085A8F4 /* CountingSort.swift */,
36+
617F9B421CBBB9080085A8F4 /* CountingSort */,
37+
617F9B411CBBB9080085A8F4 /* Products */,
38+
);
39+
sourceTree = "<group>";
40+
};
41+
617F9B411CBBB9080085A8F4 /* Products */ = {
42+
isa = PBXGroup;
43+
children = (
44+
617F9B401CBBB9080085A8F4 /* CountingSort.xctest */,
45+
);
46+
name = Products;
47+
sourceTree = "<group>";
48+
};
49+
617F9B421CBBB9080085A8F4 /* CountingSort */ = {
50+
isa = PBXGroup;
51+
children = (
52+
617F9B431CBBB9080085A8F4 /* CountingSortTest.swift */,
53+
617F9B451CBBB9080085A8F4 /* Info.plist */,
54+
);
55+
path = CountingSort;
56+
sourceTree = "<group>";
57+
};
58+
/* End PBXGroup section */
59+
60+
/* Begin PBXNativeTarget section */
61+
617F9B3F1CBBB9080085A8F4 /* CountingSort */ = {
62+
isa = PBXNativeTarget;
63+
buildConfigurationList = 617F9B481CBBB9080085A8F4 /* Build configuration list for PBXNativeTarget "CountingSort" */;
64+
buildPhases = (
65+
617F9B3C1CBBB9080085A8F4 /* Sources */,
66+
617F9B3D1CBBB9080085A8F4 /* Frameworks */,
67+
617F9B3E1CBBB9080085A8F4 /* Resources */,
68+
);
69+
buildRules = (
70+
);
71+
dependencies = (
72+
);
73+
name = CountingSort;
74+
productName = CountingSort;
75+
productReference = 617F9B401CBBB9080085A8F4 /* CountingSort.xctest */;
76+
productType = "com.apple.product-type.bundle.unit-test";
77+
};
78+
/* End PBXNativeTarget section */
79+
80+
/* Begin PBXProject section */
81+
617F9B351CBBB8A90085A8F4 /* Project object */ = {
82+
isa = PBXProject;
83+
attributes = {
84+
LastSwiftUpdateCheck = 0730;
85+
LastUpgradeCheck = 0730;
86+
TargetAttributes = {
87+
617F9B3F1CBBB9080085A8F4 = {
88+
CreatedOnToolsVersion = 7.3;
89+
};
90+
};
91+
};
92+
buildConfigurationList = 617F9B381CBBB8A90085A8F4 /* Build configuration list for PBXProject "CountingSort" */;
93+
compatibilityVersion = "Xcode 3.2";
94+
developmentRegion = English;
95+
hasScannedForEncodings = 0;
96+
knownRegions = (
97+
en,
98+
);
99+
mainGroup = 617F9B341CBBB8A90085A8F4;
100+
productRefGroup = 617F9B411CBBB9080085A8F4 /* Products */;
101+
projectDirPath = "";
102+
projectRoot = "";
103+
targets = (
104+
617F9B3F1CBBB9080085A8F4 /* CountingSort */,
105+
);
106+
};
107+
/* End PBXProject section */
108+
109+
/* Begin PBXResourcesBuildPhase section */
110+
617F9B3E1CBBB9080085A8F4 /* Resources */ = {
111+
isa = PBXResourcesBuildPhase;
112+
buildActionMask = 2147483647;
113+
files = (
114+
);
115+
runOnlyForDeploymentPostprocessing = 0;
116+
};
117+
/* End PBXResourcesBuildPhase section */
118+
119+
/* Begin PBXSourcesBuildPhase section */
120+
617F9B3C1CBBB9080085A8F4 /* Sources */ = {
121+
isa = PBXSourcesBuildPhase;
122+
buildActionMask = 2147483647;
123+
files = (
124+
617F9B491CBBB9500085A8F4 /* CountingSort.swift in Sources */,
125+
617F9B441CBBB9080085A8F4 /* CountingSortTest.swift in Sources */,
126+
);
127+
runOnlyForDeploymentPostprocessing = 0;
128+
};
129+
/* End PBXSourcesBuildPhase section */
130+
131+
/* Begin XCBuildConfiguration section */
132+
617F9B391CBBB8A90085A8F4 /* Debug */ = {
133+
isa = XCBuildConfiguration;
134+
buildSettings = {
135+
};
136+
name = Debug;
137+
};
138+
617F9B3A1CBBB8A90085A8F4 /* Release */ = {
139+
isa = XCBuildConfiguration;
140+
buildSettings = {
141+
};
142+
name = Release;
143+
};
144+
617F9B461CBBB9080085A8F4 /* Debug */ = {
145+
isa = XCBuildConfiguration;
146+
buildSettings = {
147+
ALWAYS_SEARCH_USER_PATHS = NO;
148+
CLANG_ANALYZER_NONNULL = YES;
149+
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
150+
CLANG_CXX_LIBRARY = "libc++";
151+
CLANG_ENABLE_MODULES = YES;
152+
CLANG_ENABLE_OBJC_ARC = YES;
153+
CLANG_WARN_BOOL_CONVERSION = YES;
154+
CLANG_WARN_CONSTANT_CONVERSION = YES;
155+
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
156+
CLANG_WARN_EMPTY_BODY = YES;
157+
CLANG_WARN_ENUM_CONVERSION = YES;
158+
CLANG_WARN_INT_CONVERSION = YES;
159+
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
160+
CLANG_WARN_UNREACHABLE_CODE = YES;
161+
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
162+
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
163+
COPY_PHASE_STRIP = NO;
164+
DEBUG_INFORMATION_FORMAT = dwarf;
165+
ENABLE_STRICT_OBJC_MSGSEND = YES;
166+
ENABLE_TESTABILITY = YES;
167+
GCC_C_LANGUAGE_STANDARD = gnu99;
168+
GCC_DYNAMIC_NO_PIC = NO;
169+
GCC_NO_COMMON_BLOCKS = YES;
170+
GCC_OPTIMIZATION_LEVEL = 0;
171+
GCC_PREPROCESSOR_DEFINITIONS = (
172+
"DEBUG=1",
173+
"$(inherited)",
174+
);
175+
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
176+
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
177+
GCC_WARN_UNDECLARED_SELECTOR = YES;
178+
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
179+
GCC_WARN_UNUSED_FUNCTION = YES;
180+
GCC_WARN_UNUSED_VARIABLE = YES;
181+
INFOPLIST_FILE = CountingSort/Info.plist;
182+
IPHONEOS_DEPLOYMENT_TARGET = 9.3;
183+
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
184+
MTL_ENABLE_DEBUG_INFO = YES;
185+
ONLY_ACTIVE_ARCH = YES;
186+
PRODUCT_BUNDLE_IDENTIFIER = com.alihafizji.CountingSort;
187+
PRODUCT_NAME = "$(TARGET_NAME)";
188+
SDKROOT = iphoneos;
189+
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
190+
};
191+
name = Debug;
192+
};
193+
617F9B471CBBB9080085A8F4 /* Release */ = {
194+
isa = XCBuildConfiguration;
195+
buildSettings = {
196+
ALWAYS_SEARCH_USER_PATHS = NO;
197+
CLANG_ANALYZER_NONNULL = YES;
198+
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
199+
CLANG_CXX_LIBRARY = "libc++";
200+
CLANG_ENABLE_MODULES = YES;
201+
CLANG_ENABLE_OBJC_ARC = YES;
202+
CLANG_WARN_BOOL_CONVERSION = YES;
203+
CLANG_WARN_CONSTANT_CONVERSION = YES;
204+
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
205+
CLANG_WARN_EMPTY_BODY = YES;
206+
CLANG_WARN_ENUM_CONVERSION = YES;
207+
CLANG_WARN_INT_CONVERSION = YES;
208+
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
209+
CLANG_WARN_UNREACHABLE_CODE = YES;
210+
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
211+
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
212+
COPY_PHASE_STRIP = NO;
213+
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
214+
ENABLE_NS_ASSERTIONS = NO;
215+
ENABLE_STRICT_OBJC_MSGSEND = YES;
216+
GCC_C_LANGUAGE_STANDARD = gnu99;
217+
GCC_NO_COMMON_BLOCKS = YES;
218+
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
219+
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
220+
GCC_WARN_UNDECLARED_SELECTOR = YES;
221+
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
222+
GCC_WARN_UNUSED_FUNCTION = YES;
223+
GCC_WARN_UNUSED_VARIABLE = YES;
224+
INFOPLIST_FILE = CountingSort/Info.plist;
225+
IPHONEOS_DEPLOYMENT_TARGET = 9.3;
226+
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
227+
MTL_ENABLE_DEBUG_INFO = NO;
228+
PRODUCT_BUNDLE_IDENTIFIER = com.alihafizji.CountingSort;
229+
PRODUCT_NAME = "$(TARGET_NAME)";
230+
SDKROOT = iphoneos;
231+
VALIDATE_PRODUCT = YES;
232+
};
233+
name = Release;
234+
};
235+
/* End XCBuildConfiguration section */
236+
237+
/* Begin XCConfigurationList section */
238+
617F9B381CBBB8A90085A8F4 /* Build configuration list for PBXProject "CountingSort" */ = {
239+
isa = XCConfigurationList;
240+
buildConfigurations = (
241+
617F9B391CBBB8A90085A8F4 /* Debug */,
242+
617F9B3A1CBBB8A90085A8F4 /* Release */,
243+
);
244+
defaultConfigurationIsVisible = 0;
245+
defaultConfigurationName = Release;
246+
};
247+
617F9B481CBBB9080085A8F4 /* Build configuration list for PBXNativeTarget "CountingSort" */ = {
248+
isa = XCConfigurationList;
249+
buildConfigurations = (
250+
617F9B461CBBB9080085A8F4 /* Debug */,
251+
617F9B471CBBB9080085A8F4 /* Release */,
252+
);
253+
defaultConfigurationIsVisible = 0;
254+
};
255+
/* End XCConfigurationList section */
256+
};
257+
rootObject = 617F9B351CBBB8A90085A8F4 /* Project object */;
258+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// CountingSort.swift
3+
// CountingSort
4+
//
5+
// Created by Kauserali on 11/04/16.
6+
//
7+
//
8+
9+
import XCTest
10+
11+
class CountingSort: XCTestCase {
12+
13+
func testCountingSort() {
14+
let sequence = [10, 8, 1, 2, 5, 8]
15+
let sortedSequence = [1, 2, 5, 8, 8, 10]
16+
17+
do {
18+
let afterCountingSort = try countingSort(sequence)
19+
XCTAssertEqual(afterCountingSort, sortedSequence)
20+
} catch {
21+
XCTFail("")
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)