Реализации алгоритмов/Сортировка/Перемешиванием

k:= 25; {Индекс последнего изменения}
s:= 1; {Первый элемент массива}
e:= 25; {Последний элемент массива}
while e > s do
begin
   for i:= e downto s+1 do if Arr[i] < Arr[i-1] then
   begin
      tmp := Arr[i];
      Arr[i] := Arr[i-1];
      Arr[i-1] := tmp;
      k := i;
   end;
   s:=k;
   for i:= s to e-1 do if Arr[i]>Arr[i+1] then
   begin
      tmp := Arr[i];
      Arr[i] := Arr[i+1];
      Arr[i+1] := tmp;
      k := i;
   end;
   e:=k;
end;

Для варианта С++ и С пример не совсем соответствуют.

  #include <algorithm>

  template< typename Iterator >
  void cocktail_sort( Iterator first, Iterator last )
  {
      for( --last; first < last; --last, ++first )
      {
          for( Iterator i = first; i < last; ++i )
              if ( *(i + 1) < *i )
                  std::iter_swap( i, i + 1 );

          for( Iterator i = last - 1; i > first; --i )
              if ( *i < *(i - 1) )
                  std::iter_swap( i, i - 1 );
      }
  }
#define SWAP(A,B) {(A)=(A)^(B); (B)=(A)^(B); (A)=(A)^(B);}

void m_sheker(int mas[], int n)
{
	int last = n-1, left = 1, right = n-1, j;

	do
	{
		for(j = right; j >= left; j--)
		{
			if(mas[j-1] > mas[j])
			{
				SWAP(mas[j-1], mas[j]);
				last = j;
			}
		}

		left = last + 1;

		for(j = left; j <= right; j++)
		{
			if(mas[j-1] > mas[j])
			{
				SWAP(mas[j-1], mas[j]);
				last = j;
			}
		}

		right = last-1;

	} while(left < right);
}
 public static void ArrSort(int[] name)
        {
            int b = 0;
            int left = 0;//Левая граница
            int right = name.Length - 1;//Правая граница
            while(left<right)
            {
                for (int i = left; i < right; i++)//Слева направо...
                {
                   if (name[i] > name[i + 1])
                        {
                            b = name[i];
                            name[i] = name[i + 1];
                            name[i + 1] = b;
                            b= i;
                        }
                }
                right = b;//Сохраним последнюю перестановку как границу
                if (left >= right) break;//Если границы сошлись выходим
                for (int i = right; i > left; i--)//Справа налево...
                {
                   if (name[i-1] > name[i])
                        {
                            b = name[i];
                            name[i] = name[i-1];
                            name[i -1] = b;
                            b = i;
                        }
                }
                left = b;//Сохраним последнюю перестановку как границу
            }
        }
program cocktailsort

integer A(17)
integer i,t
logical::sort=.true.
open(1,file='massiv.txt',status='old')
	read(1,*) A
		do i=1,17
			write(*,*) A(i)
		enddo
do while(sort)
sort=.false.
	do i=1, 16
		if(A(i)>A(i+1)) then
			t=A(i)
			A(i)=A(i+1)
			A(i+1)=t
			sort=.true.
		endif
	enddo
sort=.false.

	do i=16, 1, -1
		if(A(i)>A(i+1)) then
			t=A(i)
			A(i)=A(i+1)
			A(i+1)=t
			sort=.true.
		endif
	enddo
enddo
write(*,*) '---'
do i=1,17
	write(*,*) A(i)
enddo
pause
end
<?php
function swap(&$a, &$b) {
    $a = $a ^ $b;
    $b = $a ^ $b;
    $a = $a ^ $b;
}



function sheker($mas, $n)
{
        $last = $n-1; $left = 1; $right = $n-1;

        do
        {
                for($j = $right; $j >= $left; $j--)
                {
                        if($mas[$j-1] > $mas[$j])
                        {
                                swap($mas[$j-1], $mas[$j]);
                                $last = $j;
                        }
                }

                $left = $last + 1;

                for($j = $left; $j <= $right; $j++)
                {
                        if($mas[$j-1] > $mas[$j])
                        {
                                swap($mas[$j-1], $mas[$j]);
                                $last = $j;
                        }
                }

                $right = $last-1;

        } while($left < $right);
}
//(sheker($array,count($array)));
?>
while ( $left < $right ) {
	for ( my $j = $right; $j >= $left; $j-- ) {
		if ( $array_for_sort[$j-1] > $array_for_sort[$j] ) {
			( $array_for_sort[$j-1],$array_for_sort[$j]) = ( $array_for_sort[$j],$array_for_sort[$j-1] );
			$last = $j;
		}
	}
	$left = $last + 1;
	for( my $j = $left; $j <= $right; $j++ ) {
		if ( $array_for_sort[$j-1] > $array_for_sort[$j] ) {
			( $array_for_sort[$j-1],$array_for_sort[$j] ) = ( $array_for_sort[$j],$array_for_sort[$j-1] );
			$last = $j;
		}
	}
	$right = $last - 1;
}