飘云阁

 找回密码
 加入我们

QQ登录

只需一步,快速开始

查看: 2176|回复: 3

编程马拉松(3)

[复制链接]

该用户从未签到

发表于 2007-8-13 08:14:09 | 显示全部楼层 |阅读模式
本贴引自第八阁论坛:http://bbs.chinadbg.cn/forum-7-1.html
还有飞翔技术论坛:http://www.powk.net/bbs/forumdisplay.php?fid=4&page=1
从键盘输入10个数,排序,请高手们写出高级语言+汇编+反汇编,代码
还有要注意,我写程序的错误,因为这才是真正要让大家学习的地方。
非常高兴看到大家的支持,你们发的程序,请在第二天加上注释。(主要是有错的程序)

基础知识:变量初始化,数组应用,条件判断,循环,和打印程序,还有基础的语法,还有一个任务找错!!。我写的是C代码,其它代码各个朋友自己理解一下,也可以PM发贴人。也可以发提问
以下是C语言,常用排序方法。注意一点是可以有方法提高效率的。

  1. main()
  2. {
  3. int i,j,a[10],b;
  4. for(i=0;i<10;i++)/*从键盘输入10个数*/
  5. scanf("%d",&a[i]) ;
  6. /*以下冒泡排序*/
  7. /*冒泡排序的基本思想:对于n个数进行排序(现假定是从大到小排序,以下均按此进行),将相邻两个数依次比较,将大数调在前头:也就是说第一个数和第二个数比较,大数放前,小数放后,第二个和第三个进行比较,大数放前、小数放后,然后依次类推。。。经过第一轮比较以后,我们找到一个最小数在最下面(沉底)。然后进行下一轮比较,最后一个数就不用再参加比较了,所以本轮就可以少比较一次。*/
  8. for(i=0;i<10;i++)
  9. for(j=0;j<10-i;j++)
  10.      if(a[j]<a[j+1])
  11. {
  12. b=a[j];a[j]=a[j+1];a[j+1]=b;
  13. }
  14. for(i=0;i<10;i++)
  15. printf("%d\n",a[i])  ;
  16. getch();
  17. /*以下选择排序*/
  18. /*排序的基本思想:先从第一个数开始起,用第一个数和其它的数进行比较,如果比第一个数大就交换位置,否则不进行交换,这样经过第一轮比较我们就能够找出最大值放在第一位置,然后从第二个位置起再找次大数,这样依次下去,就可以进行整个数的排序,实践证明,n个数最多需要n-1轮排序就可以了。*/

  19. /*以下插入排序*/
  20. /*插入排序基本思想:(假定从大到小排序)依次从后面拿一个数和前面已经排好序的数进行比较,比较的过程是从已经排好序的数中最后一个数开始比较,如果比这个数小,继续往前面比较,直到找到比它大的数,然后就放在它的后面,如果一直没有找到,肯定这个数已经比较到了第一个数,那就放到第一个数的前面。*/

  21. }
