finish the sort

This commit is contained in:
2018-06-02 13:07:05 +08:00
parent f5b754a7bd
commit e98e1f2d99
38 changed files with 1377 additions and 999 deletions

View File

@@ -1,9 +1,10 @@
#include <random>
#include <functional>
#include <iostream>
#include <ctime>
using namespace std;
#define CONT
#define COUT
//#define CONT
//#define COUT_LIST
struct LinkNode{
int data;
@@ -11,11 +12,11 @@ struct LinkNode{
};
#ifdef CONT
long int cont_if = 0;
long int cont_change = 0;
long long int cont_if = 0;
long long int cont_change = 0;
#endif
#ifdef CONT
#ifdef COUT_LIST
void coutList(LinkNode* head){
head = head->next;
while (head){
@@ -27,13 +28,28 @@ void coutList(LinkNode* head){
#endif
LinkNode* initArandList(int num){
auto* head = new LinkNode{0, nullptr};
default_random_engine generator;
LinkNode* head = nullptr;
default_random_engine generator((unsigned)clock());
uniform_int_distribution<int> dis(0,10000);
auto dice = bind(dis,generator);
dice();
for(int i=0;i<num;i++)
head = new LinkNode{dice(),head};
return head;
return new LinkNode{0,head};
}
LinkNode* initLinkList(int num){
LinkNode *head = nullptr;
for(int i = 0;i<num;i++){
head = new LinkNode{i,head};
}
return new LinkNode{0,head};
}
LinkNode* initReList(int num){
LinkNode *head = nullptr;
for(int i = num;i>0;i--){
head = new LinkNode{i,head};
}
return new LinkNode{0,head};
}
void InsertSort(LinkNode* in) {
#ifdef CONT
@@ -52,7 +68,7 @@ void InsertSort(LinkNode* in) {
#ifdef CONT
cont_if++;
#endif
if (h1->next->data < h2->data) {
if (h1->next->data > h2->data) {
h3->next = h2->next;
h2->next = h1->next;
h1->next = h2;
@@ -60,7 +76,7 @@ void InsertSort(LinkNode* in) {
#ifdef CONT
cont_change += 4;
#endif
#ifdef COUT
#ifdef COUT_LIST
coutList(in);
#endif
break;
@@ -91,18 +107,18 @@ void BubbleSort(LinkNode* in){
while (head->next != end){
if(head->data > head->next->data){
#ifdef CONT
cont_if++;
cont_change +=3;
#endif
tmp = head->data;
head->data = head->next->data;
head->next->data = tmp;
#ifdef COUT
#ifdef COUT_LIST
coutList(in);
#endif
}
head = head->next;
#ifdef CONT
cont_if ++;
cont_change +=1;
#endif
}
@@ -117,24 +133,137 @@ void SelectionSort(LinkNode *in){
cont_if = 0;
cont_change = 0;
#endif
LinkNode *end=in,*head=end,*beformin = nullptr,*tmp;
while (end){
LinkNode *end=in,*head,*beformin = nullptr,*tmp;
while (end->next->next){
beformin = end;
head = end;
while (head->next){
if(head->next->data < beformin->next->data){
#ifdef CONT
cont_change ++;
#endif
beformin = head;
}
#ifdef CONT
cont_if ++;
cont_change ++;
#endif
head = head->next;
}
#ifdef CONT
cont_change += 5;
#endif
tmp = beformin->next;
beformin->next = tmp->next;
tmp->next = end->next;
end->next = tmp;
end = tmp;
#ifdef COUT_LIST
coutList(in);
#endif
}
}
void swap(int &a,int &b){
int tmp = a;
a = b;
b = tmp;
}
LinkNode* GetPartion(LinkNode* pBegin, LinkNode* pEnd)
{
int key = pBegin->data;
LinkNode* p = pBegin;
LinkNode* q = p->next;
while(q != pEnd)
{
#ifdef CONT
cont_change++;
cont_if++;
#endif
if(q->data < key)
{
#ifdef CONT
cont_change +=4;
#endif
p = p->next;
swap(p->data,q->data);
}
q = q->next;
}
#ifdef CONT
cont_change +=3;
#endif
swap(p->data,pBegin->data);
return p;
}
void QuickSort(LinkNode* pBeign, LinkNode* pEnd, LinkNode* in)
{
if(pBeign != pEnd)
{
LinkNode* partion = GetPartion(pBeign,pEnd);
#ifdef COUT_LIST
coutList(in);
#endif
QuickSort(pBeign,partion,in);
QuickSort(partion->next,pEnd,in);
}
}
void QuickSort(LinkNode* in){
#ifdef CONT
cont_if = 0;
cont_change = 0;
#endif
QuickSort(in, nullptr,in);
}
void freeTheLinkList(LinkNode* in){
while(in){
LinkNode* tmp = in;
in = tmp->next;
delete tmp;
}
}
int main()
{
int size = 10;
char *sortType[] ={"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>","ð<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>","<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>","ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"};
void (*sortFun[])(LinkNode*) = {InsertSort,BubbleSort,QuickSort,SelectionSort};
char *testType[] = {"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>","˳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>","<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"};
LinkNode* (*testFun[])(int) = {initArandList,initReList,initLinkList};
#ifndef COUT_LIST
size = 10;
for(int n = 1;n<4;n++) {
size = size*10;
cout << endl << "<EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݹ<EFBFBD>ģ:" << size << endl;
#endif
for (int j = 0; j < 3; j++) {
cout << "<EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:" << testType[j] << endl;
for (int i = 0; i < 4; i++) {
auto randomlist = testFun[j](size);
#ifdef COUT_LIST
coutList(randomlist);
#endif
#ifndef CONT
clock_t start, end;
start = clock();
#endif
sortFun[i](randomlist);
#ifndef CONT
end = clock();
cout << sortType[i] << "<EFBFBD><EFBFBD>ʱ:" << (end - start) << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << endl;
#endif
#ifdef COUT_LIST
coutList(randomlist);
#endif
#ifdef CONT
cout << sortType[i] << "ʹ<EFBFBD>ø<EFBFBD>ֵ:" << cont_change << "<EFBFBD><EFBFBD>" << endl;
cout << sortType[i] << "ʹ<EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>:" << cont_if << "<EFBFBD><EFBFBD>" << endl;
#endif
freeTheLinkList(randomlist);
}
}
#ifndef COUT_LIST
}
#endif
return 0;
}