Skip to content

Commit 9cd51d4

Browse files
committed
Improve counting sort
1 parent f22da2b commit 9cd51d4

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

algorithms/sorting/counting_sort.m

+4-7
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,19 @@
33
% INPUT: array of integers between 1 and k
44
% OUTPUT: sorted array
55

6-
ans = []; % result array (sorted array)
6+
ans = zeros(size(arr)); % result array (sorted array)
77

88

9-
C = zeros(k); % array for counting
10-
C = C(1,:);
9+
C = zeros(1,k); % array for counting
1110

1211
% counts the occurs of element i
1312
for i = 1 : length(arr)
1413
C(arr(i)) += 1;
1514
endfor
1615

1716
% adress calculation
18-
for j = 1 : k
19-
if j > 1
20-
C(j) += C(j-1);
21-
endif
17+
for j = 2 : k
18+
C(j) += C(j-1);
2219
endfor
2320

2421
for m = length(arr) : -1 : 1

0 commit comments

Comments
 (0)