This code snippet demonstrates how to implement a bubble sort algorithm on an array, visualize the sorting process, and remove duplicates from the sorted array in ProBuilder language. The example uses financial data (closing prices) but focuses on the sorting and deduplication logic.
// Data array $da ($dx is new array without duplicates)
// Label Array $la ($lx is new array without duplicates)
defparam drawonlastbaronly = true
// fill up the array with CLOSE and Bar ID (label)
for k = 0 to 7
$da[k] = round((close[k] - 0.5))
$la[k] = k
next
MaxElements = lastset($da)
//////////////////////////////////////////////////////////////////
// plot unsorted arrays
temp = MaxElements + 1
Offset = average[10](range[1])
drawtext("Elements #temp#",BarIndex-20,low - range)
for k = 0 to MaxElements
x = $da[k]
y = $la[k]
drawtext("close[#y#]: #x#",BarIndex-20,high + (Offset * k))
next
//////////////////////////////////////////////////////////////////
// (Bubble Sort)
FOR i = 0 TO MaxElements
FOR j = 0 TO MaxElements
IF $da[j] > $da[i] THEN
// swap data
temp = $da[j]
$da[j] = $da[i]
$da[i] = temp
// swap labels
temp = $la[j]
$la[j] = $la[i]
$la[i] = temp
ENDIF
NEXT
NEXT
//////////////////////////////////////////////////////////////////
// plot Sorted arrays
for k = 0 to MaxElements
x = $da[k]
y = $la[k]
drawtext("close[#y#]: #x#",BarIndex-10,high + (Offset * k))
next
//////////////////////////////////////////////////////////////////
// remove duplicates by comparing the current element to the next one (creating 2 new arrays)
NewMaxElements = 0
FOR i = 0 TO MaxElements
IF ($da[i] <> $da[i + 1]) OR (i = MaxElements) THEN
$dx[NewMaxElements] = $da[i] //save datum to new array, when different
$lx[NewMaxElements] = $la[i] //save label, too
NewMaxElements = NewMaxElements + 1
ENDIF
NEXT
//////////////////////////////////////////////////////////////////
// plot new Arrays (without duplicates)
temp = NewMaxElements
drawtext("Elements #temp#",BarIndex,low - range)
FOR k = 0 to NewMaxElements - 1
x = $dx[k]
y = $lx[k]
drawtext("close[#y#]: #x#",BarIndex,high + (Offset * k))
NEXT
return
This code snippet includes several key operations:
drawtext.Check out this related content for more information:
https://www.prorealcode.com/topic/output-array-with-label-after-sort/#post-185443
Visit Link