- UID
- 32468
注册时间2007-6-1
阅读权限10
最后登录1970-1-1
周游历练
该用户从未签到
|
本贴引自第八阁论坛:http://bbs.chinadbg.cn/forum-7-1.html
还有飞翔技术论坛:http://www.powk.net/bbs/forumdisplay.php?fid=4&page=1
从键盘输入10个数,排序,请高手们写出高级语言+汇编+反汇编,代码
还有要注意,我写程序的错误,因为这才是真正要让大家学习的地方。
非常高兴看到大家的支持,你们发的程序,请在第二天加上注释。(主要是有错的程序)
基础知识:变量初始化,数组应用,条件判断,循环,和打印程序,还有基础的语法,还有一个任务找错!!。我写的是C代码,其它代码各个朋友自己理解一下,也可以PM发贴人。也可以发提问
以下是C语言,常用排序方法。注意一点是可以有方法提高效率的。
- main()
- {
- int i,j,a[10],b;
- for(i=0;i<10;i++)/*从键盘输入10个数*/
- scanf("%d",&a[i]) ;
- /*以下冒泡排序*/
- /*冒泡排序的基本思想:对于n个数进行排序(现假定是从大到小排序,以下均按此进行),将相邻两个数依次比较,将大数调在前头:也就是说第一个数和第二个数比较,大数放前,小数放后,第二个和第三个进行比较,大数放前、小数放后,然后依次类推。。。经过第一轮比较以后,我们找到一个最小数在最下面(沉底)。然后进行下一轮比较,最后一个数就不用再参加比较了,所以本轮就可以少比较一次。*/
- for(i=0;i<10;i++)
- for(j=0;j<10-i;j++)
- if(a[j]<a[j+1])
- {
- b=a[j];a[j]=a[j+1];a[j+1]=b;
- }
- for(i=0;i<10;i++)
- printf("%d\n",a[i]) ;
- getch();
- /*以下选择排序*/
- /*排序的基本思想:先从第一个数开始起,用第一个数和其它的数进行比较,如果比第一个数大就交换位置,否则不进行交换,这样经过第一轮比较我们就能够找出最大值放在第一位置,然后从第二个位置起再找次大数,这样依次下去,就可以进行整个数的排序,实践证明,n个数最多需要n-1轮排序就可以了。*/
- /*以下插入排序*/
- /*插入排序基本思想:(假定从大到小排序)依次从后面拿一个数和前面已经排好序的数进行比较,比较的过程是从已经排好序的数中最后一个数开始比较,如果比这个数小,继续往前面比较,直到找到比它大的数,然后就放在它的后面,如果一直没有找到,肯定这个数已经比较到了第一个数,那就放到第一个数的前面。*/
- }
复制代码
- void Display(int a[], int n);
- void InsertSort(int a[], int n);
- void ShellSort(int a[], int n);
- void BubbleSort(int a[], int n);
- void QuickSort(int a[], int low, int high);
- void SelectSort(int a[], int n);
- void HeapSort(int a[], int n);
- int main(int argc, char* argv[])
- {
- int a[20] ={83,78,12,45,189,98,54,10,17,18,
- 70,65,27,199,69,73,92,39,46,75,};
- Display(a,20);
- HeapSort(a,20);
- Display(a,20);
- int i = 0;
- scanf("%d", &i);
- return 0;
- }
- void Display(int a[], int n)
- {
- for (int i=0; i<n; i++)
- {
- printf("%d ", a[i]);
- }
- printf("\n");
- }
- //插入排序
- void InsertSort(int a[], int n)
- {
- int key;
- int j;
- for (int i=1; i<n; i++)
- {
- key = a[i];
- j = i-1;
- while (j>=0 && key<a[j])
- {
- a[j+1] = a[j];
- j--;
- }
- a[j+1] = key;
- }
- }
- //Shell排序
- void ShellSort(int a[], int n)
- {
- int i,j,key;
- int dl = n/2;
-
- while (dl >0)
- {
- for (i=dl; i<n; i+=dl)
- {
- key = a[i];
- j = i - dl;
- while (j>=0 && key<a[j])
- {
- a[j+dl] = a[j];
- j = j - dl;
- }
- a[j+dl] = key;
- }
- dl = dl/2;
- }
- }
- //冒泡排序
- void BubbleSort(int a[], int n)
- {
- int tem, i, j, flag =1;
- for (i=1; i<n && flag; i++)
- {
- flag = 0;
- for (j=0; j<n-i; j++)
- {
- if (a[j]>a[j+1])
- {
- tem = a[j];
- a[j] = a[j+1];
- a[j+1] = tem;
- flag = 1;
- }
- }
- }
- }
- //快速排序中调用划分区域函数
- int Partition(int a[], int low, int high)
- {
- int key = a[low];
-
- while (low < high)
- {
- while (low <high && a[high]>=key) high --;
- a[low] = a[high];
- while (low <high && a[low]<=key) low ++;
- a[high] = a[low];
- }
- a[low] = key;
- return low;
- }
- //快速排序,递归函数,low为数组下界,high为数组上界
- void QuickSort(int a[], int low, int high)
- {
- int par;
-
- if (low < high)
- {
- par = Partition(a, low, high);
- QuickSort(a, low, par-1);
- QuickSort(a, par+1, high);
- }
- }
- //选择排序
- void SelectSort(int a[], int n)
- {
- int i,j,k,tem;
- for (i=0; i<n-1; i++)
- {
- k = i;
- for (j=i+1; j<n; j++)
- {
- if (a[k] > a[j])
- {
- k=j;
- }
- }
- if (k!=i)
- {
- tem = a[i];
- a[i] = a[k];
- a[k] = tem;
- }
- }
- }
- //堆排序中调用函数,从s这个结点开始调整堆为一个大堆
- void AdjustHeap(int a[], int s, int n)
- {
- int i;
- int key = a[s-1];
- for (i=s*2; i<=n; i=i*2)
- {
-
- if (i<n && a[i-1] < a[i])
- {
- i++;
- }
- if (key> a[i-1])
- {
- break;
- }
-
- a[s-1] = a[i-1];
- s = i;
- }
- a[s-1] = key;
- }
- //堆排序
- void HeapSort(int a[], int n)
- {
- int i,j,tem;
- for (i=n/2; i>0; i--)
- {
- AdjustHeap(a, i, n);
- }
- for (j=0; j<n; j++)
- {
- tem = a[n-j-1];
- a[n-j-1] = a[0];
- a[0] = tem;
- AdjustHeap(a, 1, n-j-1);
- }
- }
复制代码
- procedure TForm1.Button1Click(Sender: TObject);
- var
- InputString,ResultString:String;
- TmpInt,IntCount,ErrorType,i,j:Integer;
- IntNum:array[0..100] of Integer;
- begin
- ResultString:='';
- ErrorType:=0;
- InputString:= InputBox('输入窗口', '输入10个整数请用空格隔开', '1 2 3 4 5 6 7 8 9 0');
- InputString:=Trim(InputString)+' ';
- while pos(' ',InputString)<>0 do Delete(InputString,pos(' ',InputString),1);//清除可能出现的多空格分隔符
- If trim(InputString)='' then ErrorType:=1;//判断输入字符串的合法性
- for i := 1 to Length(InputString) do
- begin
- if not (InputString[i] in ['0'..'9',#$20]) then ErrorType:=1;
- end;
- if ErrorType<>1 then //数据无异常开始
- begin
- IntCount:=0;
- while Length(InputString)>0 do //开始填入数组
- begin
- IntNum[IntCount]:=StrToInt(LeftStr(InputString,pos(' ',InputString)-1)); //leftStr 从左取得分隔符之前的数字
- Delete(InputString,1,pos(' ',InputString));
- inc(IntCount);
- end;
-
- //开始冒泡
- for I := 0 to IntCount-1 do
- begin
- for j := i to IntCount-1 do
- begin
- if IntNum[i]<IntNum[j] then
- begin
- TmpInt:=IntNum[i];
- IntNum[i]:=IntNum[j];
- IntNum[j]:=TmpInt;
- end;
- end;
- end;
- //泡泡冒完
- for I := 0 to IntCount - 1 do
- ResultString:=ResultString+' '+IntToStr(IntNum[i]);
- Showmessage('共输入了'+IntToStr(IntCount)+'数字。排序:'+ResultString);
- end
- else ShowMessage('输入中含非数字字符或没有输入数据!'); //数据无异常结束
- end;
复制代码
我的第一个C++程序
第一种方法,主要是函数不同,以及调用方式的不同- //C++版冒泡排序程序
- #include<iOStream>
- using namespace std;
- void swap(int &a,int &b)
- {
- int tem = a;
- a = b;
- b = tem;
- }
- void main()
- {
- int i,j,arr[10];
- for(i=0;i<10;i++)
- {
- cout<<"请输入10个整数:";
- cin>>arr[i];
- }
- for(i=0;i<10;i++) //数组数据的循环比较
- { for(j=0;j<10-i-1;j++) //每一轮的比较
- { if(arr[j]<arr[j+1]) //比较相邻两个数大小
- { swap(arr[j],arr[j+1]); //互换函数的调用
-
- }
- }
- }
- for(i=0;i<10;i++)
- { cout<<"正确的从大到小的排序:"<<arr[i]<<endl;
- }
- }
复制代码 SWAP函数第二种实现及调用方法:- void swap(int *a, int *b)
- {int tem = *a;
- *a = *b
- *b = tem;
- }
- 调用时是:swap(&arr[j],&arr[j+1])
复制代码 下面是对我这个程序的反汇编代码- 0041E450 push ebp ;从这里开始就是main()函数的开始
- 0041E451 mov ebp, esp
- 0041E453 sub esp, 108
- 0041E459 push ebx
- 0041E45A push esi
- 0041E45B push edi
- 0041E45C lea edi, dword ptr [ebp-108]
- 0041E462 mov ecx, 42
- 0041E467 mov eax, CCCCCCCC
- 0041E46C rep stos dword ptr es:[edi]
- 0041E46E mov dword ptr [ebp-8], 0
- 0041E475 jmp short 0041E480
- 0041E477 mov eax, dword ptr [ebp-8] ;eax就相当与i ,从这里开始循环
- 0041E47A add eax, 1 ;i++
- 0041E47D mov dword ptr [ebp-8], eax ;把i保存了
- 0041E480 [color=Red]cmp dword ptr [ebp-8], 0A[/color] ;i与10比较
- 0041E484 jge short 0041E4AC ;i大于10就跳
- 0041E486 push 004570E4 ; 请输入10个整数:
- 0041E48B push 004607D0
- 0041E490 call 0041CB6D ;显示“请输入10个整数:”
- 0041E495 add esp, 8
- 0041E498 mov eax, dword ptr [ebp-8]
- 0041E49B lea ecx, dword ptr [ebp+eax*4-44]
- 0041E49F push ecx
- 0041E4A0 mov ecx, 00460710
- 0041E4A5 call 0041CAAA ;执行输入命令,相当与C++中的 cin<<arr[i]
- 0041E4AA jmp short 0041E477 ;跳回去循环
- 0041E4AC mov dword ptr [ebp-8], 0 ;对i进行初始化
- 0041E4B3 jmp short 0041E4BE
- 0041E4B5 mov eax, dword ptr [ebp-8]
- 0041E4B8 add eax, 1 ;i++
- 0041E4BB mov dword ptr [ebp-8], eax
- 0041E4BE cmp dword ptr [ebp-8], 0A ;i与10比较
- 0041E4C2 jge short 0041E512 ;大于就跳
- 0041E4C4 mov dword ptr [ebp-14], 0 ;相当与对j初始化,j=0
- 0041E4CB jmp short 0041E4D6
- 0041E4CD mov eax, dword ptr [ebp-14]
- 0041E4D0 add eax, 1 ;j++
- 0041E4D3 mov dword ptr [ebp-14], eax
- 0041E4D6 mov eax, 0A ;从这里开始的三条指令其实就是 10-i-1
- 0041E4DB sub eax, dword ptr [ebp-8]
- 0041E4DE sub eax, 1
- 0041E4E1 cmp dword ptr [ebp-14], eax ;这里就是j<10-i-1
- 0041E4E4 jge short 0041E510 ;大于就跳
- 0041E4E6 mov eax, dword ptr [ebp-14]
- 0041E4E9 mov ecx, dword ptr [ebp-14]
- 0041E4EC mov edx, dword ptr [ebp+eax*4-44]
- 0041E4F0 cmp edx, dword ptr [ebp+ecx*4-40] ;这里其实就是 if(arr[j]<arr[j+1])
- 0041E4F4 jge short 0041E50E ;如果大于那么就不执行SWAP函数
- 0041E4F6 mov eax, dword ptr [ebp-14]
- 0041E4F9 lea ecx, dword ptr [ebp+eax*4-40]
- 0041E4FD push ecx ;压入参数a
- 0041E4FE mov edx, dword ptr [ebp-14]
- 0041E501 lea eax, dword ptr [ebp+edx*4-44]
- 0041E505 push eax ;压入参数b
- 0041E506 call 0041D20C ;这里就是SWAP互换函数
- 0041E50B add esp, 8
- 0041E50E jmp short 0041E4CD ;返回进行for(j=0;j<10-i-1;j++) 循环
- 0041E510 jmp short 0041E4B5 ;返回进行for(i=0;i<10;i++) 循环
- 0041E512 mov dword ptr [ebp-8], 0 ;好了这里就是对最后一个循环的 i 初始化
- 0041E519 jmp short 0041E524
- 0041E51B mov eax, dword ptr [ebp-8]
- 0041E51E add eax, 1
- 0041E521 mov dword ptr [ebp-8], eax
- 0041E524 cmp dword ptr [ebp-8], 0A ;比较 i<10
- 0041E528 jge short 0041E559 ;大于就跳
- 0041E52A push 0041C505 ;这个参数就是endl
- 0041E52F mov eax, dword ptr [ebp-8]
- 0041E532 mov ecx, dword ptr [ebp+eax*4-44]
- 0041E536 push ecx
- 0041E537 push 004570C8 ; 字符传“正确的从大到小的排序:”
- 0041E53C push 004607D0
- 0041E541 call 0041CB6D ;这里还是显示字符串
- 0041E546 add esp, 8
- 0041E549 mov ecx, eax
- 0041E54B call 0041C631 ;显示数据arr[i]
- 0041E550 mov ecx, eax
- 0041E552 call 0041CB9A ;将光标换行
- 0041E557 jmp short 0041E51B ;返回继续循环
- 0041E559 xor eax, eax
- 0041E55B push edx
- 0041E55C mov ecx, ebp
- 0041E55E push eax
- 0041E55F lea edx, dword ptr [41E580]
- 0041E565 call 0041C5AF
- 0041E56A pop eax
- 0041E56B pop edx
- 0041E56C pop edi
- 0041E56D pop esi
- 0041E56E pop ebx
- 0041E56F add esp, 108
- 0041E575 cmp ebp, esp
- 0041E577 call 0041CD7A
- 0041E57C mov esp, ebp
- 0041E57E pop ebp
- 0041E57F retn
复制代码
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- ; Sample code for < Win32ASM Programming >
- ; by
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- ; Sorts.asm
- ; 排序算法
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- ; 使用 nmake 或下列命令进行编译和链接:
- ; ml /c /coff Sorts.asm
- ; rc Sorts.rc
- ; Link /subsystem:windows Sorts.obj Sort.res
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- .386
- .model flat, stdcall
- option casemap :none
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- ; Include 文件定义
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- include windows.inc
- include user32.inc
- includelib user32.lib
- include kernel32.inc
- includelib kernel32.lib
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- ; 数据段
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- .data
- szStr byte 255 dup (0)
- szBuffer byte 255 dup (0)
- Data dd 16,35,78,19,-10,23,56,-90 ,80,-7,5, 99, 10 ,12,1,13,18,17
- DLen dd ($ - Data) / 4
- .const
- fmt db "%s %d", 0
- fmt2 db "%s 排序后:", 0
- szCaption db "排序算法", 0
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- ; 代码段
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- .code
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- ; 冒泡排序
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- BubbleSort proc uses ecx esi ebx pData, Num
-
- mov eax, Num
- dec eax
- lea eax, [eax*4]
- add eax, pData
- mov ecx, eax
-
- L_LOOP1: mov esi, pData
-
- L_LOOP2: mov eax, [esi+4]
-
- ;比较两个数
- cmp eax, [esi]
- jg NOT_SWAP
-
- ;交换两个数
- mov ebx, [esi]
- mov [esi], eax
- mov [esi+4], ebx
-
- NOT_SWAP: add esi,4
- cmp esi, ecx
- jb L_LOOP2 ;地址比较为无符号比较
-
-
- sub ecx,4
- cmp ecx, pData
- ja L_LOOP1 ;地址比较为无符号比较
-
- ret
- BubbleSort endp
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- ; 插入排序
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- InSertSort proc uses ecx esi edi ebx pData, Num
-
- mov ecx, 1
-
- ;esi 为将要插入数据的地址
- L_LOOP: mov esi, pData
- lea esi, [esi+ecx*4]
-
- ;edi为在已排好序部分中,最后一个数的地址
- mov edi, esi
- sub edi, 4
-
- mov eax, [esi]
- L_FIND_POS: cmp eax, [edi]
- jge L_NEXT
-
- ;在已排好序部分中,找到要插入数的位置
- mov ebx, [edi]
- mov [edi+4], ebx
- sub edi, 4
- cmp edi, pData
- jae L_FIND_POS ;地址比较为无符号比较
-
- L_NEXT: mov [edi+4], eax ;插入到找到的位置。
- inc ecx
- cmp ecx, Num
- jl L_LOOP
-
- ret
- InSertSort endp
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- ; Shell 排序
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- ShelltSort proc uses ecx esi edi ebx edx pData, Num
- local @key:DWORD
-
- mov edx, Num
-
- L_START: sar edx, 1
- je SHELL_END
- mov ecx, edx
-
- ;esi 为将要插入数据的地址
- L_LOOP: mov esi, pData
- lea esi, [esi+ecx*4]
-
- ;edi为在已排好序部分中,最后一个数的地址
- mov edi, esi
- lea ebx, [edx*4]
- sub edi, ebx
-
- mov eax, [esi]
- L_FIND_POS: cmp eax, [edi]
- jge L_NEXT
-
- ;在已排好序部分中,找到要插入数的位置
- mov ebx, [edi]
- mov [edi+edx*4], ebx
- lea ebx, [edx*4]
- sub edi, ebx
- cmp edi, pData
- jae L_FIND_POS ;地址比较为无符号比较
-
- L_NEXT: mov [edi+edx*4], eax ;插入到找到的位置。
-
- lea ecx, [ecx + edx]
- cmp ecx, Num
- jl L_LOOP
-
- jmp L_START
- SHELL_END:
- ret
- ShelltSort endp
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- ; 选择排序
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- SelectSort proc uses ecx esi edi ebx edx pData, Num
- local @key:DWORD
- mov eax, pData
-
- mov ecx, Num
- dec ecx
- lea ebx, [eax + ecx * 4]
-
- mov ecx, 1
-
- L_LOOP: mov esi, pData
- lea esi, [esi + ecx*4 - 4]
- mov edx, esi
- mov edi, esi
-
- L_LOOP2: lea edi, [edi + 4]
- mov eax, [esi]
- cmp eax, [edi]
- jle NEXT
-
- mov esi, edi
-
- NEXT: cmp edi, ebx
- jl L_LOOP2
-
- cmp esi, edx
- je NOT_SWAP
-
- ;交换[esi], [edx]
- push [esi]
- push [edx]
- pop [esi]
- pop [edx]
-
- NOT_SWAP: inc ecx
- cmp ecx, Num
- jl L_LOOP
-
- ret
- SelectSort endp
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- ; 调整堆
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- AdjustHeap proc uses ecx esi edi ebx edx pData, Base ,Num
- local @key:DWORD
-
- mov eax, pData
- mov ecx, Base
- lea eax, [eax + ecx*4 - 4]
- mov eax, [eax]
- mov @key, eax
-
- L_LOOP: mov eax, Base
- lea eax, [eax*2]
- mov ecx, eax
-
- mov edx, pData
-
- mov ebx, Num
- cmp ecx, ebx
- jg ADJUST_END
-
- cmp ecx, ebx
- jge NOT_CHANG
-
- lea esi, [edx + ecx*4 - 4]
- mov eax, [esi]
- cmp eax, [edx + ecx*4]
- jge NOT_CHANG
-
- inc ecx
-
- NOT_CHANG: lea esi, [edx + ecx*4 - 4]
- mov eax, [esi]
- cmp @key,eax
-
- jg ADJUST_END
-
- NEXT: push eax
-
- mov eax, Base
- lea edi, [edx + eax*4 - 4]
- pop [edi]
-
- mov eax, ecx
- mov Base, eax
-
- lea ecx, [ecx*2]
-
- jmp L_LOOP
-
- ADJUST_END: mov eax, Base
- lea edi, [edx + eax*4 - 4]
- mov eax, @key
- mov [edi], eax
-
- ret
- AdjustHeap endp
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- ; 堆排序
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- HeapSort proc uses ecx esi edi ebx edx pData, Num
-
- mov ecx, Num
- sar ecx, 1
-
- S_ADJUST:cmp ecx, 0
- jle NEXT
- invoke AdjustHeap, pData, ecx, Num
- dec ecx
- jmp S_ADJUST
-
- NEXT: xor ebx, ebx
- S_CMP: cmp ebx, Num
- jge L_END
-
- mov ecx, Num
- sub ecx, ebx
- dec ecx
-
- mov edx, pData
- lea esi, [edx + ecx*4]
- push [esi]
-
- mov edi,edx
- push [edi]
-
- pop [esi]
- pop [edi]
-
- inc ebx
-
- invoke AdjustHeap, pData, 1, ecx
-
- jmp S_CMP
-
- L_END:
- ret
- HeapSort endp
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- ; 快速排序中调用划分区域函数
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- Partition proc uses ecx esi edi edx pData, iLow, iHigh
- local @key:DWORD
-
- mov ecx, iLow
- mov edx, iHigh
- mov eax, pData
-
- mov eax, [eax + ecx*4]
- mov @key, eax
- L_START: cmp ecx, edx
- jge PART_END
-
- L_CMPHIGH: cmp ecx, edx
- jge NEXT
-
- mov eax, pData
- lea esi, [eax + edx*4]
- mov eax, [esi]
-
- cmp eax, @key
- jl NEXT
- dec edx
- jmp L_CMPHIGH
-
- NEXT: mov eax, pData
- lea edi, [eax + edx*4]
- lea esi, [eax + ecx*4]
-
- push [edi]
- pop [esi]
-
- S_CMPLOW: cmp ecx, edx
- jge NEXTLOW
-
- mov eax, pData
- lea esi, [eax + ecx*4]
- mov eax, [esi]
-
- cmp eax, @key
- jg NEXTLOW
- inc ecx
- jmp S_CMPLOW
-
- NEXTLOW: mov eax, pData
- lea edi, [eax + edx*4]
- lea esi, [eax + ecx*4]
-
- push [esi]
- pop [edi]
-
- jmp L_START
-
- PART_END: mov eax, pData
- lea esi, [eax + ecx*4]
- push @key
- pop [esi]
-
- mov eax, ecx
-
- ret
- Partition endp
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- ; 快速排序
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- QuickSort proc uses eax ecx ebx edx pData, iLow, iHigh
-
- mov ebx, iLow
- mov edx, iHigh
-
- cmp ebx, edx
- jge QUICK_END
-
- invoke Partition, pData, ebx, edx
-
- dec eax
- invoke QuickSort, pData, ebx, eax
-
- lea eax, [eax + 2]
- invoke QuickSort, pData, eax, edx
-
- QUICK_END:
- ret
-
- QuickSort endp
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- start:
- mov edi, 0
-
- L_ORIG: mov eax, [Data + edi*4]
- invoke wsprintf, addr szBuffer, addr fmt, addr szBuffer, eax
- inc edi
- cmp edi, DLen
- jb L_ORIG
-
- invoke wsprintf, addr szBuffer, addr fmt2, addr szBuffer
-
- mov eax, DLen
- dec eax
- invoke ShelltSort, addr Data, DLen
-
- mov edi, 0
-
- L_SORT: mov eax, [Data + edi*4]
- invoke wsprintf, addr szBuffer, addr fmt, addr szBuffer, eax
- inc edi
- cmp edi, DLen
- jb L_SORT
-
- invoke MessageBox,NULL,offset szBuffer,offset szCaption, MB_OK
- invoke ExitProcess,NULL
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- 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 编辑 ] |
|