您的位置:首页 > 其它

排序算法(个人总结)

2013-08-02 19:37 281 查看
最近一时兴起,写了一下排序算法:粘上代码和大家共同学习!

Sort.h

/*

(c) Copyright wangzhe All rights reserved.

*/

#pragma once

#include <stdio.h>

#include <tchar.h>

#include"iostream"

using namespace std;

template<class T>

class Sort

{

public:

Sort( T* in_Arry ,size_t in_size )

{

m_pArry = in_Arry;

m_size = in_size ;

}

virtual ~Sort()

{

m_pArry = NULL;

m_size = 0;

}

protected:

void swap(T &a , T &b)

{

T temp = a;

a = b;

b= temp;

}

void HeapSortAnyOne( size_t TempSize,int Location ) //////////////就对Location位置进行调整堆,这是局部的调整

{

int max;

int k = Location;

int j = 2*k+1;

while( j < TempSize )

{

if( j < TempSize-1 && m_pArry[j] < m_pArry[j+1] )

{

j++;

}

if( m_pArry[k] < m_pArry[j] )

{

swap( m_pArry[k],m_pArry[j]);

k = j;

j = 2*k + 1;

}

else

{

break;

}

}

}

public:

void SelectSort() //选择排序

{

for( int i = 0; i < m_size;i++ )

{

int j = i;

T min =m_pArry[j];

int flag = j;

while( j < m_size )

{

if( m_pArry[j] < min )

{

min = m_pArry[j];

flag = j;

}

j++;

}

swap(m_pArry[i],m_pArry[flag]);

}

}

void HeapSort() //堆排序

{

for( int i = m_size/2 -1;i >= 0;i--)

HeapSortAnyOne(m_size,i);

cout<< "HeapSortAnyOne all"<<endl;

for ( int i = 0; i < m_size; i++ )

{

cout<< m_pArry[i]<<endl;

}

for( int j = m_size -1;j >= 0;j--)

{

swap( m_pArry[j],m_pArry[0]);

HeapSortAnyOne(j,0);

}

}

void ExChangeSort()//交换排序

{

for( int i = m_size;i>0;i-- )

{

for(int j = 0;j<i-1;j++)

{

if( m_pArry[j] > m_pArry[j+1] )

swap(m_pArry[j],m_pArry[j+1]);

}

}

}

void QuickSort( size_t iLow ,size_t iHigh ) //快速排序

{

int low , high;

low = iLow;

high = iHigh;

T obTemp = m_pArry[low];

int iGuard = low;

while( low < high )

{

while( low < high && obTemp <= m_pArry[high] )

{

high--;

}

if( low < high )

{

m_pArry[iGuard] = m_pArry[high];

iGuard = high;

}

while( low < high && obTemp >= m_pArry[low] )

{

low++;

}

if( low < high )

{

m_pArry[iGuard] = m_pArry[low];

iGuard = low;

}

}

m_pArry[iGuard] = obTemp;

if( iLow < iHigh && iGuard > iLow)

{

QuickSort( iLow ,iGuard-1 );

}

if( iLow < iHigh && iGuard < iHigh)

{

QuickSort( iGuard+1 ,iHigh );

}

}

void InsertSort()//插入排序

{

for(int i = 0;i< m_size ;i++)

{

int j = i;

T temp = m_pArry[j];

while( j > 0 )

{

if( temp < m_pArry[j-1])

{

m_pArry[j] = m_pArry[j-1];

j--;

}

else

{

break;

}

}

m_pArry[j] = temp;

}

}

void ShellSort(int gaps )//希尔排序

{

for(int k = gaps; k > 0; k--)

{

int ifirst = 0;

while( ifirst < k )

{

for(int i = ifirst;i < m_size ;i +=k )

{

int j = i;

T Temp = m_pArry[j] ;

while( j >= k )

{

if(Temp < m_pArry[j-k])

{

m_pArry[j] = m_pArry[j-k];

j-=k;

}

else

{

break;

}

}

m_pArry[j] = Temp;

}

ifirst++;

}

}

}

int GetSize()

{

return m_size;

}

protected:

T *m_pArry;

size_t m_size;

};

Sort.cpp

/*

(c) Copyright wangzhe All rights reserved.

*/

#include"Sort.h"

int main( int agrc,char*agrv[])

{

int temp[] = {7,9,4,6,3,2,8,1,4,3};

typedef struct sT

{

int key;

char * ptr;

bool& operator< (const sT &st)

{

bool bFlag = false;

if( this->key < st.key )

bFlag = true;

return bFlag;

}

}STO;

STO ArrySt[] = {{7,"777"},{9,"999"},{4,"444"},{6,"666"}};

for ( int i = 0; i < sizeof(ArrySt)/sizeof(STO); i++ )

{

cout<< ArrySt[i].key<<": "<<ArrySt[i].ptr<<endl;

}

/*for ( int i = 0; i < sizeof(temp)/sizeof(int); i++ )

{

cout<< temp[i]<<endl;

}*/

Sort<STO> oSort(ArrySt,4);

//选择排序

//oSort.SelectSort();

//oSort.HeapSort();

//

//交换排序

//oSort.ExChangeSort();

//oSort.QuickSort(0,9);

//

//插入排序

//oSort.InsertSort();

oSort.ShellSort(oSort.GetSize()/2);

//

cout<< "AfterSort"<<endl;

/*for ( int i = 0; i < sizeof(temp)/sizeof(int); i++ )

{

cout<< temp[i]<<endl;

}*/

for ( int i = 0; i < sizeof(ArrySt)/sizeof(STO); i++ )

{

cout<< ArrySt[i].key<<": "<<ArrySt[i].ptr<<endl;

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: