Please help with this new sorting algorithm. I implement it on VB but i cant get a correct output. It's a new sorting algorithm and the pseudocode was provided. Here are the informations about it.
Here are the steps:
1. In a single pass on the array, the algorithm finds max, min, Number Of Positive (NOP) elements, and Number Of Negative (NON) elements.
2. If min equals max then the array is already sorted, go to step 6, otherwise, go to step 3.
3. Checks the values NOP and NON to create the
additional arrays:
a. If NOP is greater than zero then creates a new
array (PosArray) of size (max+1).
b. If NON is greater than zero then creates a new
array NegArray of size (|min|+1).
4. The algorithm now, in a single pass, checks the elements of the original array and:
a. If the current element is positive, then the algorithm increments by one the value of the element in the PosArray which has an index equals to the value of the current element itself.
b. If the current element is negative, then the algorithm increments by one the value of the element in the NegArray which has an index equals to the absolute value of the current element itself.
5. The algorithm copies the values of the NegArray and PosArray through the following steps:
a. Starting from the last element of the NegArray
and ending at the value 1, if the current element
is greater than zero, then copies the value of the
current element's index into the original array
using a for-loop which repeats based on the value
of the current element.
b. Starting from the first element of the PosArray
and ending at the value max, if the current
element is greater than zero, then copies the value
of the current element's index into the original
array using a for-loop which repeats based on the
value of the current element.
6. Exit
Here is the pseudocode:
1 if size > 1 then
2 var a, b, max, min, NOP, NON, x, y, z
3 max:= array(0)
4 min:= array(0)
5 NOP:= 0
6 NON:= 0
7 for a: = 0 to size-1 do
Find max, min, Number of positive
elements (NOP), and Number of
negative elements (NON).
8 if min ≠ max then
9 if NOP > 0 then
10 create a new array: PosArray[max+1]
11 if NON > 0 then
12 create a new array: NegArray[|min|+1]
13 for b:= 0 to size-1 do
14 if array(b)>= 0 then
15 PosArray(array(b)):= PosArray(array(b))+1
16 else
17 NegArray(|array(b)|):= NegArray(|array(b)|)+1
18 if NON > 0 then
19 for x:= |min| downto 1 do
20 if NegArray(x) == 1 then
21 array(i):= -1*x
22 i:= i+1
23 else
24 if NegArray(x) > 1 then
25 z=i
26 for y:= z to z+NegArray(x)-1 do
27 array(i) := -1* x
28 i:= i+1
29 if NOP > 0 then
30 for x:= 0 to max do
31 if PosArray(x) == 1 then
32 array(i):= x
33 i:= i+1
34 else
35 if PosArray(x) > 1 then
36 z=i
37 for y:= z to z+PosArray(x)-1 do
38 array(i) := x
39 i:= i+1
40 return array
PS: I think the main prob is the initialization of the 2 arrays. It was not included on the pseudocode. I already tried min-1, min, min+1, max-1 but it does not give correct output. I implement it on VB and the only missing part is the initial value of the 2 arrays.
Please help . Thanks!