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
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.

Um, show us some real code? We're not going to debug pseudocode, that's a waste of time.

Here's the VB6 code of the command button that contains the algorithm. It shows incorrect output.

``````rivate Sub Command3_Click()
size = List2.ListCount
If size > 1 Then
Dim a, b, i, max, min, NOP, NON, x, y, z, PosArray(10000), NegArray(10000) As Integer
maxi = Val(List2.List(0))
mini = Val(List2.List(0))
NOP = 0
NON = 0
For a = 0 To size - 1
If Val(List2.List(a)) >= maxi Then
maxi = Val(List2.List(a))
ElseIf Val(List2.List(a)) < mini Then
mini = Val(List2.List(a))
End If
If Val(List2.List(a)) < 0 Then
NegArray(NON) = Val(List2.List(a))
NON = NON + 1
Else
PosArray(NOP) = Val(List2.List(a))
NOP = NOP + 1
End If
Next
If mini <> maxi Then
Dim k, t As Integer
If NOP > 0 Then
For k = 0 To maxi + 1
PosArray(k) = mini - 1
Next
End If
If NON > 0 Then
For t = 0 To Abs(mini) + 1
NegArray(t) = mini - 1
Next
End If
For b = 0 To size - 1
If Val(List2.List(b)) >= 0 Then
PosArray(List2.List(b)) = PosArray(Val(List2.List(b))) + 1
Else
NegArray(Abs(Val(List2.List(b)))) = NegArray(Abs(Val(List2.List(b)))) + 1
End If
Next
i = 0
If NON > 0 Then
For x = Abs(mini) To 1
If NegArray(x) = 1 Then
List2.List(x) = -1 * x
i = i + 1
Else
If NegArray(x) > 1 Then
z = i
End If
End If
Next
End If
For y = z To z + NegArray(x) - 1
List2.List(i) = -1 * x
i = i + 1
Next
If NOP > 0 Then
For x = 0 To maxi
If PosArray(x) = 1 Then
List2.List(x) = x
i = i + 1
Else
If PosArray(x) > 1 Then
z = i
End If
End If
Next
End If
For y = z To z + PosArray(x) - 1
List2.List(i) = x
i = i + 1
Next
End If
End If
End Sub``````

Lines 24-34 aren't in your psuedocode (and I believe they are causing your problem). What are you trying to accomplish with them?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.