各种排序小结

①冒泡排序(O(n^2))

稳定排序

// Balloon Sort
Var
  temp,i,j,k,n:integer;
  a:array[1..1000]of longint;
Begin
  readln(n);
  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
        temp:=a[j];
        a[j]:=a[j-1];
        a[j-1]:=temp;
      End;
  for i:=1 to n do
    write(a[i],' ');
  writeln;
End.

②快速排序(O(n*log(n)))

稳定版(双关键字排序)

// Quick Sort  稳定版快排 b记录下标
增加过程中临时变量sub记录中间数的下标
Var
  n,x:longint;
  a,b:array[1..1000000]of longint;

Procedure qsort(l,r:longint);
Var
  tmp,i,j,mid,sub:longint;
Begin
  i:=l; j:=r;
  mid:=a[(i+j)div 2];
  sub:=b[(l+r)div 2];
  repeat
    while (a[i]<mid) or ((a[i]=mid) and (b[i]<sub)) do inc(i);
    while (a[j]>mid) or ((a[j]=mid) and (b[j]>sub)) do dec(j);
    if (i<=j) then Begin
                     tmp:=a[i];a[i]:=a[j];a[j]:=tmp;
                     tmp:=b[i];b[i]:=b[j];b[j]:=tmp;
                     inc(i); dec(j);
                   End;
  until i>j;
  if l<j then qsort(l,j);
  if i<r then qsort(i,r);
End;

Begin
  readln(n);
  for x:=1 to n do
  Begin
    read(a[x]);
    b[x]:=x;
  End;
  qsort(1,n);
  for x:=1 to n do
    writeln(x,':',a[x],' ',b[x],' ');
  writeln;
End.

③归并排序(O(n*log(n)))

稳定,求逆序对。

// Merge Sort 稳定排序(注意等号否则不稳定)
Var
  a,r:array[0..10010] of integer;
  n,i:integer;

Procedure mergesort(s,t:integer);
Var
  m,i,j,k:integer;
Begin
  if s=t then exit;
  m:=(s+t)div 2;
  mergesort(s,m);
  mergesort(m+1,t);
  i:=s; j:=m+1; k:=s;
  while (i<=m)and(j<=t) do
  Begin
    if a[i]<=a[j] then
    Begin
      r[k]:=a[i];
      inc(i); inc(k);
    End
    else
Begin
ans:=ans-m-i+1;//这里是统计逆序对数的地方 可以画出区间来理解
      r[k]:=a[j];
      inc(j); inc(k);
    End;
  End;

  while i<=m do
  Begin
    r[k]:=a[i];
    inc(i); inc(k);
  End;

  while j<=t do
  Begin
    r[k]:=a[j];
    inc(j); inc(k);
  End;

  for i:=s to t do
    a[i]:=r[i];
End;

Begin
  readln(n);
  for i:=1 to n do
    read(a[i]);
  mergesort(1,n);
  for i:=1 to n do
    write(a[i],' ');
  writeln;
End. 

④堆排序(O(n*log(n)))

不稳定排序

堆的操作:(本来想在数据结构讲,想想还是写在这里吧)

      建堆:非叶子节点按倒顺 从上至下更新堆

      删除:对于一个建好的堆 将堆中最后一个元素覆盖到根节点,从上至下更新堆

      插入:对于一个建好的堆 将待插入的元素放在堆末,从下至上更新堆

// Heap Sort 不稳定排序
Var
  heap:array[0..10010] of longint;
  n,i,a,len:longint;

Procedure put(x:longint);
Var
  fa,son,temp:longint;
Begin
  len:=len+1;
  heap[len]:=x;
  son:=len;
  while (son<>1)and(heap[son]<heap[son div 2]) do
  Begin
    temp:=heap[son div 2];
    heap[son div 2]:=heap[son];
    heap[son]:=temp;
    son:=son div 2;
  End;
End;

Function get:longint;
Var
  fa,son,temp:longint;
Begin
  get:=heap[1];
  heap[1]:=heap[len];
  dec(len);
  fa:=1;
  while (fa*2<=len)or(fa*2+1<=len) do
  Begin
    if (fa*2+1>len)or(heap[fa*2+1]>heap[fa*2]) then
         son:=fa*2
    else son:=fa*2+1;

    if heap[fa]>heap[son] then
    Begin
      temp:=heap[fa];
      heap[fa]:=heap[son];
      heap[son]:=temp;
      fa:=son;
    End
    else break;
  End;
End;

Begin
  len:=0;
  readln(n);
  for i:=1 to n do
  Begin
    readln(a);
    put(a);
  End;

  for i:=1 to n-1 do
    write(get,' ');
  writeln(get);
End.
posted @ 2012-02-19 12:13  你滴韩王  阅读(175)  评论(0)    收藏  举报