7种排序算法
#pragma once
#include<iostream>
#include<string>
using namespace std;
class sort {
public:
void SWAP1(int &x, int &y) { x = x ^ y; y = x ^ y; x = x ^ y; }
void SWAP2(int &x, int &y) { int temp; temp = x; x = y; y = temp; }
void SWAP3(int &x, int &y) { x = x + y; y = x - y; x = x - y; }
void SWAP(int &x, int &y) {
if (x != y) {
SWAP1(x, y);
}
else {
SWAP2(x, y);
}
}
void selectsort(int arr[], int len) {
int i, j;
int index;
for (i = 0; i < len; i++) {
index = i;
for (j = i + 1; j < len; j++) {
index = (arr[j] < arr[index]) ? j : index;
}
SWAP(arr[i], arr[index]);
}
}
void bobsort(int arr[], int len) {
int i, j;
for (i = 0; i < len; i++) {
for (j = 0; j < len - i - 1; j++) {
if (arr[j] > arr[j + 1]) { SWAP(arr[j], arr[j + 1]); }
}
}
}
void insertsort1(int arr[], int len) {
int i, j;
for (i = 1; i < len; i++) {
for (j = i - 1; j >= 0 && arr[j] > arr[j + 1]; j--) {
SWAP(arr[j], arr[j + 1]);
}
}
}
void insertsort2(int arr[], int len) {
int i, j;
int tmp;
for (i = 1; i < len; i++) {
tmp = arr[i];
for (j = i - 1; j >= 0 && arr[j] > tmp; j--) {
arr[j + 1] = arr[j];
}
SWAP(arr[j + 1], tmp);
}
}
void hillsort(int arr[], int len) {
int i, j;
int tmp, d;
d = len >> 1;
while (d > 0) {
for (i = d; i < len; i++) {
tmp = arr[i];
for (j = i - d; j >= 0 && arr[j] > tmp; j -= d) {
arr[j + d] = arr[j];
}
SWAP(arr[j + d], tmp);
}
d >>= 1;
}
}
void quicksort(int arr[], int low, int high) {
int t = arr[low];
int f = low + 1;
int b = high;
if (low >= high) return;
while (f <= b) {
while (f <= b && arr[f] <= t) f++;
while (f <= b && arr[b] >= t) b--;
if (f < b) {
SWAP(arr[f], arr[b]);
f++;
b++;
}
}
arr[low] = arr[b];
arr[b] = t;
quicksort(arr, low, b - 1);
quicksort(arr, b + 1, high);
}
void heapify1(int arr[], int len) {
int i = 0;
while (i < len) {
if ((2 * i + 1) < len && (2 * i + 2) < len) {
int *lc = &arr[2 * i + 1];
int *rc = &arr[2 * i + 2];
int *p = (*lc > *rc) ? lc : rc;
if (*p > arr[i]) {
SWAP(*p, arr[i]);
i = 0;
}
else {
i++;
}
}
else if ((2 * i + 1) < len) {
int lc = arr[2 * i + 1];
if (lc > arr[i]) {
SWAP(arr[2 * i + 1], arr[i]);
i = 0;
}
else {
i++;
}
}
else {
i++;
}
}
}
void heapsort1(int arr[], int len) {
while (len > 0) {
heapify1(arr, len);
SWAP(arr[0], arr[len - 1]);
len--;
}
}
void heapify2(int arr[], int index,int heapsize) {
int lc = index * 2 + 1;
while (lc < heapsize) {
int largest = (lc + 1 < heapsize && arr[lc + 1] > arr[lc]) ? lc + 1 : lc;
largest = arr[largest] > arr[index] ? largest : index;
if (largest == index) {
break;
}
SWAP2(arr[largest], arr[index]);
index = largest;
lc = index * 2 + 1;
}
}
void heapinsert(int arr[], int index) {
while (arr[index] > arr[(index - 1) / 2]) {
SWAP2(arr[index], arr[(index - 1) / 2]);
index = (index - 1) / 2;
}
}
void heapsort2(int arr[], int len) {
if (arr == NULL || len < 2) {
return;
}
for (int i = 0; i < len; i++) {
heapinsert(arr, i);
}
int heapsize = len;
SWAP2(arr[0], arr[--heapsize]);
while (heapsize > 0) {
heapify2(arr, 0, heapsize);
SWAP2(arr[0], arr[--heapsize]);
}
}
void mergesort(int arr[],int len) {
if (arr == NULL || len < 2) {
return;
}
process(arr,0,len-1);
}
void process(int arr[], int L, int R) {
if (L == R) {
return;
}
int mid = L + ((R - L) >> 2);
process(arr,L, mid);
process(arr, mid + 1, R);
merge(arr, L, mid, R);
}
void merge(int arr[], int L, int M, int R) {
int len = R - L + 1;
int *T = new int[len];
int p1 = L;
int p2 = M + 1;
int i=0;
while (p1 <= M && p2 <= R) {
T[i++] = arr[p1] < arr[p2] ? arr[p1++] : arr[p2++];
}
while (p1<=M) {
T[i++] = arr[p1++];
}
while (p2 <= R) {
T[i++] = arr[p2++];
}
for (i = 0; i < len;i++) {
arr[L+i] = T[i];
}
}
void printArr(int arr[], int len) {
int i;
for (i = 0; i < len; i++) {
cout << arr[i] << "\t";
}
}
};
测试:
#include<iostream>
#include<fstream>
#include<string>
#include<ctime>
#include"sort.h"
#define MAXSIZE 100
using namespace std;
void Print_runtime() {
cout << clock() <<"us"<< endl;
}
int main() {
string str;
sort S;
int arr[MAXSIZE];
int length=5;
fstream testfile;
testfile.open("test.txt", ios::in);
if (!testfile.is_open()) { cout << "false!" << endl; }
int i;
for (i = 0; i < length; i++) {
testfile >> arr[i];
}
S.printArr(arr, length);
printf("\nInput sort name:(bob,insert1,insert2,hill,select,quick,heap1,heap2,merge):\n");
cin >> str;
if (str.find("bob") != string::npos) {S.bobsort(arr,length); Print_runtime();}
else if (str.find("insert1") != string::npos) { S.insertsort1(arr, length); Print_runtime(); }
else if (str.find("insert2") != string::npos) { S.insertsort2(arr, length); Print_runtime();}
else if (str.find("hill") != string::npos) { S.hillsort(arr,length); Print_runtime();}
else if (str.find("select") != string::npos) { S.selectsort(arr, length); Print_runtime();}
else if (str.find("quick") != string::npos) { S.quicksort(arr,0,length-1); Print_runtime();}
else if (str.find("heap1") != string::npos) { S.heapsort1(arr,length - 1); Print_runtime();}
else if (str.find("heap2") != string::npos) { S.heapsort2(arr, length); Print_runtime();}
else if (str.find("merge") != string::npos) { S.mergesort(arr,length); Print_runtime();}
S.printArr(arr, length);
system("pause");
}
|