GeckoPrimed

Pinescript Selection Sort Using Arrays

The selection sort algorithm sorts an array by repeatedly finding the smallest element from unsorted array and pushing it to the beginning. Two subarrays are maintained during the execution of the script. One of the subarrays is in a sorted state while the other is in a sorted state. After each iteration of the for loop the sorted list is searched for the next correct element which is then pushed onto the sorted subarray.

Worst case performance : О(n^2) comparisons and O(n)swaps
Best case performance : O(n^2) comparisons and O(n) Swaps
Average performance: О(n^2) comparisons and O(n) Swaps
Worst-case space complexity:О(n) total, O(1) auxiliary

The Pseudocode is given below
procedure selection sort 
   list  : array of items
   n     : size of list

   for i = 1 to n - 1
   /* set current element as minimum*/
      min = i    
  
      /* check the element to be minimum */

      for j = i+1 to n 
         if list[j] < list[min] then
            min = j;
         end if
      end for

      /* swap the minimum element with the current element*/
      if indexMin != i  then
         swap list[min] and list[i]
      end if
   end for
	
end procedure
Script de código aberto

Dentro do verdadeiro espírito TradingView, o autor deste script publicou ele como um script de código aberto, para que os traders possam compreender e checar ele. Um viva ao autor! Você pode usá-lo gratuitamente, mas a reutilização deste código em uma publicação é regida pelas Regras da Casa. Você pode favoritá-lo para usá-lo em um gráfico.

Aviso legal

As informações e publicações não devem ser e não constituem conselhos ou recomendações financeiras, de investimento, de negociação ou de qualquer outro tipo, fornecidas ou endossadas pela TradingView. Leia mais em Termos de uso.

Quer usar esse script no gráfico?