Question: The following c code sorts an array using Triage sort algorithm. Triage sort is a recursive sorting algorithm.The algorithm is defined as follows: If there
The following c code sorts an array using Triage sort algorithm. Triage sort is a recursive sorting algorithm.The algorithm is defined as follows:
If there are only 2 elements in the list, compare them, swap if required. If there are 3 or more elements in the list, then:
Stooge sort the initial 2/3 of the list
Stooge sort the final 2/3 of the list
Stooge sort the initial 2/3 of the list again
Here is the c code:
int size = 6; int array[6] = {5,7,2,20,3,11}; void t_s ( int * array, int size){ int temp; int s; int m; if(size==2){ if(array[0] else{ if(((size*2)%3)==0) m=0; else m=1; s = ((size*2)/3) +m; t_s(array,s); t_s(array+(size-s),s); t_s(array,s); } } int main() { t_s(array,size); return 0; } It is important to get the integer sort size used in the recursive calls by rounding the 2/3 upwards, e.g. rounding 2/3 of 5 should give 4 rather than 3, as otherwise the sort can fail on certain data. To round up, you need to find the quotient and reminder for divide by 3. You can use the following instructions for RISC-V: rem Rd,Rs1,Rs2 #Rd = reminder(Rs1/Rs2) div Rd,Rs1,RS2 #Rd = quotient(Rs1/Rs2) Complete the following code snippet: .data Array: .word 5, 7, 2, 20, 3, 11 .text addi x10,x0,6 # X10 = size of Array la x11,Array # x11 = base address of Array jal x1,T_S # call function T_S beq x0,x0,END T_S: #put your code here END: nop
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
