Implementing and Visualizing Bubble Sort with Duplicate Removal in ProBuilder

28 Jan 2023
0 comment
0 attachment

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:

  • Initialization: Two arrays, $da for data and $la for labels, are filled with rounded closing prices and their corresponding bar indices.
  • Visualization of Unsorted Data: Before sorting, the elements of the arrays are displayed on the chart using drawtext.
  • Bubble Sort Algorithm: A nested loop structure is used to compare and swap elements to sort the array in ascending order. Both the data and their corresponding labels are swapped simultaneously.
  • Visualization of Sorted Data: After sorting, the sorted elements are displayed on the chart to visualize the changes.
  • Duplicate Removal: A new set of arrays ($dx and $lx) is created to store unique elements and their labels by comparing each element with the next one.
  • Visualization of Data without Duplicates: Finally, the unique elements are displayed, showing the result of the deduplication process.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/output-array-with-label-after-sort/#post-185443

Visit Link
What is a Snippet? A snippet is a small, reusable chunk of code designed to solve specific tasks quickly. Think of it as a shortcut that helps you achieve your coding goals without reinventing the wheel. How to Use: Simply copy the snippet and paste it into your project where needed. Don't forget to tweak it to fit your context. Snippets are not just time-savers; they're also learning tools to help you become a more efficient coder.
robertogozzi Master
Roberto https://www.ots-onlinetradingsoftware.com
Author’s Profile

Comments

Search Snippets

Showing some results...
Sorry, no result found!

Snippets Categories

global
35
indicator
133
strategy
171

Recent Snippets

How to Create a Simple MTF Trend Dashboard with EMA and SMA
indicator
This indicator builds a compact multi-timeframe (MTF) dashboard that shows whether price is trading above or below a [...]
How to Display Per-Bar Volume Accumulation in Real Time (Intrabar Updates)
global
This snippet tracks and displays the current bar’s accumulated volume while the bar is still forming, instead of only [...]
Ticks Counter: Count Tick Updates Per Bar on Tick or Time Charts
global
This snippet counts how many tick updates have occurred for the current bar by incrementing a per-bar counter on each [...]
How to Build a Step-Based Trailing Stop That Moves to Break-Even First
strategy
This snippet implements a step trailing stop that advances in fixed increments once price reaches predefined profit [...]
Utilizing Arrays to Track and Compare Indicator Values Within the Same Bar in ProBuilder
indicator
This ProBuilder code snippet demonstrates how to use arrays to compare the values of an indicator (RSI in this case) [...]
Logo Logo
Loading...