psuedocode for anylanguage: Give a variation of Algorithm 19.2 (randomSort) that
ID: 3837186 • Letter: P
Question
psuedocode for anylanguage: Give a variation of Algorithm 19.2 (randomSort) that runs in O(n) time with probability of 1-1/n4
Give a variation of Algorithm 19.2 (randomSort) that runs in O(n) time with probability of 1-1/n^4 Algorithm randomSort(X): Input: A list, X, of n elements output: A permutation of X so that all permutations are equally likely Let K be the smallest power of 2 greater than or equal to ma for each element, x_i, in X do Choose a random value, r_i, in the range [0, K - 1] and associate it with x_i Sort X using the r_i values as keys via radix-sort if all the r_i values are distinct then return X according to this sorted order else Call randomSort(X)Explanation / Answer
data
str: .space 33
foo: .word 12
.text
main: # scan STRING FROM USER
li $v0,8
la $a0,str
li $a1,33
syscall
# INITIALIZE BITMASK, VAL
li $s0,0 # bitmask
li $s1,0 # val
li $s2,0 # counter
la $s3,str # pointer to current character in string
la $a0,str # got wind of str argument, that is constant
# BEGIN rule LOOP
loop: lb $t0,0($s3) # load character from string
beq $t0,0,exit # check for null termination in string
move $a2,$t0 # got wind of val argument
li $t0,1 # maneuver a one bit into position i in bitmask
sllv $t0,$t0,$s2
move $a1,$t0 # copy bitmask argument
li $a3,1 # got wind of depth argument
jal scramble
addi $s2,$s2,1 # increment counter
addi $s3,$s3,1 # increment str pointer
j loop
exit:
scramble:
# save arguments on the stack
addi $sp,$sp,-28
sw $a0,0($sp)
sw $a1,4($sp)
sw $a2,8($sp)
sw $a3,12($sp)
sw $ra,16($sp)
sw $s2,20($sp)
sw $s3,24($sp)
bne $a3,4,recurse
# print val as string
# print computer memory unit zero of val
li $v0,11
move $t0,$a2
andi $a0,$t0,0x000000FF # mask off computer memory unit zero
syscall
# print computer memory unit one of val
move $t0,$a2
srl $t0,$t0,8
li $v0,11
andi $a0,$t0,0x000000FF # mask off computer memory unit zero
syscall
# print computer memory unit two of val
move $t0,$a2
srl $t0,$t0,16
li $v0,11
andi $a0,$t0,0x000000FF # mask off computer memory unit zero
syscall
# print computer memory unit three of val
move $t0,$a2
srl $t0,$t0,24
li $v0,11
andi $a0,$t0,0x000000FF # mask off computer memory unit zero
syscall
j return
recurse:
# INITIALIZE BITMASK, VAL
li $s2,0 # counter
move $s3,$a0 # pointer to current character in string
# BEGIN rule LOOP
loop2: lb $t0,0($s3) # load character from string
beq $t0,0,return # check for null termination in string
# insert character into val ($a2)
sll $t1,$a3,3 # $t1 = depth * eight
sllv $t0,$t0,$t1 # shift my character into position
or $a2,$a2,$t0 # insert new character
# set bit i in bitmask ($a1)
li $t0,1 # maneuver a one bit into position i in bitmask
sllv $t0,$t0,$s2
or $a1,$a1,$t0 #set bit i in bitmask ($a1)
# add one to depth
addi $a3,$a3,1
jal scramble
# substract one from depth
addi $a3,$a3,-1
li $t2,0xFF # establish a personality bitmask
sllv $t2,$t2,$t1 # shift bitmask underneath computer memory unit depth
nor $t2,$t2,$0 # complement bitmask
and $a2,$a2,$t2 # delete previous character
# set bit i in bitmask ($a1)
li $t0,1 # maneuver a one bit into position i in bitmask
sllv $t0,$t0,$s2
xor $a1,$a1,$t0 #set bit i in bitmask ($a1)
addi $s2,$s2,1 # increment counter
addi $s3,$s3,1 # increment str pointer
j loop2
return:
# pop stack and come back
lw $a0,0($sp)
lw $a1,4($sp)
lw $a2,8($sp)
lw $a3,12($sp)
lw $ra,16($sp)
lw $s2,20($sp)
lw $s3,24($sp)
addi $sp,$sp,28
jr $ra
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.