复制代码


  1. void Display(int a[], int n);

  2. void InsertSort(int a[], int n);
  3. void ShellSort(int a[], int n);
  4. void BubbleSort(int a[], int n);
  5. void QuickSort(int a[], int low, int high);
  6. void SelectSort(int a[], int n);
  7. void HeapSort(int a[], int n);

  8. int main(int argc, char* argv[])
  9. {
  10.         int a[20] ={83,78,12,45,189,98,54,10,17,18,
  11.                                 70,65,27,199,69,73,92,39,46,75,};
  12.         Display(a,20);

  13.         HeapSort(a,20);

  14.         Display(a,20);

  15.         int i = 0;
  16.         scanf("%d", &i);
  17.         return 0;
  18. }
  19. void Display(int a[], int n)
  20. {
  21.         for (int i=0; i<n; i++)
  22.         {
  23.                 printf("%d  ", a[i]);
  24.         }
  25.         printf("\n");
  26. }

  27. //插入排序
  28. void InsertSort(int a[], int n)
  29. {
  30.         int key;
  31.         int j;

  32.         for (int i=1; i<n; i++)
  33.         {
  34.                 key = a[i];
  35.                 j = i-1;
  36.                 while (j>=0 && key<a[j])
  37.                 {
  38.                         a[j+1] = a[j];
  39.                         j--;
  40.                 }
  41.                 a[j+1] = key;
  42.         }
  43. }

  44. //Shell排序
  45. void ShellSort(int a[], int n)
  46. {
  47.         int i,j,key;
  48.         int dl = n/2;
  49.        
  50.         while (dl >0)
  51.         {
  52.                 for (i=dl; i<n; i+=dl)
  53.                 {
  54.                         key = a[i];
  55.                         j = i - dl;
  56.                         while (j>=0 && key<a[j])
  57.                         {
  58.                                 a[j+dl] = a[j];
  59.                                 j = j - dl;
  60.                         }
  61.                         a[j+dl] = key;
  62.                 }
  63.                 dl = dl/2;
  64.         }
  65. }


  66. //冒泡排序
  67. void BubbleSort(int a[], int n)
  68. {
  69.         int tem, i, j, flag =1;

  70.         for (i=1; i<n && flag; i++)
  71.         {
  72.                 flag = 0;
  73.                 for (j=0; j<n-i; j++)
  74.                 {
  75.                         if (a[j]>a[j+1])
  76.                         {
  77.                                 tem = a[j];
  78.                                 a[j] = a[j+1];
  79.                                 a[j+1] = tem;
  80.                                 flag = 1;
  81.                         }
  82.                 }
  83.         }
  84. }


  85. //快速排序中调用划分区域函数
  86. int Partition(int a[], int low, int high)
  87. {       
  88.         int key = a[low];
  89.        
  90.         while (low < high)
  91.         {
  92.                 while (low <high && a[high]>=key) high --;
  93.                 a[low] = a[high];

  94.                 while  (low <high && a[low]<=key) low ++;
  95.                 a[high] = a[low];
  96.         }
  97.         a[low] = key;
  98.         return low;
  99. }

  100. //快速排序,递归函数,low为数组下界,high为数组上界
  101. void QuickSort(int a[], int low, int high)
  102. {
  103.         int par;
  104.        
  105.         if (low < high)
  106.         {
  107.                 par = Partition(a, low, high);
  108.                 QuickSort(a, low, par-1);
  109.                 QuickSort(a, par+1, high);
  110.         }
  111. }

  112. //选择排序
  113. void SelectSort(int a[], int n)
  114. {
  115.         int i,j,k,tem;

  116.         for (i=0; i<n-1; i++)
  117.         {
  118.                 k = i;
  119.                 for (j=i+1; j<n; j++)
  120.                 {
  121.                         if (a[k] > a[j])
  122.                         {
  123.                                 k=j;
  124.                         }
  125.                 }

  126.                 if (k!=i)
  127.                 {
  128.                         tem = a[i];
  129.                         a[i] = a[k];
  130.                         a[k] = tem;
  131.                 }
  132.         }
  133. }

  134. //堆排序中调用函数,从s这个结点开始调整堆为一个大堆
  135. void AdjustHeap(int a[], int s, int n)
  136. {
  137.         int i;

  138.         int key = a[s-1];
  139.         for (i=s*2; i<=n; i=i*2)
  140.         {
  141.        
  142.                 if (i<n && a[i-1] < a[i])
  143.                 {
  144.                         i++;
  145.                 }

  146.                 if (key> a[i-1])
  147.                 {
  148.                         break;
  149.                 }
  150.                
  151.                 a[s-1] = a[i-1];
  152.                 s = i;
  153.         }
  154.         a[s-1] = key;
  155. }

  156. //堆排序
  157. void HeapSort(int a[], int n)
  158. {
  159.         int i,j,tem;

  160.         for (i=n/2; i>0; i--)
  161.         {
  162.                 AdjustHeap(a, i, n);
  163.         }

  164.         for (j=0; j<n; j++)
  165.         {
  166.                 tem = a[n-j-1];
  167.                 a[n-j-1] = a[0];
  168.                 a[0] = tem;

  169.                 AdjustHeap(a, 1, n-j-1);
  170.         }

  171. }

