排序算法的簡單實現

作者: Phinecos(洞庭散人)  來源: 博客園  發布時間: 2008-08-16 22:49  閱讀: 886 次  推薦: 0   原文鏈接   [收藏]  
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)
{
    
int i;
    
for (i=0;i<n;++i)
    
{
        cout
<<data[i]<<" ";
    }

    cout<<endl;
}

int getRadix(int num,int count)
{//返回num在趟數為count時的數值,0趟最后一位,趟倒數第一位,依次類推
    int temp = num,result,nCount=0;
    
while(nCount<=count)
    
{
        result 
= temp%10;
        temp 
= temp/10;
        nCount
++;
    }

    return result;
}

void RadixSort(int data[],int n,const int count)
{//基數排序,count為趟數
    CQueue *queue = new CQueue[10];//下標從到的個隊列
    int i,j,num,m;
    
//
    for (i=0;i<count;++i)
    
{
        
//分配
        for (j=0;j<n;++j)
        
{
            num 
= getRadix(data[j],i);//當前趟數下的基數
            queue[num].EnQueue(data[j]);
        }

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

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


        }

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

}


void swap(int& a,int& b)
{
    
int temp = a;
    a 
=  b;
    b 
= temp;
}

int minElement(int data[],int begin,int end)
{
    
int result=begin,maxNum = data[begin],i;
    
for (i=begin+1;i<end;++i)
    
{
        
if (data[i]<maxNum)
        
{
            maxNum 
= data[i];
            result 
= i;
        }

    }

    return result;
}

void SelectionSort(int data[],int n)
{//選擇排序
    int i,num;
    
//共需要進行n-1輪
    for (i=0;i<n-1;++i)
    
{
        num 
= minElement(data,i,n);
        swap(data[i],data[num]);
    }

}

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

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

        else
            break;
    }

    data[pos] = tmp;
}

void BuildHeap(int data[],int n)
{//初始建小頂堆
    int i;
    
for (i=n/2;i>0;--i)
    
{
        AdjustHeap(data,i,n);
    }


}


void HeapSort(int data[],int n)
{//堆排序
    int* tdata = new int[n+1];
    
int i;
    tdata[
0= -1;//第一個元素舍棄
    for (i=0;i<n;++i)
    
{
        tdata[i
+1= data[i];
    }

    BuildHeap(tdata,n); //將tdata[1n]建成初始堆
    for(i=n-1;i>=1;--i)
    
//對當前無序區進行堆排序,共做n-1趟。
        swap(tdata[1],tdata[i+1]);
        AdjustHeap(tdata,
1,i); //重新調整為堆
    }

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

    delete[] tdata;
}


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

        }

    }

}


void InsertSort(int data[],int n)
{//插入排序
    int i,j,pos,num;
    
for (i=1;i<n;++i)
    
{
        num 
= data[i];
        
for (j=0;j<=i-1&&data[i]>data[j];++j);
        pos 
= j;//插入點
        for (j=i-1;j>=pos;--j)
        
{
            data[j
+1= data[j];
        }

        data[pos] = num;
    }

}

void QuickSort(int *data,int low,int high)
{//快速排序
    int pivot;
    
int scanUp,scanDown;
    
int mid;
    
if(high-low<=0)
        
return;
    
else 
    
{
        
if(high-low==1)
        
{
            
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
        {
            
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()
{
    
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;
}
0
0
 
 
 

文章列表

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()