[转]C++常见排序算法

原文: http://www.oschina.net/code/snippet_115193_2795

C++各种常见排序算法 冒泡排序,插入排序,快排序,选择排序,希尔排序

BubbleSort.cpp
// BubbleSort.cpp: implementation of the CBubbleSort class.
    //
    //
    
   #include "BubbleSort.h"
    
   //
   // Construction/Destruction
   //
     
    CBubbleSort::CBubbleSort()
    {
     
   }
     
    CBubbleSort::~CBubbleSort()
    {
     
    }
     
   void CBubbleSort::Sort (int * arr,const int size)
   {
        int tmp =0 ;
        for (int i =0 ;i
   
   

    
    

    
    
    
            for (int j=i+1;j
     
     

      
      

    
      
      
            {
                if(arr[i]>arr[j])
                {
                    tmp =arr[i];
                    arr[i] = arr[j];
                    arr[j] = tmp ;
                }
            }
    }
BubbleSort.h
// BubbleSort.h: interface for the CBubbleSort class.
//
//
 
#if !defined(AFX_BUBBLESORT_H__2A6E4581_4C19_4061_BD68_9FA2DD286EB5__INCLUDED_)
#define AFX_BUBBLESORT_H__2A6E4581_4C19_4061_BD68_9FA2DD286EB5__INCLUDED_
 
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
 
#include "Sort.h"
 
class CBubbleSort : public CSort  
{
public:
    CBubbleSort();
    virtual ~CBubbleSort();
 
    virtual void Sort(int * arr,const int size)  ;
 
};
 
#endif // !defined(AFX_BUBBLESORT_H__2A6E4581_4C19_4061_BD68_9FA2DD286EB5__INCLUDED_)

InsertSort.cpp
// InsertSort.cpp: implementation of the CInsertSort class.
//
//
 
#include "InsertSort.h"
 
//
// Construction/Destruction
//
 
CInsertSort::CInsertSort()
{
 
}
 
CInsertSort::~CInsertSort()
{
 
}
        /*
        if (mid != high&& mid != low)
        {
            exit(0);
        }
        */
 
 
 
void CInsertSort::Sort (int * arr,const int size)
{
    if(!arr)
        return ;
    //
    for (int i =1 ;i
   
   

    
    

    
    
    
    {
        int tmp = arr[i];
        
        int low =0,high = i-1 ,mid ;
        while (low<= high)
        {
            mid= (low+high)/2;
            if(arr[i]
     
     
                high = mid-1;
            else 
                low = mid +1 ;
        }
        //high ???tmp????????λ???low??high+1
        for (int j =i-1 ;j>=low;j--)
        {
            arr[j+1]= arr[j];
        }
        arr[low]= tmp ;
    }
}
 
 
void CInsertSort::DirectInsertSort(int *arr,const int size)
{
    if(!arr)
        return ;
    for (int i=1;i
     
     

      
      

    
      
      
    {          
        int tmp = arr[i];
        for (int j = i-1; j>=0 && tmp < arr[j];j--)
        {
            arr[j+1] = arr[j];
        }
        arr[j+1] =tmp ;
        
    }
}
InsertSort.h

 

// InsertSort.h: interface for the CInsertSort class.
//
//
 
#if !defined(AFX_INSERTSORT_H__FF5699F7_8D6A_4709_93F1_97CDC299528A__INCLUDED_)
#define AFX_INSERTSORT_H__FF5699F7_8D6A_4709_93F1_97CDC299528A__INCLUDED_
 
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
 
#include "Sort.h"
#include 
   
   
 
class CInsertSort : public CSort  
{
public:
    CInsertSort();
    virtual ~CInsertSort();
 
    virtual void Sort (int * arr,const int size) ;
    void DirectInsertSort(int *arr,const int size);
};
 
#endif // !defined(AFX_INSERTSORT_H__FF5699F7_8D6A_4709_93F1_97CDC299528A__INCLUDED_)

 

Main.cpp
#include 
   
   
#include 
   
   
#include 
   
    
   
   
#include "BubbleSort.h"
#include "InsertSort.h"
#include "QuickSort.h"
#include "SelectionSort.h"
#include "ShellSort.h"
 
#define    LENGTH    100
 
using namespace std; 
 
int main()
{
//    FILE *pFile ;
    //auto_ptr
    
    
     
      apFile(pFile);
    
    
    //std::tr1::shared_ptr
    
    
     
      sp(new int(0));
    
    
    //tr1::shared_ptr
    
    
     
      spFile ;
    
    
//    pFile = fopen( ("2.txt"), ("wt"));
//    if(!pFile)
//    {
//        std::cout<<"File open error!/n"<
    
    
//        exit(0);
//    }
    int arr[LENGTH];
    int i =0 ;
    for(i=0;i
   
   

    
    

    
    
    
    {
        arr[i] = rand()%1000;    
        //cout<
      
      
       
       <
       
       
      
      
    }
    
    CSort *pSort ;
    pSort = new CShellSort ;
    pSort->Sort(arr,LENGTH) ;
//    CInsertSort is ;
//    is.DirectInsertSort(arr,LENGTH);
     
    delete pSort ;
    for ( i =0 ;i
     
     
    {
        std::cout<
     
     
      
      <
      
      
     
     
    }
//    for( i=0;i
      
      
//    {
//        fprintf(pFile, ("%d/n"),arr[i]);
//        //cout<
      
      
       
       <
       
       
      
      
//    }
//    fclose(pFile);
 
//    FILE *pFileReader ;
//    pFileReader = fopen("1.txt","rt");
//    if(!pFileReader)
//    {
//        std::cout<<"File open error!/n"<
      
      
//        exit(0);
//    }
 
//    int n =0;
//    for(i=0;i
      
      
//    {
//        fscanf(pFileReader,"%d/n",&n);
//        std::cout<
      
      
       
       <
       
       
      
      
//    }
//    //fclose(pFileReader);
     
    return 0 ;
}
SelectionSort.cpp
// SelectionSort.cpp: implementation of the CSelectionSort class.
//
//
 
#include "SelectionSort.h"
 
//
// Construction/Destruction
//
 
CSelectionSort::CSelectionSort()
{
 
}
 
CSelectionSort::~CSelectionSort()
{
 
}
 
 
void CSelectionSort::Sort (int * arr,const int size) 
{
    if(!arr)
        return ;
    for (int i =0 ;i
   
   

    
    

    
    
    
    {
        int pos= i ;
        for (int j= i;j
     
     

      
      

    
      
      
        {
            if(arr[pos] > arr[j])
                pos = j ;
        }
        int tmp = arr[i];
        arr[i] = arr[pos];
        arr[pos]= tmp ;
    }
}

SelectionSort.h
// SelectionSort.h: interface for the CSelectionSort class.
//
//
 
#if !defined(AFX_SELECTIONSORT_H__99AADAE2_AC7F_49BF_B382_388CD8A9E784__INCLUDED_)
#define AFX_SELECTIONSORT_H__99AADAE2_AC7F_49BF_B382_388CD8A9E784__INCLUDED_
 
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
 
#include "Sort.h"
 
class CSelectionSort : public CSort  
{
public:
    CSelectionSort();
    virtual ~CSelectionSort();
 
    virtual void Sort(int * arr,const int size)  ;
};
 
#endif // !defined(AFX_SELECTIONSORT_H__99AADAE2_AC7F_49BF_B382_388CD8A9E784__INCLUDED_)

ShellSort.cpp
// ShellSort.cpp: implementation of the CShellSort class.
//
//
 
#include "ShellSort.h"
 
//
// Construction/Destruction
//
 
CShellSort::CShellSort()
{
 
}
 
CShellSort::~CShellSort()
{
 
}
void CShellSort::Sort (int * arr,const int size)
{
    if(!arr)
        return ;
    int increment = size ;
    do {
        increment = increment/3 +1;
        ShellPass(arr,size,increment);
    } while(increment>1);
}
void CShellSort::ShellPass(int *arr ,int size ,int delta)
{
    for (int i =delta;i
   
   
    {
        int tmp = arr[i];
        for (int j = i-delta; j>=0 && tmp 
   
   
        {
            arr[j+delta] = arr[j];
        }
        arr[j+delta] = tmp ;
    }
}
Sort.cpp
// Sort.h: interface for the CSort class.
//
//
 
#if !defined(AFX_SORT_H__39C9279B_CB59_4081_B68B_C8D0459C1C1E__INCLUDED_)
#define AFX_SORT_H__39C9279B_CB59_4081_B68B_C8D0459C1C1E__INCLUDED_
 
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
 
class CSort  
{
public:
    CSort();
    virtual ~CSort();
 
    virtual void Sort(int * arr,const int size) =0;
 
};
 
#endif // !defined(AFX_SORT_H__39C9279B_CB59_4081_B68B_C8D0459C1C1E__INCLUDED_)

Sort.h
// Sort.h: interface for the CSort class.
//
//
 
#if !defined(AFX_SORT_H__39C9279B_CB59_4081_B68B_C8D0459C1C1E__INCLUDED_)
#define AFX_SORT_H__39C9279B_CB59_4081_B68B_C8D0459C1C1E__INCLUDED_
 
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
 
class CSort  
{
public:
    CSort();
    virtual ~CSort();
 
    virtual void Sort(int * arr,const int size) =0;
 
};
 
#endif // !defined(AFX_SORT_H__39C9279B_CB59_4081_B68B_C8D0459C1C1E__INCLUDED_)

ShellSort.h
// ShellSort.h: interface for the CShellSort class.
//
//
 
#if !defined(AFX_SHELLSORT_H__77F1EA61_E5FA_42F9_96A0_A718CB82519E__INCLUDED_)
#define AFX_SHELLSORT_H__77F1EA61_E5FA_42F9_96A0_A718CB82519E__INCLUDED_
 
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
 
#include "Sort.h"
 
class CShellSort : public CSort  
{
public:
    CShellSort();
    virtual ~CShellSort();
 
    virtual void Sort(int * arr,const int size) ;
    void ShellPass (int *arr ,int size ,int delta);
};
 
#endif // !defined(AFX_SHELLSORT_H__77F1EA61_E5FA_42F9_96A0_A718CB82519E__INCLUDED_)

QuickSort.h
// QuickSort.h: interface for the CQuickSort class.
//
//
 
#if !defined(AFX_QUICKSORT_H__55ABF9C0_0E66_431F_B3CE_5D63EF7BE1C6__INCLUDED_)
#define AFX_QUICKSORT_H__55ABF9C0_0E66_431F_B3CE_5D63EF7BE1C6__INCLUDED_
 
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
 
#include "Sort.h"
 
class CQuickSort : public CSort  
{
public:
    CQuickSort();
    virtual ~CQuickSort();
 
    virtual void Sort (int * arr,const int size) ;
private :
    int Partition(int * arr,int low ,int high) ;
    void QuickSort(int *arr,int low , int high) ;
};
 
#endif // !defined(AFX_QUICKSORT_H__55ABF9C0_0E66_431F_B3CE_5D63EF7BE1C6__INCLUDED_)

QuickSort.cpp
// QuickSort.cpp: implementation of the CQuickSort class.
//
//
 
#include "QuickSort.h"
 
//
// Construction/Destruction
//
 
CQuickSort::CQuickSort()
{
 
}
 
CQuickSort::~CQuickSort()
{
 
}
 
void CQuickSort::Sort (int * arr,const int size) 
{
    if(!arr)
        return ;
    QuickSort(arr,0,size-1);
}
 
void CQuickSort::QuickSort(int *arr,int low , int high)
{
 
    if (low
   
   
    {
        int pivotloc = Partition(arr,low,high);
        QuickSort(arr,low,pivotloc-1);
        QuickSort(arr,pivotloc+1,high);
    }
}
int CQuickSort::Partition(int * arr,int low ,int high)
{
    int pivot = arr[low];
    while (low
   
   
    {
        
        while(low
   
   
    
    =pivot)
   
   
            high --; 
        arr[low]= arr[high] ;
        while(low
   
   

    
    

    
    
    
            low ++;
        arr[high]= arr[low];
    }
    arr[low] = pivot;
    return low ;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值