复制代码

  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   InputString,ResultString:String;
  4.   TmpInt,IntCount,ErrorType,i,j:Integer;
  5.   IntNum:array[0..100] of Integer;
  6. begin
  7. ResultString:='';
  8. ErrorType:=0;
  9. InputString:= InputBox('输入窗口', '输入10个整数请用空格隔开', '1 2 3 4 5 6 7 8 9 0');
  10. InputString:=Trim(InputString)+' ';
  11.   while pos('  ',InputString)<>0 do Delete(InputString,pos('  ',InputString),1);//清除可能出现的多空格分隔符
  12.   If trim(InputString)='' then ErrorType:=1;//判断输入字符串的合法性
  13.   for i := 1 to Length(InputString) do
  14.     begin
  15.       if not (InputString[i] in ['0'..'9',#$20]) then ErrorType:=1;
  16.     end;
  17. if ErrorType<>1 then //数据无异常开始
  18. begin
  19.    IntCount:=0;
  20.    while Length(InputString)>0 do //开始填入数组
  21.    begin
  22.      IntNum[IntCount]:=StrToInt(LeftStr(InputString,pos(' ',InputString)-1)); //leftStr 从左取得分隔符之前的数字
  23.      Delete(InputString,1,pos(' ',InputString));
  24.      inc(IntCount);
  25.    end;
  26.    
  27.   //开始冒泡
  28.   for I := 0 to IntCount-1 do
  29.   begin
  30.     for j := i to IntCount-1 do
  31.     begin
  32.        if IntNum[i]<IntNum[j] then
  33.        begin
  34.          TmpInt:=IntNum[i];
  35.          IntNum[i]:=IntNum[j];
  36.          IntNum[j]:=TmpInt;
  37.        end;
  38.      end;
  39.    end;
  40. //泡泡冒完
  41.     for I := 0 to IntCount - 1 do
  42.     ResultString:=ResultString+'  '+IntToStr(IntNum[i]);
  43.     Showmessage('共输入了'+IntToStr(IntCount)+'数字。排序:'+ResultString);
  44. end
  45. else ShowMessage('输入中含非数字字符或没有输入数据!'); //数据无异常结束
  46. end;
复制代码

我的第一个C++程序

第一种方法,主要是函数不同,以及调用方式的不同
  1. //C++版冒泡排序程序
  2. #include<iOStream>
  3. using namespace std;
  4. void swap(int &a,int &b)
  5. {
  6.         int tem = a;
  7.         a = b;
  8.         b = tem;
  9. }


  10. void main()
  11. {
  12.         int i,j,arr[10];
  13.         for(i=0;i<10;i++)
  14.         {
  15.                 cout<<"请输入10个整数:";
  16.                 cin>>arr[i];
  17.     }
  18.         for(i=0;i<10;i++)                         //数组数据的循环比较
  19.         {        for(j=0;j<10-i-1;j++)    //每一轮的比较
  20.            { if(arr[j]<arr[j+1])       //比较相邻两个数大小
  21.         {  swap(arr[j],arr[j+1]);    //互换函数的调用
  22.                         
  23.         }
  24.         }
  25.         }
  26.         for(i=0;i<10;i++)
  27.         {        cout<<"正确的从大到小的排序:"<<arr[i]<<endl;
  28.         }

  29. }
复制代码
SWAP函数第二种实现及调用方法:
  1. void swap(int *a, int *b)
  2. {int tem = *a;
  3. *a = *b
  4. *b = tem;
  5. }
  6. 调用时是:swap(&arr[j],&arr[j+1])
复制代码
下面是对我这个程序的反汇编代码
  1. 0041E450  push    ebp                             ;从这里开始就是main()函数的开始
  2. 0041E451  mov     ebp, esp
  3. 0041E453  sub     esp, 108
  4. 0041E459  push    ebx
  5. 0041E45A  push    esi
  6. 0041E45B  push    edi
  7. 0041E45C  lea     edi, dword ptr [ebp-108]
  8. 0041E462  mov     ecx, 42
  9. 0041E467  mov     eax, CCCCCCCC
  10. 0041E46C  rep     stos dword ptr es:[edi]
  11. 0041E46E  mov     dword ptr [ebp-8], 0
  12. 0041E475  jmp     short 0041E480
  13. 0041E477  mov     eax, dword ptr [ebp-8]                              ;eax就相当与i ,从这里开始循环
  14. 0041E47A  add     eax, 1                                                      ;i++
  15. 0041E47D  mov     dword ptr [ebp-8], eax                              ;把i保存了
  16. 0041E480  [color=Red]cmp     dword ptr [ebp-8], 0A[/color]   ;i与10比较
  17. 0041E484  jge     short 0041E4AC                                        ;i大于10就跳
  18. 0041E486  push    004570E4                                               ;  请输入10个整数:
  19. 0041E48B  push    004607D0            
  20. 0041E490  call    0041CB6D                                                  ;显示“请输入10个整数:”
  21. 0041E495  add     esp, 8
  22. 0041E498  mov     eax, dword ptr [ebp-8]
  23. 0041E49B  lea     ecx, dword ptr [ebp+eax*4-44]
  24. 0041E49F  push    ecx
  25. 0041E4A0  mov     ecx, 00460710
  26. 0041E4A5  call    0041CAAA                                                 ;执行输入命令,相当与C++中的 cin<<arr[i]
  27. 0041E4AA  jmp     short 0041E477                                        ;跳回去循环


  28. 0041E4AC  mov     dword ptr [ebp-8], 0                                 ;对i进行初始化
  29. 0041E4B3  jmp     short 0041E4BE
  30. 0041E4B5  mov     eax, dword ptr [ebp-8]                                
  31. 0041E4B8  add     eax, 1                                                         ;i++
  32. 0041E4BB  mov     dword ptr [ebp-8], eax
  33. 0041E4BE  cmp     dword ptr [ebp-8], 0A                                ;i与10比较
  34. 0041E4C2  jge     short 0041E512                                           ;大于就跳
  35. 0041E4C4  mov     dword ptr [ebp-14], 0                                 ;相当与对j初始化,j=0
  36. 0041E4CB  jmp     short 0041E4D6
  37. 0041E4CD  mov     eax, dword ptr [ebp-14]                              
  38. 0041E4D0  add     eax, 1                                                        ;j++
  39. 0041E4D3  mov     dword ptr [ebp-14], eax                              
  40. 0041E4D6  mov     eax, 0A                                                     ;从这里开始的三条指令其实就是  10-i-1                 
  41. 0041E4DB  sub     eax, dword ptr [ebp-8]
  42. 0041E4DE  sub     eax, 1
  43. 0041E4E1  cmp     dword ptr [ebp-14], eax                               ;这里就是j<10-i-1
  44. 0041E4E4  jge     short 0041E510                                            ;大于就跳
  45. 0041E4E6  mov     eax, dword ptr [ebp-14]
  46. 0041E4E9  mov     ecx, dword ptr [ebp-14]
  47. 0041E4EC  mov     edx, dword ptr [ebp+eax*4-44]
  48. 0041E4F0  cmp     edx, dword ptr [ebp+ecx*4-40]                     ;这里其实就是 if(arr[j]<arr[j+1])      
  49. 0041E4F4  jge     short 0041E50E                                             ;如果大于那么就不执行SWAP函数
  50. 0041E4F6  mov     eax, dword ptr [ebp-14]
  51. 0041E4F9  lea     ecx, dword ptr [ebp+eax*4-40]
  52. 0041E4FD  push    ecx                                                             ;压入参数a
  53. 0041E4FE  mov     edx, dword ptr [ebp-14]
  54. 0041E501  lea     eax, dword ptr [ebp+edx*4-44]
  55. 0041E505  push    eax                                                              ;压入参数b
  56. 0041E506  call    0041D20C                                                      ;这里就是SWAP互换函数
  57. 0041E50B  add     esp, 8
  58. 0041E50E  jmp     short 0041E4CD                                             ;返回进行for(j=0;j<10-i-1;j++) 循环
  59. 0041E510  jmp     short 0041E4B5                                             ;返回进行for(i=0;i<10;i++) 循环


  60. 0041E512  mov     dword ptr [ebp-8], 0                                      ;好了这里就是对最后一个循环的 i 初始化
  61. 0041E519  jmp     short 0041E524
  62. 0041E51B  mov     eax, dword ptr [ebp-8]
  63. 0041E51E  add     eax, 1
  64. 0041E521  mov     dword ptr [ebp-8], eax
  65. 0041E524  cmp     dword ptr [ebp-8], 0A                                     ;比较 i<10
  66. 0041E528  jge     short 0041E559                                                ;大于就跳
  67. 0041E52A  push    0041C505                                                       ;这个参数就是endl
  68. 0041E52F  mov     eax, dword ptr [ebp-8]
  69. 0041E532  mov     ecx, dword ptr [ebp+eax*4-44]
  70. 0041E536  push    ecx
  71. 0041E537  push    004570C8                                                      ;  字符传“正确的从大到小的排序:”
  72. 0041E53C  push    004607D0
  73. 0041E541  call    0041CB6D                                                        ;这里还是显示字符串
  74. 0041E546  add     esp, 8
  75. 0041E549  mov     ecx, eax
  76. 0041E54B  call    0041C631                                                        ;显示数据arr[i]
  77. 0041E550  mov     ecx, eax
  78. 0041E552  call    0041CB9A                                                        ;将光标换行
  79. 0041E557  jmp     short 0041E51B                                               ;返回继续循环
  80. 0041E559  xor     eax, eax
  81. 0041E55B  push    edx
  82. 0041E55C  mov     ecx, ebp
  83. 0041E55E  push    eax
  84. 0041E55F  lea     edx, dword ptr [41E580]
  85. 0041E565  call    0041C5AF
  86. 0041E56A  pop     eax
  87. 0041E56B  pop     edx
  88. 0041E56C  pop     edi
  89. 0041E56D  pop     esi
  90. 0041E56E  pop     ebx
  91. 0041E56F  add     esp, 108
  92. 0041E575  cmp     ebp, esp
  93. 0041E577  call    0041CD7A
  94. 0041E57C  mov     esp, ebp
  95. 0041E57E  pop     ebp
  96. 0041E57F  retn
复制代码

  1. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  2. ; Sample code for < Win32ASM Programming >
  3. ; by
  4. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  5. ; Sorts.asm
  6. ; 排序算法
  7. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  8. ; 使用 nmake 或下列命令进行编译和链接:
  9. ; ml /c /coff Sorts.asm
  10. ; rc Sorts.rc
  11. ; Link /subsystem:windows Sorts.obj Sort.res
  12. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  13.                 .386
  14.                 .model flat, stdcall
  15.                 option casemap :none
  16. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  17. ; Include 文件定义
  18. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  19. include                windows.inc
  20. include                user32.inc
  21. includelib        user32.lib
  22. include                kernel32.inc
  23. includelib        kernel32.lib
  24. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  25. ; 数据段
  26. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  27.                 .data
  28. szStr                        byte    255 dup (0)   
  29. szBuffer        byte    255 dup (0)
  30. Data                        dd      16,35,78,19,-10,23,56,-90 ,80,-7,5, 99, 10 ,12,1,13,18,17
  31. DLen            dd      ($ - Data) / 4     
  32.                 .const
  33. fmt         db      "%s %d", 0
  34. fmt2        db      "%s  排序后:", 0
  35. szCaption   db      "排序算法", 0
  36. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  37. ; 代码段
  38. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  39.                 .code

  40. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  41. ; 冒泡排序
  42. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  43. BubbleSort proc uses ecx  esi  ebx  pData, Num
  44.                   
  45.                    mov eax, Num
  46.                    dec eax
  47.                    lea eax, [eax*4]
  48.                    add eax, pData
  49.                    mov ecx, eax          
  50.                   
  51. L_LOOP1:   mov esi, pData
  52.                   
  53. L_LOOP2:   mov eax, [esi+4]
  54.                   
  55.                    ;比较两个数
  56.                    cmp eax, [esi]
  57.                    jg  NOT_SWAP
  58.                   
  59.                    ;交换两个数
  60.                    mov ebx, [esi]
  61.                    mov [esi], eax
  62.                    mov [esi+4], ebx
  63.                   
  64. NOT_SWAP:  add esi,4
  65.                    cmp esi, ecx
  66.                    jb L_LOOP2   ;地址比较为无符号比较
  67.                   
  68.                   
  69.                    sub ecx,4
  70.                    cmp ecx, pData
  71.                    ja L_LOOP1     ;地址比较为无符号比较
  72.                   
  73.                    ret
  74. BubbleSort endp

  75. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  76. ; 插入排序
  77. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  78. InSertSort proc uses ecx  esi  edi  ebx  pData, Num
  79.                   
  80.                    mov ecx, 1
  81.                   
  82.                    ;esi 为将要插入数据的地址
  83. L_LOOP:           mov esi, pData
  84.                    lea esi, [esi+ecx*4]
  85.                   
  86.                    ;edi为在已排好序部分中,最后一个数的地址
  87.                    mov  edi, esi
  88.                    sub  edi, 4
  89.                   
  90.                    mov eax, [esi]
  91. L_FIND_POS:   cmp eax, [edi]
  92.                    jge  L_NEXT      
  93.                   
  94.                    ;在已排好序部分中,找到要插入数的位置
  95.                    mov ebx, [edi]
  96.                    mov [edi+4], ebx
  97.                    sub edi, 4
  98.                    cmp edi, pData
  99.                    jae  L_FIND_POS    ;地址比较为无符号比较
  100.                   
  101. L_NEXT:           mov [edi+4], eax   ;插入到找到的位置。
  102.                    inc ecx
  103.                    cmp  ecx, Num
  104.                    jl   L_LOOP               
  105.                   
  106.                    ret
  107. InSertSort endp

  108. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  109. ; Shell 排序
  110. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  111. ShelltSort proc uses ecx  esi  edi  ebx edx  pData, Num
  112.                     local @key:DWORD
  113.                     
  114.                    mov edx, Num
  115.                   
  116. L_START:   sar edx, 1
  117.                    je  SHELL_END
  118.                    mov  ecx, edx
  119.                   
  120.                    ;esi 为将要插入数据的地址
  121. L_LOOP:           mov esi, pData
  122.                    lea esi, [esi+ecx*4]
  123.                   
  124.                    ;edi为在已排好序部分中,最后一个数的地址
  125.                    mov  edi, esi
  126.                    lea ebx, [edx*4]
  127.                    sub edi, ebx
  128.                   
  129.                    mov eax, [esi]
  130. L_FIND_POS: cmp eax, [edi]
  131.                    jge  L_NEXT      
  132.                   
  133.                    ;在已排好序部分中,找到要插入数的位置
  134.                    mov ebx, [edi]
  135.                    mov [edi+edx*4], ebx
  136.                    lea ebx, [edx*4]
  137.                    sub edi, ebx
  138.                    cmp edi, pData
  139.                    jae  L_FIND_POS    ;地址比较为无符号比较
  140.                   
  141. L_NEXT:           mov [edi+edx*4], eax   ;插入到找到的位置。
  142.                   
  143.                    lea  ecx, [ecx + edx]
  144.                    cmp  ecx, Num
  145.                    jl   L_LOOP
  146.                                   
  147.                    jmp  L_START
  148. SHELL_END:                  
  149.                    ret
  150. ShelltSort endp


  151. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  152. ; 选择排序
  153. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  154. SelectSort proc uses ecx  esi  edi  ebx  edx  pData, Num
  155.                    local @key:DWORD
  156.                    mov eax, pData
  157.                   
  158.                    mov ecx, Num
  159.                    dec ecx
  160.                    lea ebx, [eax + ecx * 4]
  161.                   
  162.                    mov  ecx, 1
  163.                   
  164. L_LOOP:           mov esi, pData
  165.                lea esi, [esi + ecx*4 - 4]
  166.                    mov edx, esi
  167.                    mov edi, esi
  168.                   
  169. L_LOOP2:   lea edi, [edi + 4]
  170.                    mov eax, [esi]
  171.                    cmp eax, [edi]
  172.                    jle   NEXT
  173.                   
  174.                    mov esi, edi
  175.                   
  176. NEXT:           cmp edi, ebx
  177.                    jl L_LOOP2
  178.                   
  179.                    cmp esi, edx
  180.                    je NOT_SWAP
  181.                   
  182.                    ;交换[esi], [edx]
  183.                    push [esi]
  184.                    push [edx]
  185.                    pop  [esi]
  186.                    pop  [edx]
  187.                     
  188. NOT_SWAP:        inc ecx
  189.                    cmp ecx, Num
  190.                    jl L_LOOP
  191.                   
  192.                    ret
  193. SelectSort endp


  194. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  195. ; 调整堆
  196. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  197. AdjustHeap proc uses ecx  esi  edi  ebx  edx  pData, Base ,Num

  198.                         local @key:DWORD
  199.                        
  200.                         mov eax, pData
  201.                         mov ecx, Base
  202.                         lea eax, [eax + ecx*4 - 4]
  203.                         mov eax, [eax]
  204.                         mov @key, eax
  205.                        
  206. L_LOOP:                mov eax, Base
  207.                         lea eax, [eax*2]
  208.                         mov ecx, eax
  209.                        
  210.                         mov edx, pData
  211.                        
  212.                         mov ebx, Num
  213.                         cmp  ecx, ebx
  214.                         jg    ADJUST_END
  215.                        
  216.                         cmp ecx, ebx
  217.                         jge NOT_CHANG
  218.                
  219.                         lea esi, [edx + ecx*4 - 4]
  220.                         mov eax, [esi]
  221.                         cmp  eax, [edx + ecx*4]
  222.                         jge NOT_CHANG
  223.                        
  224.                         inc ecx
  225.                        
  226. NOT_CHANG:        lea esi, [edx + ecx*4 - 4]
  227.                         mov eax, [esi]
  228.                         cmp @key,eax
  229.                        
  230.                         jg ADJUST_END
  231.                        
  232. NEXT:                 push eax
  233.                        
  234.                         mov eax, Base
  235.                         lea edi, [edx + eax*4 - 4]
  236.                         pop [edi]
  237.                        
  238.                         mov eax, ecx
  239.                         mov Base, eax
  240.                        
  241.                         lea ecx, [ecx*2]
  242.                        
  243.                         jmp L_LOOP
  244.                                
  245. ADJUST_END: mov eax, Base
  246.                         lea edi, [edx + eax*4 - 4]
  247.                         mov eax, @key
  248.                         mov [edi], eax
  249.                        
  250.                         ret
  251. AdjustHeap endp

  252. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  253. ; 堆排序
  254. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  255. HeapSort proc uses ecx  esi  edi  ebx  edx  pData, Num
  256.                
  257.                 mov ecx, Num
  258.                 sar ecx, 1
  259.                
  260. S_ADJUST:cmp ecx, 0
  261.                 jle NEXT
  262.                 invoke AdjustHeap, pData, ecx, Num
  263.                 dec ecx
  264.                 jmp  S_ADJUST
  265.                
  266. NEXT:        xor ebx, ebx
  267. S_CMP:        cmp  ebx, Num
  268.                 jge L_END
  269.                
  270.                 mov ecx, Num
  271.                 sub ecx, ebx
  272.                 dec ecx
  273.                
  274.                 mov edx, pData
  275.                 lea  esi, [edx + ecx*4]
  276.                 push [esi]
  277.                
  278.                 mov  edi,edx
  279.                 push [edi]
  280.                
  281.                 pop [esi]
  282.                 pop [edi]
  283.                
  284.                 inc ebx
  285.                
  286.                 invoke AdjustHeap, pData, 1, ecx
  287.                
  288.                 jmp S_CMP
  289.                
  290. L_END:                  
  291.                 ret
  292. HeapSort endp


  293. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  294. ; 快速排序中调用划分区域函数
  295. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  296. Partition proc uses ecx  esi  edi  edx pData, iLow, iHigh
  297.                         local @key:DWORD
  298.                        
  299.                         mov ecx, iLow
  300.                         mov edx, iHigh
  301.                         mov eax, pData
  302.                        
  303.                         mov  eax, [eax + ecx*4]
  304.                         mov  @key, eax
  305. L_START:        cmp  ecx, edx
  306.                         jge PART_END
  307.                        
  308. L_CMPHIGH:         cmp ecx, edx
  309.                         jge NEXT
  310.                        
  311.                         mov eax, pData
  312.                         lea esi, [eax + edx*4]
  313.                         mov eax, [esi]
  314.                        
  315.                         cmp eax, @key
  316.                         jl NEXT
  317.                         dec edx
  318.                         jmp L_CMPHIGH
  319.                        
  320. NEXT:                mov eax, pData
  321.                         lea edi, [eax + edx*4]
  322.                         lea esi, [eax + ecx*4]
  323.                        
  324.                         push [edi]
  325.                         pop  [esi]               
  326.                        
  327. S_CMPLOW:         cmp ecx, edx
  328.                         jge NEXTLOW
  329.                        
  330.                         mov eax, pData
  331.                         lea esi, [eax + ecx*4]
  332.                         mov eax, [esi]
  333.                        
  334.                         cmp eax, @key
  335.                         jg NEXTLOW
  336.                         inc ecx
  337.                         jmp S_CMPLOW
  338.                        
  339. NEXTLOW:        mov eax, pData
  340.                         lea edi, [eax + edx*4]
  341.                         lea esi, [eax + ecx*4]
  342.                        
  343.                         push [esi]
  344.                         pop  [edi]                               
  345.                        
  346.                         jmp L_START
  347.                        
  348. PART_END:        mov eax, pData
  349.                         lea esi, [eax + ecx*4]
  350.                         push @key
  351.                         pop [esi]
  352.                        
  353.                         mov eax, ecx
  354.                        
  355.                         ret
  356. Partition endp

  357. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  358. ; 快速排序
  359. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  360. QuickSort proc uses eax ecx  ebx  edx  pData, iLow, iHigh
  361.                        
  362.                         mov ebx, iLow
  363.                         mov edx, iHigh
  364.                        
  365.                         cmp ebx, edx
  366.                         jge QUICK_END
  367.                        
  368.                         invoke Partition, pData, ebx, edx
  369.                        
  370.                         dec eax
  371.                         invoke  QuickSort, pData, ebx, eax
  372.                        
  373.                         lea eax, [eax + 2]
  374.                         invoke  QuickSort, pData, eax, edx
  375.                        
  376. QUICK_END:  
  377.                         ret
  378.                        
  379. QuickSort endp
  380. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


  381. start:                                       
  382.                         mov edi, 0
  383.                        
  384. L_ORIG:                mov eax, [Data + edi*4]
  385.                         invoke wsprintf, addr szBuffer, addr fmt, addr szBuffer, eax
  386.                         inc edi
  387.                         cmp  edi, DLen
  388.                         jb   L_ORIG
  389.                        
  390.                         invoke wsprintf, addr szBuffer, addr fmt2, addr szBuffer
  391.                        
  392.                         mov eax, DLen
  393.                         dec eax
  394.                         invoke ShelltSort, addr Data, DLen
  395.                        
  396.                         mov edi, 0
  397.                        
  398. L_SORT:                mov eax, [Data + edi*4]
  399.                         invoke wsprintf, addr szBuffer, addr fmt, addr szBuffer, eax
  400.                         inc edi
  401.                         cmp  edi, DLen
  402.                         jb   L_SORT
  403.                        
  404.                         invoke        MessageBox,NULL,offset szBuffer,offset szCaption, MB_OK
  405.                         invoke        ExitProcess,NULL
  406. ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  407.                 end        start

复制代码

//java 的
import java.awt.*;
import javax.swing.*;
class Bb
{
        public static void main(String []args)
        {
                String b[]=new String [10];
                int a[]=new int [10];
        for(int i=0;i<10;i++)  //输入数据
        {
                b=JOptionPane.showInputDialog("","请输入一个数");
                  try  
                {
                                 a=Integer.parseInt(b);//类型转换
                                 System.out.println("您的输入为"+a);
                                }
                                 catch(Exception e) //异常处理
                                {
                                 System.out.println("输入的数据类型不对,请重新输入");
                                 i--;
                                 continue;//重新输入
                                }
              }
            for(int j=0;j<9;j++)//排序
           {
               for(int i=0;i<10;i++)
               {
              int temp;
              if(a<a[j+1])
              {
                      temp=a;
                      a=a[j+1];
                      a[j+1]=temp;
              }
               }
            }
        System.out.println("下面是输出的结果");
        for(int i=0;i<10;i++)
        {
                System.out.println(a);
        }
          }
  }


[ 本帖最后由 freesoft 于 2007-8-13 08:21 编辑 ]
PYG19周年生日快乐!

该用户从未签到

发表于 2007-8-13 17:45:17 | 显示全部楼层
我最烦的就是用数组和指针了,但是不用这两个,就等于不会编程/:002
PYG19周年生日快乐!
  • TA的每日心情
    奋斗
    6 天前
  • 签到天数: 432 天

    [LV.9]以坛为家II

    发表于 2007-8-13 20:46:53 | 显示全部楼层
    非常的支持系列产品呀,建议楼主整理成CHM格式,方便下载并查询

    [ 本帖最后由 wofan 于 2007-8-13 20:51 编辑 ]
    PYG19周年生日快乐!

    该用户从未签到

     楼主| 发表于 2007-8-14 07:47:52 | 显示全部楼层
    到一定程序在做成CHM,这点是刚开始,还不到做CHM的程序。初级的东西弄的差不多了,进入中级时就做一个。
    PYG19周年生日快乐!
    您需要登录后才可以回帖 登录 | 加入我们

    本版积分规则

    快速回复 返回顶部 返回列表