C + + insertion sort includes binary insert sort code, in the direct insert sort, array data for the sorted elements, n for the number of elements to be sorted, and also includes the elements in the data array in hill sort, n for the size of the array, etc.:
The code is as follows |
Copy Code |
#include <iostream> using namespace Std; #include "Sort.h" Insert sort directly, array data is used to hold elements to be sorted, n is the number of elements to be sorted Template<class elemtype> void Insertsort (Elemtype data[], int n) { Elemtype tmp; 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 move back DATA[J] = tmp; Insert to 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 the location of an ordered insertion Mid = (low + high)/2; Half if (TMP < DATA[MID]) High =--mid; Insertion point in the lower half area Else low = ++mid; Insertion point in high half area } for (j = i-1 J >=; j--) Data[j + 1] = Data[j]; Element move back Data[low] = tmp; Insert to correct position } } Sorts the elements in the data array by Hill, N is the size of the array Increments is the increment sequence, incrementslength is the size of the increment sequence Template<class elemtype> void Shellsort (Elemtype data[], int increments[], int n, int incrementslength) { int I, j, K; Elemtype tmp; for (k = 0; k < incrementslength; k++) {//To be sorted in increments[k] increments for (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; } } } |
Add a
1. Direct insertion sort; 2. binary insert sort; 3.Shell sort; 4. bubble sort; 5. quick sort; 6. Simple selection sort; 7. heap sort; 8.2-path merge sort; 9.2-Path Merge sort (non-recursive); 10. Cardinal Order, etc., program code as follows:
The code is as follows |
Copy Code |
#include "Sort.h" int main () { cout << "---This program to achieve sorting---" << endl << Endl; cout << "1. Direct insertion sort; 2. binary insertion sort; 3.Shell sort;" << Endl; cout << "4 bubble sort; 5. Quick sort;" << Endl; cout << "6. Simple selection sort; 7. Heap sort;" << Endl; cout << "8.2-way merge sort; 9.2-way merge sort (non-recursive); 10. Cardinal order;" << Endl; cout << "0. Exit" << Endl << Endl;
int n,select,*pdata = NULL; Sllist list; int* increments;
while (true) { cout << "Please select the sorting algorithm:" << Endl; CIN >> Select; Switch (SELECT) { Case 1: n = init (&pdata); Insertsort (Pdata,n); Break Case 2: n = init (&pdata); Binsertsort (Pdata,n); Break Case 3: int incrementslenth,i; cout << "Please enter increment sequence length:" << Endl; Cin >> Incrementslenth; increments = new Int[incrementslenth]; cout << "Please enter the increment sequence:" << Endl; for (i = 0; i < incrementslenth; i++) CIN >> Increments[i]; n = init (&pdata); Shellsort (Pdata,increments,n,incrementslenth); Delete[] increments; Break Case 4: n = init (&pdata); Bubblesort (Pdata,n); Break Case 5: n = init (&pdata); QuickSort (Pdata,n); Break Case 6: n = init (&pdata); Selectionsort (Pdata,n); Break Case 7: n = init (&pdata); Heapsort (Pdata,n); Break Case 8: n = init (&pdata); MergeSort (Pdata,n); Break Case 9: n = init (&pdata); Mergesortnonrecursion (PData, N); Break Case 10: n = init (&pdata); List. Init (Pdata,n); List. Radixsort (); List. Arrange (); Break Radixsort (Pdata,n); Break Case 0: System ("pause"); return 0; Default cout << "Input error! "<< Endl << Endl; Continue } cout << "The result of the order is:" << Endl; if (select = = 10) cout << list; Else Print (pdata,0,n-1); cout << Endl;
Delete[] PData; } System ("pause"); return 0; } int init (int** pData) { int size,i; cout << "Enter the number of elements to be sorted" << Endl; CIN >> size;
*pdata = new Int[size];
cout << "Enter the elements to be sorted" << Endl; for (i = 0; i < size; i++) Cin >> (*pdata) [i]; return size; } Template<class elemtype> void print (Elemtype data[],int begin,int end) { for (int i = begin; I <= end; i++) { cout << Data[i] << ""; } cout << Endl; } |