Simple implementation of common sorting algorithms

Source: Internet
Author: User
Tags sorts

An experiment of data structure ....

/*************************************** ****************************

File Name: sortdemo. h
Abstract: The Declaration file of the sortdemo class, which can sort integer arrays.
Development Platform: Win XP SP2
Compiling environment: cl.exe 8.0 (in Visual Studio 2005 SDK)
By: 88250
Completion date: 2006-12-25 version: 1.0
Blog: http://DL88250.ynutx.net
E-mail: DL88250@gmail.com
QQ: 845765 or 316281008

**************************************** ***************************/

# Include <iostream>
# Include <ctime>

Using namespace STD;
Const int max_num = 25; // The most elements that can be sorted

Class sortdemo
{
Public:
// Display the array Sequence
Void displayserial (const int * r ){
For (INT I = 1; I <max_num; I ++)
{
Cout <* (R + I) <'';
}
Cout <Endl;
}
// Quick sorting
Void qsort (int * r, int low, int high );
// Heap sorting
Void hsort (int * R );
// Insert the sorting directly
Void isort (int * R );
// Merge and sort
Void msort (int * R );

PRIVATE:
// Used for fast sorting and splitting an array
Int partition (int * r, int low, int high );
// Used for heap sorting for downward Filtering
Void siftdown (int * r, int begin, int end );
// Used to merge and sort Sr [S. T] into Sr [S. T]
Void mergesort (int * Sr, int S, int t );
// Used to merge and sort the ordered Sr [I. m] AND Sr [M + 1. N] into ordered Sr [I. N]
Void Merge (int * Sr, int I, int M, int N );

};
/*************************************** ****************************

File Name: heapsort. cpp
Abstract: C ++ Implementation of heap sorting
Development Platform: Win XP SP2
Compiling environment: cl.exe 8.0 (in Visual Studio 2005 SDK)
By: 88250
Completion date: 2006-12-25 version: 1.0
Blog: http://DL88250.ynutx.net
E-mail: DL88250@gmail.com
QQ: 845765 or 316281008

**************************************** ***************************/

# Include "stdafx. H"
# Include "sortdemo. H"

// Function name: siftdown
// Function: used for heap sorting and downward filtering to make R [s... m] A large heap
// Input parameter: int * r: Address of the array to be sorted
// Int S: R [s] does not meet the definition of the heap.
// Int M: The maximum boundary of the r Array
// Output parameter: void
Void sortdemo: siftdown (int * r, int S, int m)
{
Int TMP = R [s];
 
Int I = s;
Int J = 2 * I; // R [J] is the left child of R [I]
While (j <= m)
{
If (j <m) & (R [J] <R [J + 1])
{// If the right child is large, change J to the subscript of the right child.
J ++;
}

If (TMP <R [J])
{
R [I] = R [J]; // transfers R [J] to the father's position.
I = J;
J = 2 * I;
}
Else
{// The filtering is complete and the loop jumps out.
Break;
}
}
R [I] = TMP; // The value of the screened node is placed in the final position.

Return;
}

// Function name: hsort
// Function: sorts the r array by heap
// Input parameter: int * r: Address of the array to be sorted
// Output parameter: void
Void sortdemo: hsort (int * r)
{
Int temp = 0;
// Create a heap
For (INT I = max_num/2; I> = 1; I --)
{
Siftdown (R, I, max_num-1 );
}
// Heap sorting
For (INT I = max_num-1; I> = 2; I --)
{
Temp = R [1];
R [1] = R [I];
R [I] = temp; // swap the first element with the last element in the current Interval
Siftdown (R, 1, I-1); // filter the R [1] node, get the heap of (I-1) node
}

Return;
}

/*************************************** ****************************

File Name: insertsort. cpp
Abstract: directly Insert the C ++ Implementation of sorting
Development Platform: Win XP SP2
Compiling environment: cl.exe 8.0 (in Visual Studio 2005 SDK)
By: 88250
Completion date: 2006-12-25 version: 1.0
Blog: http://DL88250.ynutx.net
E-mail: DL88250@gmail.com
QQ: 845765 or 316281008

**************************************** ***************************/

# Include "stdafx. H"
# Include "sortdemo. H"

Void sortdemo: isort (int * r)
{
Int J = 0;

For (INT I = 2; I <max_num; I ++)
{
If (R [I] <R [I-1])
{
R [0] = R [I]; // copy as a sentinel
R [I] = R [I-1];
}
For (j = I-2; R [0] <R [J]; -- J)
{
R [J + 1] = R [J]; // record move-back
}
R [J + 1] = R [0]; // insert to the correct position
}

Return;
}

/*************************************** ****************************

File Name: mergesort. cpp
Abstract: C ++ Implementation of Merge Sorting
Development Platform: Win XP SP2
Compiling environment: cl.exe 8.0 (in Visual Studio 2005 SDK)
By: 88250
Completion date: 2006-12-25 version: 1.0
Blog: http://DL88250.ynutx.net
E-mail: DL88250@gmail.com
QQ: 845765 or 316281008

**************************************** ***************************/

# Include "stdafx. H"
# Include "sortdemo. H"

// Function name: msort
// Function: Merge and sort array R
// Input parameter: int * sr: Address of the array to be sorted
// Output parameter: void
Void sortdemo: msort (int * r)
{
Mergesort (R, 1, MAX_NUM-1 );
Return;
}
 
