头文件
#ifndef BUBBLE_H
#define BUBBLE_H
#include <bits/stdc++.h>
using namespace std;
struct node{
int data;
node *nx,*pre;
node():data(0),nx(nullptr),pre(nullptr){}
node(int ndata,node* nnx = nullptr,node* npre = nullptr):data(ndata),nx(nnx),pre(npre){}
};
class LinkList{
private:
node* hd;
int len;
void swap(node* a,node* b);
public:
LinkList(){hd = new node();len=0;}
virtual ~LinkList();
void insert(int num);
void traverse() const;
void bubble();
};
void LinkList::swap(node* a,node* b){
b->pre = a->pre;
a->nx = b->nx;
a->pre->nx = b;
if(b->nx) b->nx->pre = a;
a->pre = b;
b->nx = a;
}
void LinkList::bubble(){
int n = len;
node *p;
while(n){
bool flag = false;
int cnt = n;
p = hd->nx;
while(cnt){
if(p->data>p->nx->data) swap(p,p->nx),flag=true;
else p = p->nx;
cnt--;
}
if(!flag) return ;
n--;
}
}
LinkList::~LinkList(){
while(hd){
node* tmp = hd->nx;
delete hd;
hd = tmp;
}
}
void LinkList::traverse() const{
node* p = hd->nx;
while(p){
cout<<p->data<<" ";
p = p->nx;
}
cout<<endl;
}
void LinkList::insert(int num){
if(hd->nx==nullptr){
hd->nx = new node(num,nullptr,hd);
return ;
}
node* pGuard = hd->nx;
while(pGuard->nx!=nullptr)
pGuard = pGuard->nx;
pGuard->nx = new node(num,nullptr,pGuard);
len++;
}
int Partition(int *a,int l,int r){
int tmp = a[l];
while(l<r){
while(l<r&&a[r]>=tmp) r--;
a[l] = a[r];
while(l<r&&a[l]<tmp) l++;
a[r] = a[l];
}
a[l] = tmp;
return l;
}
void Qsort(int *a,int len){
int sz = 0 , *lst = new int[len+50] , *rst = new int[len+50];
int l = 0 , r = len-1;
lst[++sz] = l , rst[sz] = r;
while(sz){
l = lst[sz] , r = rst[sz--];
int mid = Partition(a,l,r) , lenl = mid-l , lenr = r-mid;
if(lenl<4&&lenr<4) {
sort(a+l,a+mid);
sort(a+mid+1,a+r+1);
}else if(lenl<4&&lenr>=4){
sort(a+l,a+mid);
lst[++sz] = mid+1 , rst[sz] = r;
}else if(lenl>=4&&lenr<4){
sort(a+mid+1,a+r+1);
lst[++sz] = l , rst[sz] = mid-1;
}else{
if(lenl>=lenr) {
lst[++sz] = mid+1 , rst[sz] = r;
lst[++sz] = l , rst[sz] = mid-1;
}else{
lst[++sz] = l , rst[sz] = mid-1;
lst[++sz] = mid+1 , rst[sz] = r;
}
}
}
delete[] lst;
delete[] rst;
}
void TwoPathInsertSort(int *a,int len){
int *b = new int [3*len] , l = len,r = len;
b[r++] = a[0];
for(int i=1;i<len;i++){
int pos=l;
while(pos<r&&b[pos]<=a[i]) pos++;
int prev = pos-l , beh = r-pos;
if(pos==r) b[r++] = a[i];
else if(pos==l) b[--l] = a[i];
else if(prev<=beh){
for(int j=l-1;j<pos-1;j++)
b[j] = b[j+1];
b[pos-1] = a[i];
l--;
}else{
for(int j=r;j>=pos+1;j--)
b[j] = b[j-1];
b[pos] = a[i];
r++;
}
for(int j=l;j<r;j++)
cout<<b[j]<<" ";
cout<<endl;
}
for(int i=0;i<len;i++)
a[i] = b[i+l];
delete[] b;
}
void CountSort(int *a,int len){
int mi = 0x3f3f3f3f , mx = -1;
for(int i=0;i<len;i++)
mx = max(mx,a[i]) , mi = min(mi,a[i]);
int *b = new int[mx-mi+100];
for(int i=0;i<=mx-mi;i++) b[i]=0;
for(int i=0;i<len;i++)
b[a[i]-mi]++;
for(int i=0;i<=mx-mi;i++){
b[i+1] += b[i];
a[b[i]-1] = i+mi;
}
delete[] b;
}
#endif
test.cpp
#include<bits/stdc++.h>
#include"SwapSort.h"
#include"InsertSort.h"
#include"BubbleSort.h"
#include"bubble.h"
using namespace std;
void test01(){
int b[10] = {1,9,7,4,5,2,10,3,6,8};
for(int i=0;i<10;i++)
cout<<b[i]<<" ";
cout<<endl;
Qsort(b,10);
for(int i=0;i<10;i++)
cout<<b[i]<<" ";
}
void test02(){
LinkList l;
for(int i=1,u;i<=5;i++){
cin>>u;
l.insert(u);
}
l.bubble();
l.traverse();
}
void test03(){
int b[10] = {1,9,7,4,5,2,10,3,6,8};
for(int i=0;i<10;i++)
cout<<b[i]<<" ";
cout<<endl;
TwoPathInsertSort(b,10);
for(int i=0;i<10;i++)
cout<<b[i]<<" ";
}
void test04(){
int b[10] = {1,9,7,4,5,2,10,3,6,8};
for(int i=0;i<10;i++)
cout<<b[i]<<" ";
cout<<endl;
CountSort(b,10);
for(int i=0;i<10;i++)
cout<<b[i]<<" ";
}
int main(){
test04();
return 0;
}