Sort Algorithm Data Structure Compositor.h

xiaoxiao2021-03-06  99

///

///

// Sort Algorithm Data Structure Compositor.h //

///

//

#include

Template

Class Compositor

{

PUBLIC:

Compositor (): sort (null) {}

Void Creat (); // Creating a rank number

Void bubble (); // bubbling sort

Void insert (); // Insert sort

// Quick sort

Void Quick ();

Void Qsort (int, int);

INT Partition (int low, int high);

// Multiple sort

Void Merge (Type SR [], Type Tr [], INT I, INT M, INT N);

Void Msort (Type Sr [], Type TR1 [], INT S, INT T);

Void mergesort ();

// Select sort

void select ();

Void print (); // Print the result

protected:

TYPE * SORT;

Int leng;

}

Template

Void Compositor :: creat ()

{

Cout << "Enter the number of data you need to sort:";

Cin >> Leng;

While (Leng <= 0)

{

COUT << "Enter data is incorrect";

Cin >> Leng;

}

Sort = new type [ieg];

Cout << "Please enter each data item:";

For (int i = 0; i

CIN >> SORT;

}

Template

void compositor :: insert ()

{

Creat ();

TYPE TEMP;

For (int i = 1; i

{

IF (sort

{

Temp = sort;

For (int J = I-1; Temp = 0; J -)

{

Sort [J 1] = Sort [J];

}

Sort [J 1] = TEMP;

}

}

PRINT ();

}

Template

Void Compositor :: bubble ()

{

Creat ();

TYPE TEMP;

For (int i = leng-1; i> = 0; I -)

{

For (int J = 0; j

{

IF (sort [j]> sort [j 1])

{

Temp = sort [j];

Sort [j] = sort [j 1];

Sort [J 1] = TEMP;

}

}

}

PRINT ();

}

Template

Void Compositor :: Quick ()

{

Creat ();

QSORT (0, Leng-1);

PRINT ();

}

Template

Void Compositor :: qsort (int S, INT T)

{

IF (S

{

INT pivotloc = partition (s, t);

Qsort (s, pivotloc-1); QSORT (Pivotloc 1, T);

}

}

Template

INT Compositor :: Partition (int low, int high)

{

TYPE PIVOTKEY = SORT [low];

While (low

{

While (low = pivotkey)

--h;

Sort [low ] = sort [high];

While (low

low;

Sort [high -] = sort [low];

}

Sort [low] = pivotkey;

Return Low;

}

Template

Void Compositor :: mergesort ()

{

Creat ();

Msort (sort, sort, 0, leng-1);

PRINT ();

}

Template

Void Compositor :: msort (Type SR [] ", TYPE TR1 [], INT S, INT T

{

Int m;

TYPE * TR2 = New Type [T-S];

IF (s == T) TR1 [S] = SR [S];

Else

{

m = (t s) / 2;

Msort (SR, TR2, S, M);

Msort (SR, TR2, M 1, T);

MERGE (TR2, TR1, S, M, T);

}

}

Template

Void Compositor :: Merge (Type Sr [], Type TR [], INT I, INT M, INT N)

{

For (int J = m 1, k = i; i <= m && j <= n; k )

{

IF (SR <= SR [J])

Tr [k] = SR [i ];

Else

Tr [K] = SR [J ];

}

While (i <= m)

Tr [k ] = SR [i ];

While (j <= n)

Tr [K ] = SR [J ];

}

Template

Void Compositor :: select ()

{

Creat ();

TYPE TEMP;

Int T;

For (int i = 0; i

{

T = i;

For (int J = i 1; j

{

IF (sort [t]> sort [j])

T = J;

}

IF (t! = i)

{

Temp = sort [t];

Sort [t] = sort;

Sort = TEMP;

}

}

PRINT ();

}

Template

Void Compositor :: print ()

{

COUT << "Sorting results are:";

For (int i = 0; i

COUT << Sort << "

Cout << Endl;

转载请注明原文地址:https://www.9cbs.com/read-102109.html

New Post(0)