// Function name: mergesort
// Function: Used to merge and sort Sr [S. T] into tr1 [S. T].
// Input parameter: int * sr: Address of the array to be sorted
// Int S: subscript S
// Int T: subscript t
Void sortdemo: mergesort (int * Sr, int S, int T)
{
If (S <t)
{
Int M = (S + T)> 1; // set Sr [s .. t] evenly divided into Sr [s .. m] AND Sr [M + 1 .. t]
Mergesort (Sr, S, m); // recursively merges Sr [S. T] into an ordered Sr [S. M]
Mergesort (Sr, m + 1, t); // recursively merge Sr [M + 1. T] into an ordered Sr [M + 1. T]
Merge (Sr, S, M, T); // merge Sr [S. M] AND Sr [M + 1. T] to Sr [S. T]
}

Return;
}

// Function name: Merge
// Function: Used to merge and sort ordered Sr [I. m] AND Sr [M + 1. N] into ordered Sr [I. N]
// Input parameter: int * sr: Address of the array to be sorted
// Int I: Low subscript
// Int M: Mid subscript
// Int N: High subscript
Void sortdemo: Merge (int * Sr, int I, int M, int N)
{
 
Int J = I, K = m + 1, P = 0;

Int * TR = new int [n-I + 1]; // auxiliary space

While (j <= m) & (k <= n ))
{// Merge Records in SR from small to large into tr
Tr [p ++] = (Sr [J] <= Sr [k])? Sr [J ++]: Sr [k ++];
}
While (j <= m)
{// Copy the remaining SR [I. m] to tr
Tr [p ++] = Sr [J ++];
}
While (k <= N)
{// Copy the remaining SR [J. N] to tr
Tr [p ++] = Sr [k ++];
}
// Return the ordered result to the SR
For (j = I, P = 0; j <= N; j ++, P ++)
{
Sr [J] = tr [p];
}

Return;
}
/*************************************** ****************************

File Name: qsort. cpp
Abstract: C ++ implementation of quick sorting
Development Platform: Win XP SP2
Compiling environment: cl.exe 8.0 (in Visual Studio 2005 SDK)
By: 88250
Completion date: 2006-12-25 version: 1.0
Blog: http://DL88250.ynutx.net
E-mail: DL88250@gmail.com
QQ: 845765 or 316281008

**************************************** ***************************/

# Include "stdafx. H"
# Include "sortdemo. H"

// Function name: qsort
// Function: quick sorting
// Input parameter: int * r: Address of the array to be sorted
// Int low: subscripts smaller than Pivot
// Int high: subscripts of records larger than Pivot
// Output parameter: void
Void sortdemo: qsort (int * r, int low, int high)
{
If (low {
// Split the table into two parts
Int partition = partition (R, low, high );
Qsort (R, low, semi-1); // recursively sorts low subtables.
Qsort (R, percentile + 1, high); // recursively sorts the high subtable
}

Return;
}

// Function name: Partition
// Function: divides the r array into two subtables through pivot.
// Input parameter: int * r: Address of the array to be sorted
// Int low: subscripts smaller than Pivot
// Int high: subscripts of records larger than Pivot
// Output parameter: void
Int sortdemo: partition (int * r, int low, int high)
{
R [0] = R [low]; // pivot record with each record in the subtable
 
While (low {// Scanning from both ends of the table alternately to the center
While (high> LOW) & (R [High]> = R [0])
{
High --;
}
If (high> LOW)
{
R [low] = R [High]; // move records smaller than pivot records to the low end
Low ++; // skip itself
}
While (low {
Low ++;
}
If (high> LOW)
{
R [High] = R [low]; // move records larger than pivot records to high-end
High --; // skip itself
 
}
}
R [low] = R [0]; // pivot records are in place

Return low; // return the Pivot Position
}

/*************************************** ****************************

File Name: testsortdemo. cpp
Abstract: Test file for the sortdemo class
Development Platform: Win XP SP2
Compiling environment: cl.exe 8.0 (in Visual Studio 2005 SDK)
By: 88250
Completion date: 2006-12-25 version: 1.0
Blog: http://DL88250.ynutx.net
E-mail: DL88250@gmail.com
QQ: 845765 or 316281008

**************************************** ***************************/

# Include "stdafx. H"
# Include "sortdemo. H"
# Include <cstdlib>
# Include <ctime>
# Include <iostream>

Using namespace STD;

Int main (void)
{
Int testarray [max_num]; // max_num is defined in sortdemo. h file.
Sortdemo sorter;
Srand (unsigned INT) Time (null ));
Int choose = 1;
For (;;)
{
// Generate a random number. R [0] is retained as the auxiliary space.
For (INT I = 1; I <max_num; I ++)
{
Testarray [I] = rand () %100;
}

Cout <"select Sorting Algorithm:" <Endl
<"1. Fast sorting 2. Heap sorting 3. Insert directly for sorting 4. 2. Merge and sort 5. Exit" <Endl;
Cin> choose;
If (5 = choose)
{
Return 0;
}
Cout <"sequence before sorting:" <Endl;
Sorter. displayserial (testarray );
Switch (choose)
{
Case 1:
{
Sorter. qsort (testarray, 1, max_num-1 );
Break;
}
Case 2:
{
Sorter. hsort (testarray );
Break;
}
Case 3:
{
Sorter. isort (testarray );
Break;
}
Case 4:
{
Sorter. msort (testarray );
Break;
}
Default:
{
Cout <"Incorrect choice! "<Endl;
Break;
}
}
Cout <"sorted sequence:" <Endl;
Sorter. displayserial (testarray );
System ("pause ");
System ("CLS ");
}

Return 0;
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.