Complicated sort

xiaoxiao2021-03-05  29

#include

// Hill sort: unstable, is a reform of direct insertion

Void shellsort (int A [】, int N)

{

INT H, I, J, T;

For (h = n / 2; h> 0; h / = 2) // h is the step size, the following algorithm is replaced by the direct insertion algorithm in H.

For (i = h; i

{

T = a [i];

J = I-h;

While (j> = 0 && t

{

a [j h] = a [j];

J- = H;

}

A [J H] = T;

}

}

// Quick sort: unstable, is an improvement in bubble sorting

Void Quicksort (Int a [], int low, int high)

{

INT I, J, T;

IF (low

{

i = Low; j = high; t = a [low]; // Divide for LOW to HIGH as a [low]

While (i

{

While (I T)

J -;

IF (i

a [i ] = a [j];

While (i

i ;

IF (i

a [j -] = a [i];

}

a [i] = t;

Quicksort (A, Low, I-1);

Quicksort (A, I 1, HIGH);

}

}

// Stack Sort: Unstable, is the improvement of directly selecting sort

Void Sift (Int a [], int N, int S)

{

INT I, J, T;

i = S; j = 2 * i 1;

T = a [s];

While (j

{

IF (J

J ;

IF (t

{

a [i] = a [j];

i = j;

J = 2 * i 1;

}

Else

Break;

}

a [i] = t;

}

Void Heapsort (Int a [], int N)

{

INT I, J, T;

// Construction initial pile

For (i = n / 2; i> = 0; I -)

SIFT (A, N, I);

For (j = n-1; j> = 1; J -)

{

T = a [0];

a [0] = a [j];

a [j] = t;

SIFT (A, J, 0);

}

}

void main ()

{

INT A [] = {93, 35, 29, 45, 48, 82, 76, 17};

For (int i = 0; i <8; i )

Printf ("% d", a [i]);

Printf ("/ n");

Shellsort (A, 8);

For (i = 0; i <8; i )

Printf ("% d", a [i]);

Printf ("/ n");

Quicksort (A, 0, 7);

For (i = 0; i <8; i )

Printf ("% d", a [i]);

Printf ("/ n");

Heapsort (A, 8);

For (i = 0; i <8; i )

Printf ("% d", a [i]);

}


New Post(0)