选择排序的c语言描述:
# include <stdio.h>
int main(void)
{
int i, j; //循环变量
int Index; //保存最小的值的下标
int buf; //互换数据时的临时变量
int a[] = {xxx}; //数组元素
int n; //元素个数
for (i=0; i<n-1; ++i) //n个数比较n-1轮
{
Index = i;
for (j=i+1; j<n; ++j) //每轮比较n-1-i次, 找本轮最小数的下标
{
if (a[Index] > a[j])
{
Index = j; //保存小的数的下标
}
}
if (Index != i) /*找到最小数之后如果它的下标不是i则说明它不在最左边, 则互换位置*/
{
buf = a[Index];
a[Index] = a[i];
a[i] = buf;
}
}
}
汇编程序共分为以下几个部分:
main
主程序,负责协调各个子函数
input
数组元素输入模块
output
排序元素输出模块
findmin
在一次内层循环中找到index的模块
sort
循环主部分,主要是控制两层循环
.data
array: .space 400
message_input_n: .asciiz "Please input n:\n"
message_input_array: .asciiz "Please input a digit:\n"
message_output: .asciiz "Here is the sorted array:\n"
space: .asciiz " "
.text
main:
jal input
nop
move $t0, $v0
move $a0, $t0
move $s0, $t0
jal sort
nop
move $t0, $s0
move $a0, $t0
jal output
nop
li $v0, 10
syscall
input:
li $v0, 4
la $a0, message_input_n
syscall
li $v0, 5 # input n
syscall
move $t0, $v0 # [ $t0 ] is n
li $t1, 0 # [ t1 ] is i
for_1_begin:
slt $t2, $t1, $t0 # ( i < n )? 1 : 0
beq $t2, $zero, for_1_end # if i >= n then goto end
nop
la $t2, array # get the address of array to 【 $t2 】
sll $t3, $t1, 2 # t3 = t1 * 4
addu $t2, $t2, $t3 # t2 += t3
li $v0, 4
la $a0, message_input_array
syscall
li $v0, 5
syscall
sw $v0, 0($t2) # store the num to the array, whose address is 0($t2)
addi $t1, $t1, 1 # i++
j for_1_begin
nop
for_1_end:
move $v0, $t0
jr $ra
nop
output:
move $t0, $a0
li $v0, 4
la $a0, message_output
syscall
li $t1, 0
for_2_begin:
slt $t2, $t1, $t0
beq $t2, $zero, for_2_end
nop
la $t2, array # get the address of array to 【 $t2 】
sll $t3, $t1, 2 # t3 = t1 * 4
addu $t2, $t2, $t3 # t2 += t3
lw $a0, 0($t2)
li $v0, 1
syscall
li $v0, 4
la $a0, space
syscall
addi $t1, $t1, 1
j for_2_begin
nop
for_2_end:
jr $ra
nop
sort:
move $t0, $a0
li $t1, 0
for_3_begin:
slt $t2, $t1, $t0
beq $t2, $zero, for_3_end
nop
la $t2, array
sll $t3, $t1, 2 # t3 = t1 * 4
addu $t2, $t2, $t3 # t2 += t3
move $a0, $t0
move $a1, $t1
sw $t2, 0($sp)
subi $sp, $sp, 4
sw $t1, 0($sp)
subi $sp, $sp, 4
sw $t0, 0($sp)
subi $sp, $sp, 4
sw $ra, 0($sp)
subi $sp, $sp, 4
jal findmin
nop
addi $sp, $sp, 4
lw $ra, 0($sp)
addi $sp, $sp, 4
lw $t0, 0($sp)
addi $sp, $sp, 4
lw $t1, 0($sp)
addi $sp, $sp, 4
lw $t2, 0($sp)
lw $t3, 0($v0) # min address
lw $t4, 0($t2) # now address
sw $t3, 0($t2) # swap min and now
sw $t4, 0($v0)
addi $t1, $t1, 1
j for_3_begin
nop
for_3_end:
addiu $sp, $sp, 32 # take care of the [ $sp ]
jr $ra
nop
findmin:
la $t0, array # get the address of the last element
sll $a0, $a0, 2
subi $a0, $a0, 4
addu $t0, $t0, $a0
lw $t1, 0($t0) # init the min with the last element --> min = array[i]
move $t2, $t0 # set $t2 to the address of min elem ( that is $t0 )
move $t3, $t0 # use $t3 as i
la $t0, array
sll $a1, $a1, 2
addu $t0, $t0, $a1
for_4_begin:
sge $t4, $t3, $t0 # ( t3 >= t0 ) ? 1 : 0
beq $t4, $zero, for_4_end
no
lw $t5, 0($t3)
slt $t6, $t5, $t1
beq $t6, $zero, if_1_else
nop
move $t1, $t5 # min = arr[ j ]
move $t2, $t3 # index = j
j if_1_end
nop
if_1_else:
# nothing
if_1_end:
subi $t3, $t3, 4 # i -= 4
j for_4_begin
nop
for_4_end:
move $v0, $t2
jr $ra
nop