1 / 14

简单排序

简单排序. 1. 选择排序 . 选择排序的基本思想是: 对待排序的记录序列进行 n-1 遍的处理,第 1 遍处理是将 L[1..n] 中最小者与 L[1] 交换位置,第 2 遍处理是将 L[2..n] 中最小者与 L[2] 交换位置, ...... ,第 i 遍处理是将 L[i..n] 中最小者与 L[i] 交换位置。这样,经过 i 遍处理之后,前 i 个记录的位置就已经按从小到大的顺序排列好了。. 例 1: 输入序列数据按非减顺序输出 . 程序如下 :

sovann
Download Presentation

简单排序

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 简单排序

  2. 1.选择排序  选择排序的基本思想是: 对待排序的记录序列进行n-1遍的处理,第1遍处理是将L[1..n]中最小者与L[1]交换位置,第2遍处理是将L[2..n]中最小者与L[2]交换位置,......,第i遍处理是将L[i..n]中最小者与L[i]交换位置。这样,经过i遍处理之后,前i个记录的位置就已经按从小到大的顺序排列好了。

  3. 例1:输入序列数据按非减顺序输出. 程序如下:   program xzpx;const n=7;var a:array[1..n] of integer;    i,j,k,t:integer;begin write('Enter date:'); for i:= 1 to n do read(a[i]); writeln; for i:=1 to n-1 do  begin   k:=i;   for j:=i+1 to n do    if a[j]<a[k] then k:=j;   if k<>i then    begin t:=a[i];a[i]:=a[k];a[k]:=t;end;  end; write('output data:'); for i:= 1 to n do write(a[i]:6); writeln; end.

  4. 2.插入排序 • 插入排序的基本思想:经过i-1遍处理后,L[1..i-1]己排好序。第i遍处理仅将L[i]插入L[1..i-1]的适当位置p,原来p后的元素一一向右移动一个位置,使得L[1..i]又是排好序的序列。

  5. 例2:输入序列数据按非减顺序输出. 程序1: program crpx;const n=7;var a:array[1..n] of integer;    i,j,k,t:integer;begin write('Enter date:'); for i:= 1 to n do read(a[i]); writeln; for i:=2 to n do  begin   k:=a[i];j:=i-1;   while (k<a[ j]) and (j>0) do    begin a[j+1]:=a[ j ];j:=j-1 end;   a[j+1]:=k;  end; write('output data:'); for i:= 1 to n do write(a[i]:6); writeln; end.

  6. 3.冒泡排序 • 冒泡排序又称交换排序其基本思想是:对待排序的记录的关键字进行两两比较,如发现两个记录是反序的,则进行交换,直到无反序的记录为止。

  7. 例:输入序列数据按非减顺序输出。 程序1: program mppx;const n=7;var a:array[1..n] of integer;    i,j,k,t:integer;begin     write('Enter date:');     for i:= 1 to n do read(a[i]);     for i:=1 to n -1 do      for j:=n downto i+1 do       if a[j-1]<a[j] then        begin t:=a[j-1];a[j-1]:=a[j];a[j]:=t end;     write('output data:');     for i:= 1 to n do write(a[i]:6);     writeln;end.

  8. 快速排序 快速排序的思想是:先从数据序列中选一个元素,并将序列中所有比该元素小的元素都放到它的右边或左边,再对左右两边分别用同样的方法处之直到每一个待处理的序列的长度为1, 处理结束.

  9. b[i]:=x;  i:=i+1;j:=j-1;  if s<j then quicksort(b,s,j);  if i<t then quicksort(b,i,t);  end;beginwrite('input data:');for i:=1 to n do read(a[i]);writeln;quicksort(a,1,n);write('output data:');for i:=1 to n do write(a[i]:6);writeln;end. 例:输入一组数据小到大排序. 程序1: program kspv;const n=7;typearr=array[1..n] of integer;vara:arr;i:integer;procedure quicksort(var b:arr; s,t:integer);var i,j,x,t1:integer;  begin  i:=s;j:=t;x:=b[i];  repeat   while (b[j]>=x) and (j>i) do j:=j-1;   if j>i then begin t1:=b[i]; b[i]:=b[j];b[j]:=t1;end;   while (b[i]<=x) and (i<j) do i:=i+1;   if i<j then begin t1:=b[j];b[j]:=b[i];b[i]:=t1; end  until i=j;

  10. 5.1 顺序查找1.顺序查找的思想是是:将查找值顺序逐个与结点值进行比较,相等即为查找成功,否则查找失败.

  11. 程序如下: program sxcz; const n=7; type arr=array[1..n] of integer; var x1,i:integer; a:arr; b:boolean; place:integer; procedure search(r:arr;m,x:integer; var found:boolean;var p:integer); begin p:=1;found:=false; while(p<=m) and not found do if r[p]=x then found:=true else p:=p+1; end; begin write('Enter array:'); for i:=1 to n do read(a[i]); writeln; write('Enter search data:'); read(x1); search(a,n,x1,b,place); if b then begin writeln('yes');writeln('Place of',x1:5,'is:',place); end else writeln('no'); end.

  12. 5.2 二分查找1.二分查找的基本思想:首先将结点按关键字排序,其次将查找值与中间位置的值比较,相等,查找成功;不等,则中间数据大于或小于查找值,无论怎样查找将在一半的数据中查找。

  13. 例:输入序列数据查找指定值. 程序: program sxcz; const n=7; type arr=array[1..n] of integer; var x1,i:integer; a:arr; place:integer; procedure paixv(var r:arr;m:integer); var k,j,i,t:integer; begin k:=m; while k>0 do begin j:=k-1;k:=0; for i:=1 to j do if r[i]>r[i+1] then begin t:=r[i];a[i]:=r[i+1];r[i+1]:=t;k:=i;end; end; end;

  14. procedure search(r:arr;m,x:integer; var p:integer); var low,high,mid:integer; begin p:=0;low:=1;high:=m; while low<=high do begin mid:=(low+high) div 2; if x>r[mid] then low:=mid+1 else if x<r[mid] then high:=mid-1 else begin p:=mid;exit;end; end; end; begin write('Enter array:'); for i:=1 to n do read(a[i]); writeln; write('Enter search data:'); read(x1); paixv(a,n); search(a,n,x1,place); if place<>0 then writeln('yes') else writeln('no'); end.

More Related