C++ Exercises(十五)--排序算法的简单实现

 

struct  Node 
{//队列结点
    int data;
    
struct Node* pNext;
}
;
class  CQueue
{//队列类(带头结点)
public:
    CQueue(
void);
    
~CQueue(void);
    
bool isEmpty()const;//是否为空
    void EnQueue(int num);//入队列
    int DeQueue();//出队列
    int Front()const;//对头元素
    void clearQueue();//清空队列
    int Size()const;
    
void printQueue()const;
private:
    Node 
*front;//头结点
    Node *end;//尾结点
    int size;//队列大小
        
}
;
#include 
" Queue.h "
#include 
< cstdlib >
#include 
< assert.h >
#include 
< iostream >
using   namespace  std;

CQueue::CQueue(
void )
{
    
this->front = new Node;
    
this->front->pNext = NULL;
    
this->end = this->front;
    
this->size = 0;
}

void  CQueue::EnQueue( int  num)
{//入队列
    Node* pNum = new Node;
    pNum
->data = num;
    pNum
->pNext = NULL;
    
this->end->pNext = pNum;
    
this->end = pNum;
    
this->size++;
}

int  CQueue::DeQueue()
{//出队列,返回对头元素值
    assert(this->front!=this->end);//队列不空
    int result;
    Node
* pNum = this->front->pNext;
    result 
= pNum->data;
    
if (pNum==this->end)
    
{//队列中只有一个元素了
        this->front->pNext = NULL;
        
this->end = this->front;
    }

    
else
    
{
        
this->front->pNext = pNum->pNext;
    }

    delete pNum;
    
this->size--;
    
return result;
}

void  CQueue::clearQueue()
{//清空队列
    if (!this->isEmpty())
    
{
        Node
* pNum = this->front->pNext;//指向第一个结点
        Node* pre = this->front;
        
while (pNum!=NULL)
        
{
            pre 
= pNum;
            delete pre;
            pNum 
= pNum->pNext;
        }

        
this->front->pNext = NULL;
        
this->end = this->front;
        
this->size = 0;
    }


}

void  CQueue::printQueue() const
{
    
if (!this->isEmpty())
    
{
        Node
* pNum = this->front->pNext;
        
while (pNum!=NULL)
        
{
            cout
<<pNum->data<<" ";
            pNum 
= pNum->pNext;
        }

        cout
<<endl;
    }

}

int  CQueue::Size() const
{
    
return this->size;
}

bool  CQueue::isEmpty() const
{
    
return this->front==this->end;
}

CQueue::
~ CQueue( void )
{
this->clearQueue();
}



#include  " Queue.h "
#include 
< iostream >
using   namespace  std;

void  printArray( int  data[], int  n)
ExpandedBlockStart.gifContractedBlock.gif
{
    
int i;
    
for (i=0;i<n;++i)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        cout
<<data[i]<<" ";
    }

    cout
<<endl;
}

int  getRadix( int  num, int  count)
ExpandedBlockStart.gifContractedBlock.gif
{//返回num在趟数为count时的数值,0趟最后一位,趟倒数第一位,依次类推
    int temp = num,result,nCount=0;
    
while(nCount<=count)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        result 
= temp%10;
        temp 
= temp/10;
        nCount
++;
    }

    
return result;
}

void  RadixSort( int  data[], int  n, const   int  count)
ExpandedBlockStart.gifContractedBlock.gif
{//基数排序,count为趟数
    CQueue *queue = new CQueue[10];//下标从到的个队列
    int i,j,num,m;
    
//
    for (i=0;i<count;++i)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
//分配
        for (j=0;j<n;++j)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            num 
= getRadix(data[j],i);//当前趟数下的基数
            queue[num].EnQueue(data[j]);
        }

        
for (j=0;j<10;++j)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            cout
<<"队列"<<j<<":"<<endl;
            queue[j].printQueue();
        }

        
        
//收集
        m = 0;
        
for (j=0;j<10;++j)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
while (!queue[j].isEmpty())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                data[m] 
= queue[j].DeQueue();
                m
++;
            }


        }

        cout
<<"收集趟数: "<<i<<""<<endl;
        printArray(data,n);
    }

}


void  swap( int &  a, int &  b)
ExpandedBlockStart.gifContractedBlock.gif
{
    
int temp = a;
    a 
=  b;
    b 
= temp;
}

int  minElement( int  data[], int  begin, int  end)
ExpandedBlockStart.gifContractedBlock.gif
{
    
int result=begin,maxNum = data[begin],i;
    
for (i=begin+1;i<end;++i)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
if (data[i]<maxNum)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            maxNum 
= data[i];
            result 
= i;
        }

    }

    
