C + + can implement a variety of sorting algorithm classes, such as direct insertion sort, binary insertion sort, shell sort, merge sort, simple selection order, cardinal order, the elements in the data array for hill sort, bubble sort, recursive implementation, heap sorting, using array to achieve the order of cardinality.
#ifndef sort_h #define Sort_h #include <iostream> #include <queue> using namespace std;
1. Direct Insert sort template<class elemtype> void Insertsort (Elemtype data[], int n);
2. Binary insert sort template<class elemtype> void Binsertsort (Elemtype data[], int n);
3.Shell sort//The elements in the data array are sorted by Hill, N is the increment sequence for the array size//increments, Incrementslength is the size of the increment sequence Template<class elemtype>
void Shellsort (Elemtype data[],int increments[], int n, int incrementslength);
1.Bubble Sort template<class elemtype> void Bubblesort (Elemtype data[], int n);
2. Quick sort template<class elemtype> void QuickSort (Elemtype data[], int n); Merge sort////////////////////merge sorted template<class elemtype> void MergeSort (Elemtype data[
],int N);
Template<class elemtype> void Mergesortnonrecursion (Elemtype data[], int n); Selection sort////////////////////Simple select sort template<class elemtype> void Selectionsort (Elem
Type data[], int n); Heap Sort
Template<class elemtype> void Heapsort (Elemtype data[],int N);
Radix Sort/////////////////Static link table Node const int DIGITS = 10;
const int radix = 10;
Class Sllist; ostream& operator<< (ostream& os, sllist &s);//Because vc++6.0 does not support//namespace using a using Yu Youyuan std in the class Sllist
Pre-make forward declaration//if using other C + + compiler, these two sentences can be deleted//static linked list static linked list//[0]: Head node class Sllist {struct node {int key[digits];
int info;
int next;
};
Friend ostream& operator<< (ostream& os, sllist &s);
Public:sllist ():d ata (NULL), Length (0) {};
~sllist ();
void Arrange ();
void Init (int arr[],int n);
void Radixsort ();
Private:void Distribute (int[], int[], int);
void Collection (int[], int[], int);
Node *data;
int length;
};
Cardinality sort void Radixsort (int data[], int n);
void Radixsort (sllist&); Util///////////////template<class elemtype> void Swap (elemtype& A, elemtype& b) {El
Emtype C = A; A = b;
b = C;
int init (int** data);
Template<class elemtype> void print (Elemtype data[],int begin,int end); Insert sort directly, array data is used to hold the elements to be sorted, n is the number of elements to be sorted template<class elemtype> void Insertsort (Elemtype data[], int n) {elemtype T
mp
int I, J;
for (i = 1; i < n; i++) {if (Data[i] > Data[i-1]) continue; TMP = Data[i];
Save the element to be inserted data[i] = data[i-1]; for (j = i-1 J > 0 && data[j-1] > tmp;j--) data[j] = data[j-1]; element to move data[j] = tmp;
Insert to the correct position}//binary insert sort template<class elemtype> void Binsertsort (Elemtype data[], int n) {elemtype tmp;
int I, J, mid, Low, high; for (i = 1; i < n; i++) {tmp = Data[i];
Save the element to be inserted low = 0;
High = i-1; while (low <= high) {//in Data[low. High] binary find an ordered insertion in the position mid = (low + high)/2; binary if (TMP < DATA[MID]) high =--mid; Insertion point in lower half else low =++mid; Insertion point in high half area} for (j = i-1/J >= Low; j--) Data[j + 1] = Data[j]; element to move Data[low] = tmp; Insert to the correct position}//The elements in the data array are sorted by Hill, N is the array size//increments as the increment sequence, incrementslength is the size of the increment sequence Template<class elemtype> v
OID Shellsort (elemtype data[], int increments[], int n, int incrementslength) {int I, j, K;
Elemtype tmp;
for (k = 0; k < incrementslength; k++) {//in increments[k] for the order of increment (i = increments[k]; i < n; i++) {
TMP = Data[i];
for (j = i; J >= Increments[k]; J-= Increments[k]) {if (tmp >= data[j-increments[k]) break;
DATA[J] = data[j-increments[k]];
} Data[j] = tmp; }}//bubble sort template<class elemtype> void Bubblesort (Elemtype data[], int n) {int lastswapindex = n-1;//For
Record the last exchanged element subscript int I, J;
for (i = Lastswapindex i > 0;i = lastswapindex) {lastswapindex = 0; for (j = 0; J < i; j +) if (Data[j] > data[j + 1] {Swap (data[j],data[j + 1]);
Lastswapindex = j; Quick sort Template<class elemtype> int Partition (elemtype data[], int low, int high) {Elemtype pivot =
Data[low];
while (Low < high) {while (Low < high && Data[high] >= pivot) high--;
Data[low] = Data[high];
while (Low < high && pivot >= Data[low]) low++;
Data[high] = Data[low];
} Data[low] = pivot;
return to Low;
} template<class elemtype> void QuickSort (Elemtype data[], int begin, int end) {if (Begin >= end) return;
int pivot = Partition (data, begin, end);
QuickSort (data, begin, Pivot-1);
QuickSort (data, pivot + 1, end);
} template<class elemtype> void QuickSort (Elemtype data[], int n) {if (n < 2) return;
QuickSort (data, 0, n-1); In the array data, the elements of [lptr...rptr-1][rptr...rightend] are merged//Tmparr into the secondary storage space at the time of merging template<class elemtype> void merge ( Elemtype data[], Elemtype TMparr[], int lptr, int rptr, int rightend) {int leftend = rptr-1;
int ptr,i;
ptr = i = lptr; while (lptr <= leftend && rptr <= rightend) if (Data[lptr] <= data[rptr]) tmparr[ptr++] = data[
Lptr++];
else tmparr[ptr++] = data[rptr++];
while (Lptr <= leftend) tmparr[ptr++] = data[lptr++];
while (Rptr <= rightend) tmparr[ptr++] = data[rptr++];
for (; I <= rightend; i++) data[i] = Tmparr[i]; //recursive implementation//merging the elements of [begin...end] in the array data template<class elemtype> void Msort (Elemtype data[], Elemtype tmparr[]
, int begin, int end) {int middle;
if (begin >= end) return; Middle = (begin + End)/2; Divide data equally into the begin. Middle] and [middle. end] Msort (data, Tmparr, begin, middle); Recursive first half msort (data, Tmparr, Middle + 1, end); Recursive second half part Merge (data, Tmparr, begin, Middle + 1, end); Will Data[begin. Middle],data[middle..
end] to merge} Template<class elemtype> void MergeSort (Elemtype data[], int n) {elemtype* PARR = NULL;
PARR = new Elemtype[n];
Msort (data,parr,0,n-1);
Delete[] PARR; }//non-recursive implementation template<class elemtype> void Mpass (Elemtype data[], elemtype tmparr[], int n, int mergelength) {int I
= 0;
while (i <= n-2 * mergelength) {Merge (data, Tmparr, I, i + mergelength, i + 2 * mergeLength-1);
i = i + 2 * mergelength;
if (i + mergelength < n) Merge (data, Tmparr, I, i + mergelength, n-1);
} template<class elemtype> void Mergesortnonrecursion (Elemtype data[], int n) {int mergelength = 1;
elemtype* PARR = NULL;
PARR = new Elemtype[n];
while (Mergelength < n) {mpass (data, PARR, N, mergelength);
Mergelength *= 2;
} delete[] PARR;
//Simple Select sort template<class elemtype> void Selectionsort (Elemtype data[], int n) {int i, j, Min;
for (i = 0; i < n; i++) {min = i;
for (j = i + 1; j < N; j + +) {if (Data[j] < data[min]) min = j;
} Swap (Data[i],data[min]); //I///I specifies the end of the specified element in the array for subscript//returnPoint of the left child in the array subscript inline int leftchild (int i) {return 2 * i + 1;} template<class elemtype> void Heapadjust (Elemtype D
ata[], int i, int n) {elemtype tmp;
int child; for (TMP = data[i]; Leftchild (i) < n;
i = child) {child = Leftchild (i);
if (Child!= n-1 && data[child + 1] > Data[child])//Fetch larger children node child++;
if (TMP < Data[child]) data[i] = Data[child];
else break;
} Data[i] = tmp;
} template<class elemtype> void Heapsort (Elemtype data[], int n) {int i;
for (i = N/2 i >= 0; i--)//Jian Yu heapadjust (data, I, n);
for (i = n-1;i > 0; i--) {//Swap the root node of the heap with the last leaf node and adjust swap (data[0],data[i]);
Heapadjust (data, 0, I);
}///cardinality sorted by array void radixsort (int data[], int n) {const int radix = 10;
const int digits = 10;
int i,j,k,factor;
Queue<int> Queues[radix]; for (i = 0,factor = 1, i < digits;i++,factor *= radix) {for (j = 0;j < n; j +) queues[(Data[j]/factor)%radix].push (Data[j]); Assign for (k = j = 0 J < radix j++,k++)//Collect while (!queues[j].empty ()) {Data[k] = queue
S[j].front ();
Queues[j].pop ();
}}//Assignment void Sllist::D istribute (int front[], int rear[], int digit) {int I, index;
for (i = 0; i < radix i++) front[i] = 0;
for (i = Data[0].next i > 0; i = data[i].next) {index = Data[i].key[digit];
if (front[index] = = 0) Front[index] = i;
else Data[rear[index]].next = i;
Rear[index] = i;
}//collect void sllist::collection (int front[], int rear[], int digit) {int I, current; for (i = 0; front[i] = = 0; i++); Find the first non-null table Data[0].next = Front[i];
The header node points to the first node in the first nonempty table, current = rear[i++];
for (; i < radix; i++) {if (front[i) = = 0) continue; Data[current].next = Front[i];
Link two nonempty table current = Rear[i];
} data[current].next = 0;
}///cardinality sorted with sllist void Sllist::radixsort () {int i;
int Front[radix],rear[radix];
Assign and collect each keyword from the lowest-bit priority for (i = 0; i < DIGITS i++) {Distribute (front, rear, i);
Collection (front, rear, i);
} sllist::~sllist () {delete[] data;
length = 0;
} void Sllist::init (int arr[], int n) {length = n + 1;
if (data!= NULL) delete[] data;
data = new Node[n + 1];
Data[0].next = 1;
for (int i = 1; I <= n; i++) {int value = Data[i].info = Arr[i-1];
for (int j = 0;j < J + +) {data[i].key[j] = value% 10;//+ ' 0 ';
Value/= 10;
} Data[i].next = i + 1;
} data[n].next = 0;
To adjust the position of the element according to the pointer value of each node in the list, so that the elements in the sllist are arranged by the keyword positive sequence void Sllist::arrange () {int i, TMP; int current = Data[0].next; Current holds the present position of the first element for (i = 1; i < length; i++) {while (the current < i)///finds the I element and holds it in its present position in the static list cur
Rent = Data[current].next;
TMP = Data[current].next; If (current!= i) {Swap (data[current], data[i]);//I adjust the element in place data[i].next = current; Point to moved element} current = tmp; Prepare for the first + 1 elements} ostream& operator<< (ostream& os,sllist &s) {for (int i = 1; i < s.length; i++) cout << s.data[i].info << "";
Os << Endl;
return OS; } #endif