We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 5fb906a + 9cd51d4 commit ee918daCopy full SHA for ee918da
algorithms/sorting/counting_sort.m
@@ -3,22 +3,19 @@
3
% INPUT: array of integers between 1 and k
4
% OUTPUT: sorted array
5
6
- ans = []; % result array (sorted array)
+ ans = zeros(size(arr)); % result array (sorted array)
7
8
9
- C = zeros(k); % array for counting
10
- C = C(1,:);
+ C = zeros(1,k); % array for counting
11
12
% counts the occurs of element i
13
for i = 1 : length(arr)
14
C(arr(i)) += 1;
15
endfor
16
17
% adress calculation
18
- for j = 1 : k
19
- if j > 1
20
- C(j) += C(j-1);
21
- endif
+ for j = 2 : k
+ C(j) += C(j-1);
22
23
24
for m = length(arr) : -1 : 1
0 commit comments