return result;
}

void  SelectionSort( int  data[], int  n)
ExpandedBlockStart.gifContractedBlock.gif
{//选择排序
    int i,num;
    
//共需要进行n-1轮
    for (i=0;i<n-1;++i)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        num 
= minElement(data,i,n);
        swap(data[i],data[num]);
    }

}

void  AdjustHeap( int  data[], int  begin, int  end)
ExpandedBlockStart.gifContractedBlock.gif
{//堆调整data[beginend-1]
    int tmp = data[begin];
    
int c = begin*2,pos = begin;
    
while(c<=end)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
if (c<end&&data[c+1]>data[c])
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            c
++;
        }

        
if (data[c]>tmp)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            data[pos] 
= data[c];
            pos 
= c;
            c 
= c*2;
            
        }

        
else
            
break;
    }

    data[pos] 
= tmp;
}

void  BuildHeap( int  data[], int  n)
ExpandedBlockStart.gifContractedBlock.gif
{//初始建小顶堆
    int i;
    
for (i=n/2;i>0;--i)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        AdjustHeap(data,i,n);
    }


}


void  HeapSort( int  data[], int  n)
ExpandedBlockStart.gifContractedBlock.gif
{//堆排序
    int* tdata = new int[n+1];
    
int i;
    tdata[
0= -1;//第一个元素舍弃
    for (i=0;i<n;++i)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        tdata[i
+1= data[i];
    }

    BuildHeap(tdata,n); 
//将tdata[1n]建成初始堆
    for(i=n-1;i>=1;--i)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
//对当前无序区进行堆排序,共做n-1趟。
        swap(tdata[1],tdata[i+1]);
        AdjustHeap(tdata,
1,i); //重新调整为堆
    }

    
for (i=0;i<n;++i)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        data[i] 
= tdata[i+1];
    }

    delete[] tdata;
}


void  BubbleSort( int  data[], int  n)
ExpandedBlockStart.gifContractedBlock.gif
{//冒泡排序
    bool isChange = true;
    
int i,k;
    
for (k=n-1;k>=1&&isChange==true;--k)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        isChange 
= false;
        
for (i=0;i<k;++i)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (data[i]>data[i+1])
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                swap(data[i],data[i
+1]);
                isChange 
= true;
            }

        }

    }

}


void  InsertSort( int  data[], int  n)
ExpandedBlockStart.gifContractedBlock.gif
{//插入排序
    int i,j,pos,num;
    
for (i=1;i<n;++i)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        num 
= data[i];
        
for (j=0;j<=i-1&&data[i]>data[j];++j);
        pos 
= j;//插入点
        for (j=i-1;j>=pos;--j)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            data[j
+1= data[j];
        }

        data[pos] 
= num;
    }

}

void  QuickSort( int   * data, int  low, int  high)
ExpandedBlockStart.gifContractedBlock.gif
{//快速排序
    int pivot;
    
int scanUp,scanDown;
    
int mid;
    
if(high-low<=0)
        
return;
    
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
if(high-low==1)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if(data[high]<data[low])
                swap(data[low],data[high]);
            
return;
        }

        mid 
= (low+high)/2;
        pivot 
= data[mid];
        swap(data[low],data[mid]);
        scanUp 
= low+1;
        scanDown 
= high;
        
do
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
while(scanUp<=scanDown&&data[scanUp]<=pivot)
                scanUp
++;
            
while(data[scanDown]>pivot)
                scanDown
--;
            
if(scanUp<scanDown)
                swap(data[scanUp],data[scanDown]);
        }
while(scanUp<scanDown);
        data[low] 
= data[scanDown];
        data[scanDown] 
= pivot;
        
if(low<scanDown-1)
            QuickSort(data,low,scanDown
-1);
        
if(scanDown+1<high)
            QuickSort(data,scanDown
+1,high);
    }

}

int  main()
ExpandedBlockStart.gifContractedBlock.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
int a[] = {10,32,55,41,39,12,11,15,20,19,21,22,29,25};
    
int len = sizeof(a)/sizeof(int);
    cout
<<"排序前:"<<endl;
    printArray(a,len);
    
//RadixSort(a,len,2);
    
//SelectionSort(a,len);
    
//HeapSort(a,len);
    
//BubbleSort(a,len);
    
//InsertSort(a,len);
    QuickSort(a,0,len);
    cout
<<"排序后:"<<endl;
    printArray(a,len);
    system(
"pause");
    
return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值