019 一维数组 选择法对十个整数进行排序
2015-01-22 16:21
288 查看
/***********************019 一维数组*********************** * 使用选择法对十个整数进行排序。 * C语言精彩编程百例第19 */ #include<stdio.h> void main() { int i,j,min,temp; int array[10]; printf("please input ten integer:\n"); for(i=0;i<10;i++) { printf("array[%d]=",i); scanf("%d",&array[i]); } printf("The array is:"); for(i=0;i<9;i++) { min=i; for(j=i;j<10;j++) if(array[min]>array[j]) min =j; temp=array[i]; array[i]=array[min]; array[min]=temp; } printf("\nThe result:\n"); for(i=0;i<10;i++) printf("%d",array[i]); printf("\n"); }
对应的汇编内容:
.file "019.c" .def ___main; .scl 2; .type 32; .endef .text LC0: .ascii "please input ten integer:\12\0" LC1: .ascii "array[%d]=\0" LC2: .ascii "%d \0" LC3: .ascii "The array is:\0" LC4: .ascii "\12The result:\12\0" LC5: .ascii "\12\0" .align 2 .globl _main .def _main; .scl 2; .type 32; .endef _main: pushl %ebp movl %esp, %ebp subl $88, %esp andl $-16, %esp movl $0, %eax movl %eax, -76(%ebp) movl -76(%ebp), %eax call __alloca call ___main subl $12, %esp # printf("please input ten integer:\n") pushl $LC0 call _printf addl $16, %esp movl $0, -12(%ebp) # i=0 L4: cmpl $9, -12(%ebp) # i<10 jle L7 jmp L5 L7: subl $8, %esp # printf("array[%d]=",i); pushl -12(%ebp) pushl $LC1 call _printf addl $16, %esp subl $8, %esp # scanf("%d",&array[i]); leal -72(%ebp), %edx # edx = &array[0] movl -12(%ebp), %eax # eax = i sall $2, %eax # eax= eax*4 leal (%eax,%edx), %eax # eax= &array[0]+i*4 pushl %eax # scanf("%d",&array[i]); pushl $LC2 call _scanf addl $16, %esp leal -12(%ebp), %eax # &i incl (%eax) # i++ jmp L4 # 下次循环 L5: subl $12, %esp # printf("The array is:"); pushl $LC3 call _printf addl $16, %esp movl $0, -12(%ebp) # i=0 L8: cmpl $8, -12(%ebp) # i 和 9-1比较 jle L11 # i<9 jmp L9 L11: movl -12(%ebp), %eax movl %eax, -20(%ebp) # min =i movl -12(%ebp), %eax movl %eax, -16(%ebp) # j =i L12: cmpl $9, -16(%ebp) # j 和 10-1 比 jle L15 jmp L13 L15: movl -20(%ebp), %eax # eax =min movl -16(%ebp), %edx # edx =j movl -72(%ebp,%eax,4), %eax # eax =[ebp+eax*4-72]=arrar[min] cmpl -72(%ebp,%edx,4), %eax # array[min] 和 array[j] 比较 jle L14 # 小于等于跳 movl -16(%ebp), %eax # eax = j movl %eax, -20(%ebp) # min =j L14: leal -16(%ebp), %eax # j++ incl (%eax) jmp L12 L13: movl -12(%ebp), %eax # eax =i movl -72(%ebp,%eax,4), %eax # eax = array[i] movl %eax, -24(%ebp) # temp=eax movl -12(%ebp), %edx # edx=i movl -20(%ebp), %eax # eax=min movl -72(%ebp,%eax,4), %eax # eax=array[min] movl %eax, -72(%ebp,%edx,4) # array[i]=eax movl -20(%ebp), %edx # edx=min movl -24(%ebp), %eax # eax=temp movl %eax, -72(%ebp,%edx,4) # array[min]=eax leal -12(%ebp), %eax # i++ incl (%eax) jmp L8 L9: subl $12, %esp # printf("\nThe result:\n"); pushl $LC4 call _printf addl $16, %esp movl $0, -12(%ebp) # i=0 L17: cmpl $9, -12(%ebp) # i<10 jle L20 jmp L18 L20: subl $8, %esp # printf("%d",array[i]); movl -12(%ebp), %eax pushl -72(%ebp,%eax,4) pushl $LC2 call _printf addl $16, %esp leal -12(%ebp), %eax # i++ incl (%eax) jmp L17 # 下次循环 L18: subl $12, %esp pushl $LC5 call _printf addl $16, %esp leave ret .def _scanf; .scl 2; .type 32; .endef .def _printf; .scl 2; .type 32; .endef
按照程序内容来看
int i,j,min,temp; 的内存位置 -12(%epb)~-24(%epb)
int array[10]; 的内存位置 [%ebp+0*4-72] ~ [%ebp+10*4-72]
相关文章推荐
- 对数组进行选择排序
- 一维数组的选择排序
- 使用指针法对数组进行冒泡排序、选择排序、插入排序法、二分查找法
- 有一个数组,包含10个整数,编写代码对前5个数进行升序排序,后5个数进行降序排序。
- 输入一个5X5的二维数组,将数组进行排序,其中一维数组按照平均值降序,一维数组内部升序排列。
- 用冒泡法将10个整数进行由大到小排序(数组)
- mapreduce对一维的数组进行排序
- 对一个5位数的任意整数,求出其降序数。例如,整数是82319,则其降序数是98321。算法提示:将整数的各位数分解到一维整型数组a中,再将a数组中的元素按降序排序,最后输出a数组元素值。
- 通过指针变量用选择法对数组中的10个整数按从大到小进行排序
- 用面向对象解决:输入一个5X5的二维数组,将数组进行排序,其中一维数组按照平均值降序,一维数组内部升序排列
- 从键盘输入十个整数存入一维数组中,求出它们的和及平均值并输出(要求用指针访问数组元素)?
- 已知一个几乎有序的数组,几乎有序是指,如果把数组排好顺序的话,每个元素移动的距离可以不超过k,并且k相对于数组来说比较小。请选择一个合适的排序算法针对这个数据进行排序。 给定一个int数组A,同时给定
- 用3位以内的随机整数填充长度为20的整型数组,然后使用冒泡排序法按升序进行排序,输出排序后的数组元素(每行输出10个元素)。
- 请通过代码实现以下功能:输入一个5X5的二维数组,将数组进行排序,其中一维数组按照平均值降序,一维数组内部升序排列。
- 选择排序算法---对一个数组进行选择排序
- 用选择的方式对数组进行排序,并写出对应的优化后的代码实现。(重点写思路、原理)
- 选择排序本质:内层循环找出最小值并与数组左边的交换,外层循环进行数组从左到右排序,从小到大排序。
- 精彩百例:一维数组,选择排序
- 输入一个5X5的二维数组,将数组进行排序,其中一维数组按照平均值降序,一维数组内部升序排列。
- 那些年我们刷过的算法题(排序)---有一个只由0,1,2三种元素构成的整数数组,请使用交换、原地排序而不是使用计数进行排序