Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Центр помощи > [FAQ][Pascal|VB|C#] Быстрая сортировка


Автор: THandle 22.3.2008, 19:59
Пример на Паскале:

Код

program QuickSort;

const
  MAX = 5;

type
  TArray = array [1..MAX] of integer;

procedure Swap(var Arr : TArray; pos1, pos2 : integer);
var
  tmp : integer;
begin
  tmp := Arr[pos1];
  Arr[pos1] := Arr[pos2];
  Arr[pos2] := tmp;
end;

procedure Sort(var Arr : TArray; mine, maxe: integer);
var
  i, j, n : integer;
begin
  if mine >= maxe then
    exit;
  n := Arr[mine];
  i := mine - 1;
  j := maxe + 1;
  while i < j do
    begin
      repeat
        inc(i);
      until Arr[i] >= n;
      repeat
        dec(j);
      until Arr[j] <= n;
    if i < j then
      swap(arr, i, j);
   end;
  Sort(Arr, mine, j);
  Sort(Arr, j + 1, maxe);
end;

var
  iArray : TArray;
  i : integer;
Begin
 for i := 1 to MAX do 
   begin
     write('Enter next element: ');
     readln(iArray[i]);
   end;
 Sort(iArray, 1, MAX);
 writeln('After sort: ');
 for i := 1 to MAX do
   writeln(iArray[i]);
 readln;
End.




Пример на Visual Basic:

Код

Option Explicit
Option Base 1
Const Max = 5
Dim A(1 To Max) As Long

Sub Swap(ByRef Arr() As Long, ByVal Pos1 As Long, ByVal Pos2 As Long)
  Dim Tmp As Long
  Tmp = A(Pos1)
  A(Pos1) = A(Pos2)
  A(Pos2) = Tmp
End Sub

Sub Sort(ByRef Arr() As Long, ByVal MinE As Long, ByVal MaxE As Long)
  Dim I, J, N As Long
  If MinE >= MaxE Then Exit Sub
  N = Arr(MinE)
  I = MinE - 1
  J = MaxE + 1
  Do While I < J
    Do
      I = I + 1
    Loop Until Arr(I) >= N
    Do
      J = J - 1
    Loop Until Arr(J) <= N
    If I < J Then Call Swap(Arr, I, J)
  Loop
  Call Sort(Arr, MinE, J)
  Call Sort(Arr, J + 1, MaxE)
End Sub

'Пример использования - по нажатию кнопки
Private Sub Command1_Click()
  Dim I As Long
  For I = 1 To Max
    A(I) = CLng(InputBox("Введите элемент:"))
  Next I
  Call Sort(A, 1, Max)
  'Массив отсортирован. Можно вывести данные, например, в ListBox
  For I = 1 To Max
    List1.AddItem CStr(A(I))
  Next I
End Sub




Авторы:

http://vingrad.ru/@Rrader - пример на VB.
http://vingrad.ru/@THandle - пример на Паскале.

Автор: THandle 27.5.2008, 14:00
Мой вариант на C#:

Код

using System;

class QuickSortClass
{    
     public const int MAX = 5;
     
     int[] IArray = new int[MAX];
     
     
     public void GetArray()
     {
         for (int i = 0; i < MAX; ++i)
         {
             Console.Write("Enter element: ");
             string S = Console.ReadLine();
             int el = int.Parse(S);
             IArray[i] = el;
         }
             
     }
     
     private void Swap(int First, int Second)
     {
          int tmp;
          tmp = IArray[First];
          IArray[First] = IArray[Second];
          IArray[Second] = tmp;
     }
     
     public void PrintArray()
     {
         for (int i = 0; i < MAX; ++i)
         {
            Console.WriteLine(IArray[i]);
         }
     }
     
     
     public void QuickSort(int MinE, int MaxE)
     {
      if (MinE >= MaxE)
          return;
      int n = IArray[MinE], i = MinE - 1, j = MaxE + 1;
      while (i < j)
      {
          do
          {
              ++i;
          }while (IArray[i] < n);
          do
          {
              --j;
          }while (IArray[j] > n);
          if (i < j)
              Swap(i, j);
      }
      QuickSort(MinE, j);
      QuickSort(j + 1, MaxE);
     }
}

class QuickSort
{
    public static void Main(string[] args)
    {
            QuickSortClass QSORT = new QuickSortClass();
            QSORT.GetArray();
            Console.WriteLine("Before sort: ");
            QSORT.PrintArray();
            QSORT.QuickSort(0, QuickSortClass.MAX - 1);
            Console.WriteLine("After sort: ");
            QSORT.PrintArray();       
            Console.ReadLine();  
    }
}


Автор: mannan 22.11.2011, 15:23
THandle а можешь код расписать пож - та

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)