zoukankan      html  css  js  c++  java
  • dephi 多种排序 算法

    1冒泡排序

    procedure BubbleSort(var x:array of integer);

    var

      i,j,intTmp:integer;

    begin

      for i:=0 to high(x) do

      begin

        for j:=0 to high(x)-1do

        begin

          if x[j]>x[j+1]then

          begin

            intTmp:=x[j];

            x[j]:=x[j+1];

            x[j+1]:=intTmp;

          end;

        end;

      end;

    end;

    选择排序

    procedure SelectSort(var x:array of integer);

    var

      i,j,k,intTmp:integer;

    begin

      for i:=0 to high(x)-1 do

      begin

        intTmp:=x[i];

        k:=i;

        for j:=i+1 to high(x) do

        begin

          if intTmp>x[j] then

          begin

            k:=j;

            intTmp:=x[k];

          end;

        end;

        if k<>i then

        begin

          x[k]:=x[i];

          x[i]:=intTmp;

        end;

      end;

    end;

    3.插入排序

    procedure InsertSort(var x:array of integer);

    var

      i,j,intTmp:integer;

    begin

      for i:=1 to high(x) do

      begin

        for j:=i downto 1 do

        begin

          if x[j-1]>x[j] then

          begin

            intTmp:=x[j-1];

            x[j-1]:=x[j];

            x[j]:=intTmp;

          end;

        end;

      end;

    end;

    4.希尔排序

    procedure ShellSort(var x:array of integer);

    var

      h,i,j,intTmp:integer;

    begin

      h:=high(x) div 2;

      while h>0 do

      begin

        for i:=h to high(x) do

        begin

          j:=i;

          while (j>=h) and (x[j-h]>x[j]) do

          begin

            intTmp:=x[j-h];

            x[j-h]:=x[j];

            x[j]:=intTmp;

            j:=j-h;

          end;

        end;

        h:=h div 2;

      end;

    end;

    5.快速排序

    procedure QuickSort(var x:array of integer; L,R:integer);

    var

      i,j,intTmp:integer;

    begin

      if Lthen

      begin

        i:=L;

        j:=R;

        intTmp:=x[i];

        while ido

        begin

          while (iand (x[j]>=intTmp) do

          begin

            j:=j-1;

          end;

          if ithen x[i]:=x[j];

          while (iand (x[i]<=intTmp) do

          begin

            i:=i+1;

          end;

          if ithen x[j]:=x[i];

        end;

        x[i]:=intTmp;

        QuickSort(x,L,i-1);

        QuickSort(x,i+1,R);

      end;

    end;

    6.归并排序

    procedure Merge(var x,y:array of integer; L,M,R:integer);

    var

      i,j:integer;

    begin

      i:=L;

      j:=M+1;

      while (L<=M) and (j<=R) do

      begin

        if x[L]> x[j] then

        begin

          y[i]:=x[j];

          j:=j+1;

        end

        else

        begin

          y[i]:=x[L];

          L:=L+1;

        end;

        i:=i+1;

      end;

      while L<=M do

      begin

        y[i]:=x[L];

        i:=i+1;

        L:=L+1;

      end;

      while j<=R do

      begin

        y[i]:=x[j];

        i:=i+1;

        j:=j+1;

      end;

    end;

     

    procedure MergeSort(var x, y:TArrInt);

    var

      intLength,intLen,intLen_m,i:integer;

      tmp:TArrInt;

    begin

      intLength:=high(x)+1;

      intLen:=1;

     

      while intLen

      begin

        intLen_m:=intLen;

        intLen:=intLen*2;

        i:=0;

        while i+intLen

        begin

          Merge(x,y,i,i+intLen_m-1,i+intLen-1);

          i:=i+intLen;

        end;

        if i+intLen_m

        begin

          Merge(x,y,i,i+intLen_m-1,intLength-1);

        end;

     

        tmp:=x;

        x:=y;

        y:=tmp;

      end;

    end;

    7.堆排序

    procedure HeapAdjust(var x:array of integer; i,intLen:integer);

    var

      intTmp,intChild:integer;

    begin

      intTmp:=x[i];

      intChild:=2*i+1;

      while intChild

      begin

        if (intChild+1

        begin

          intChild:=intChild+1;

        end;

        if x[i]

        begin

          x[i]:=x[intChild];

          i:=intChild;

          intChild:=2*i+1;

        end

        else

        begin

          break;

        end;

        x[i]:=intTmp;

      end;

    end;

     

    procedure BuildHeap(var x:array of integer);

    var

      i:integer;

    begin

      for i:=high(x) div 2 downto 0 do

      begin

        HeapAdjust(x,i,High(x)+1);

      end;

    end;

     

    procedure HeapSort(var x:array of integer);

    var

      i,intTmp:integer;

    begin

      BuildHeap(x);

      for i:=high(x) downto 0 do

      begin

        intTmp:=x[i];

        x[i]:=x[0];

        x[0]:=intTmp;

        HeapAdjust(x,0,i);

      end;

    end;

     性能测试对比单位:毫秒



  • 相关阅读:
    第06课:GDB 常用命令详解(下)
    第05课:GDB常用命令详解(中)
    第04课:GDB常用命令详解(上)
    第03课:GDB常用的调试命令概览
    第02课:启动GDB调试
    第01课:调试信息与调试原理
    数据库(二)
    数据库笔记(一)
    acedSSGet 翻译
    ObjectARX动态添加AutoCAD传统下拉菜单入门篇(一)
  • 原文地址:https://www.cnblogs.com/luckForever/p/7254162.html
Copyright © 2011-2022 走看看