# Temporal Multimodal Data-Processing Algorithms Based on Algebraic System of Aggregates
## 5 Algorithms of Operations on Aggregates
### Ordering Operations
The result of applying the sorting operation to an aggregate \(A\) is an aggregate \(A_{\uparrow}=A\uparrow\bar{a}^{k}\) (for ascending sorting) or \(A_{\downarrow}=A\downarrow\bar{a}^{k}\) (for descending sorting) in all tuples, in which the elements are reordered according to the new ordering of the indices of the elements of the sorted primary tuple \(\bar{a}^{k}\). If some tuple is shorter than the primary tuple, it is appended by a dummy element and is then sorted. If some tuple is longer than the primary one, the elements with indices that exceed the largest index in the primary tuple, are not sorted (remain on their positions). An example of the result of sorting operation is given in Figure 17.  
The sorting operation can be realized using any appropriate sorting algorithm [20], including the one shown in Listing 8. ```
Input: aggregate \(A\) and primary tuple \(\left\langle a^{p}_{k_{p}}\right\rangle^{n_{p}}_{k_{p}=1}\) Output: aggregate \(R\) for\(k_{p}=1:n_{p}\)do for\(k_{m}=1:(n_{p}-k)\)do  superupply is false if\(a^{p}_{k_{m}}>a^{p}_{k_{m}+1}\)do for\(i=1:N\)do \(r^{i}_{k_{m}}=a^{i}_{k_{m}+1}\) \(r^{i}_{k_{m}+1}=a^{i}_{k_{m}}\)  end if  superuped is true  end for end if ifsuperped is false break loop  end end
```  
**Listing 8** Algorithm of the ascending sorting operation. Figure 17: An example of applying the sorting operation. The _extraction_ operation removes a certain element from the primary tuple of an aggregate. The result of extraction of the element \(a_{m}^{1}\) from the aggregate \(A\) is the aggregate \(A_{\times}=A\times a_{m}^{1}\) in all tuples, from which the elements with index \(m\) are removed. An example of the result of the extraction operation for \(m=2\) is given in Figure 18.  
The extraction operation can be fulfilled according to the algorithm in Listing 9. The _insertion_ operation adds a given element to the known position in the primary tuple of an aggregate. The result of insertion of the element \(a_{m}^{1}\) to the aggregate \(A\) is the aggregate \(A_{\times}=A\times a_{m}^{1}\) in all tuples (except the primary one), to which dummy elements with index \(m\) are added. An example of the result of insertion of a new element \(x\) to the second position of the primary tuple is shown in Figure 19, where the indices of the shifted elements remain as they numerated in the original tuple for explanatory purposes. The insertion operation can be fulfilled according to the algorithm in Listing 10. Figure 19: An example of applying the insertion operation. Figure 18: An example of applying the extraction operation. The singling operation removes the duplicate values that are located next to each other from the primary tuple of the aggregate; elements with the same indices as those of the removed duplicates in the primary tuple are simultaneously removed in all other tuples. An example of the result of the singling operation is given in Figure 20. Elements of the same color marked with the same border color are equal. The singling operation can be fulfilled according to the algorithm in Listing 11. ```
Input: aggregate \(A\) Output: aggregate \(R\) \(m=2\) while not all found do for\(k_{1}=m:n_{1}\)do if\(a_{k_{1}}^{1}=a_{k_{1}-1}^{1}\)then \(m=k_{1}\) break loop  end ifall found end for\(i=1:N\)do extract\(a_{m}^{i}\) end
```  
**Algorithm 11** Algorithm of the singling operation.