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

BubbleSort.cpp ~ 657B 下载(216)


01// BubbleSort.cpp: implementation of the CBubbleSort class.

05#include "BubbleSort.h"
06
07//
08// Construction/Destruction
09//
10
11CBubbleSort::CBubbleSort()
12{
13
14}
15
16CBubbleSort::~CBubbleSort()
17{
18
19}
20
21void CBubbleSort::Sort (int * arr,const int size)
22{
23int tmp =0 ;
24for (int i =0 ;i<size ;i++)
25for (int j=i+1;j<size ;j++)
26{
27if(arr[i]>arr[j])
28{
29tmp =arr[i];
30arr[i] = arr[j];
31arr[j] = tmp ;
32}
33}
34}

[文件] BubbleSort.h ~ 613B 下载(167)


01// BubbleSort.h: interface for the CBubbleSort class.
02//
03//
04
05#if !defined(AFX_BUBBLESORT_H__2A6E4581_4C19_4061_BD68_9FA2DD286EB5__INCLUDED_)
06#define AFX_BUBBLESORT_H__2A6E4581_4C19_4061_BD68_9FA2DD286EB5__INCLUDED_
07
08#if _MSC_VER > 1000
09#pragma once
10#endif // _MSC_VER > 1000
11
12#include "Sort.h"
13
14class CBubbleSort : public CSort
15{
16public:
17CBubbleSort();
18virtual ~CBubbleSort();
19
20virtual void Sort(int * arr,const int size) ;
21
22};
23
24#endif // !defined(AFX_BUBBLESORT_H__2A6E4581_4C19_4061_BD68_9FA2DD286EB5__INCLUDED_)

[文件] InsertSort.h ~ 682B 下载(149)


01// InsertSort.h: interface for the CInsertSort class.
02//
03//
04
05#if !defined(AFX_INSERTSORT_H__FF5699F7_8D6A_4709_93F1_97CDC299528A__INCLUDED_)
06#define AFX_INSERTSORT_H__FF5699F7_8D6A_4709_93F1_97CDC299528A__INCLUDED_
07
08#if _MSC_VER > 1000
09#pragma once
10#endif // _MSC_VER > 1000
11
12#include "Sort.h"
13#include <stdlib.h>
14
15class CInsertSort : public CSort
16{
17public:
18CInsertSort();
19virtual ~CInsertSort();
20
21virtual void Sort (int * arr,const int size) ;
22void DirectInsertSort(int *arr,const int size);
23};
24
25#endif // !defined(AFX_INSERTSORT_H__FF5699F7_8D6A_4709_93F1_97CDC299528A__INCLUDED_)

[文件] InsertSort.cpp ~ 1KB 下载(98)


01// InsertSort.cpp: implementation of the CInsertSort class.
02//
03//
04
05#include "InsertSort.h"
06
07//
08// Construction/Destruction
09//
10
11CInsertSort::CInsertSort()
12{
13
14}
15
16CInsertSort::~CInsertSort()
17{
18
19}
20/*
21if (mid != high&& mid != low)
22{
23exit(0);
24}
25*/
26
27
28//���ֲ�������
29void CInsertSort::Sort (int * arr,const int size)
30{
31if(!arr)
32return ;
33//
34for (int i =1 ;i<size ;i++)
35{
36int tmp = arr[i];

38int low =0,high = i-1 ,mid ;
39while (low<= high)
40{
41mid= (low+high)/2;
42if(arr[i]<arr[mid])
43high = mid-1;
44else
45low = mid +1 ;
46}
47//high ָ��tmpӦ�����ǰһλ�ã�lowֵΪhigh+1
48for (int j =i-1 ;j>=low;j--)
49{
50arr[j+1]= arr[j];
51}
52arr[low]= tmp ;

57void CInsertSort::DirectInsertSort(int *arr,int size)
58{
59if(!arr)
60return ;
61for (int i=1;i<size ;i++)
62{
63int tmp = arr[i];
64for (int j = i-1; j>=0 && tmp < arr[j];j--)
65{
66arr[j+1] = arr[j];
67}
68arr[j+1] =tmp ;
69
70}
71}

[文件] Main.cpp ~ 1KB 下载(149)

01#include <iostream>
02#include <stdlib.h>
03#include <memory>
04#include "BubbleSort.h"
05#include "InsertSort.h"
06#include "QuickSort.h"
07#include "SelectionSort.h"
08#include "ShellSort.h"
09
10#define LENGTH 100
11
12using namespace std;
13
14int main()
15{
16// FILE *pFile ;
17//auto_ptr<FILE> apFile(pFile);
18//std::tr1::shared_ptr<int> sp(new int(0));
19//tr1::shared_ptr<FILE> spFile ;
20// pFile = fopen( ("2.txt"), ("wt"));
21// if(!pFile)
22// {
23// std::cout<<"File open error!\n"<<std::endl;
24// exit(0);
25// }
26int arr[LENGTH];
27int i =0 ;
28for(i=0;i<LENGTH ;i++)
29{
30arr[i] = rand()%1000;
31//cout<<arr[i]<<endl;
32}
33
34CSort *pSort ;
35pSort = new CShellSort ;
36pSort->Sort(arr,LENGTH) ;
37// CInsertSort is ;
38// is.DirectInsertSort(arr,LENGTH);
39
40delete pSort ;
41for ( i =0 ;i<LENGTH;i++)
42{
43std::cout<<arr[i]<<std::endl;
44}
45// for( i=0;i<LENGTH ;i++)
46// {
47// fprintf(pFile, ("%d\n"),arr[i]);
48// //cout<<arr[i]<<endl;
49// }
50// fclose(pFile);
51
52// FILE *pFileReader ;
53// pFileReader = fopen("1.txt","rt");
54// if(!pFileReader)
55// {
56// std::cout<<"File open error!\n"<<std::endl;
57// exit(0);
58// }
59
60// int n =0;
61// for(i=0;i<LENGTH;i++)
62// {
63// fscanf(pFileReader,"%d\n",&n);
64// std::cout<<n<<std::endl;
65// }
66// //fclose(pFileReader);
67
68return 0 ;
69}

[文件] SelectionSort.cpp ~ 776B 下载(148)

01// SelectionSort.cpp: implementation of the CSelectionSort class.
02//
03//
04
05#include "SelectionSort.h"
06
07//
08// Construction/Destruction
09//
10
11CSelectionSort::CSelectionSort()
12{
13
14}
15
16CSelectionSort::~CSelectionSort()
17{
18
19}
20
21//����������ѡ��С�ģ�������������棬�Ӷ��������+1
22void CSelectionSort::Sort (int * arr,const int size)
23{
24if(!arr)
25return ;
26for (int i =0 ;i<size ;i++)
27{
28int pos= i ;
29for (int j= i;j<size ;j++)
30{
31if(arr[pos] > arr[j])
32pos = j ;
33}
34int tmp = arr[i];
35arr[i] = arr[pos];
36arr[pos]= tmp ;
37}
38}

[文件] SelectionSort.h ~ 635B 下载(151)


01// SelectionSort.h: interface for the CSelectionSort class.
02//
03//
04
05#if !defined(AFX_SELECTIONSORT_H__99AADAE2_AC7F_49BF_B382_388CD8A9E784__INCLUDED_)
06#define AFX_SELECTIONSORT_H__99AADAE2_AC7F_49BF_B382_388CD8A9E784__INCLUDED_
07
08#if _MSC_VER > 1000
09#pragma once
10#endif // _MSC_VER > 1000
11
12#include "Sort.h"
13
14class CSelectionSort : public CSort
15{
16public:
17CSelectionSort();
18virtual ~CSelectionSort();
19
20virtual void Sort(int * arr,const int size) ;
21};
22
23#endif // !defined(AFX_SELECTIONSORT_H__99AADAE2_AC7F_49BF_B382_388CD8A9E784__INCLUDED_)

[文件] Sort.cpp ~ 374B 下载(141)


01// Sort.cpp: implementation of the CSort class.
02//
03//
04
05#include "Sort.h"
06
07//
08// Construction/Destruction
09//
10
11CSort::CSort()
12{
13
14}
15
16CSort::~CSort()
17{
18
19}

[文件] Sort.h ~ 530B 下载(144)


01// Sort.h: interface for the CSort class.
02//
03//
04
05#if !defined(AFX_SORT_H__39C9279B_CB59_4081_B68B_C8D0459C1C1E__INCLUDED_)
06#define AFX_SORT_H__39C9279B_CB59_4081_B68B_C8D0459C1C1E__INCLUDED_
07
08#if _MSC_VER > 1000
09#pragma once
10#endif // _MSC_VER > 1000
11
12class CSort
13{
14public:
15CSort();
16virtual ~CSort();
17
18virtual void Sort(int * arr,const int size) =0;
19
20};
21
22#endif // !defined(AFX_SORT_H__39C9279B_CB59_4081_B68B_C8D0459C1C1E__INCLUDED_)

[文件] ShellSort.h ~ 652B 下载(145)


01// ShellSort.h: interface for the CShellSort class.
02//
03//
04
05#if !defined(AFX_SHELLSORT_H__77F1EA61_E5FA_42F9_96A0_A718CB82519E__INCLUDED_)
06#define AFX_SHELLSORT_H__77F1EA61_E5FA_42F9_96A0_A718CB82519E__INCLUDED_
07
08#if _MSC_VER > 1000
09#pragma once
10#endif // _MSC_VER > 1000
11
12#include "Sort.h"
13
14class CShellSort : public CSort
15{
16public:
17CShellSort();
18virtual ~CShellSort();
19
20virtual void Sort(int * arr,const int size) ;
21void ShellPass (int *arr ,int size ,int delta);
22};
23
24#endif // !defined(AFX_SHELLSORT_H__77F1EA61_E5FA_42F9_96A0_A718CB82519E__INCLUDED_)

[文件] QuickSort.h ~ 709B 下载(129)


01// QuickSort.h: interface for the CQuickSort class.
02//
03//
04
05#if !defined(AFX_QUICKSORT_H__55ABF9C0_0E66_431F_B3CE_5D63EF7BE1C6__INCLUDED_)
06#define AFX_QUICKSORT_H__55ABF9C0_0E66_431F_B3CE_5D63EF7BE1C6__INCLUDED_
07
08#if _MSC_VER > 1000
09#pragma once
10#endif // _MSC_VER > 1000
11
12#include "Sort.h"
13
14class CQuickSort : public CSort
15{
16public:
17CQuickSort();
18virtual ~CQuickSort();
19
20virtual void Sort (int * arr,const int size) ;
21private :
22int Partition(int * arr,int low ,int high) ;
23void QuickSort(int *arr,int low , int high) ;
24};
25
26#endif // !defined(AFX_QUICKSORT_H__55ABF9C0_0E66_431F_B3CE_5D63EF7BE1C6__INCLUDED_)

[文件] QuickSort.cpp ~ 1KB 下载(123)


01// QuickSort.cpp: implementation of the CQuickSort class.
02//
03//
04
05#include "QuickSort.h"
06
07//
08// Construction/Destruction
09//
10
11CQuickSort::CQuickSort()
12{
13
14}
15
16CQuickSort::~CQuickSort()
17{
18
19}
20
21void CQuickSort::Sort (int * arr,const int size)
22{
23if(!arr)
24return ;
25QuickSort(arr,0,size-1);
26}
27
28void CQuickSort::QuickSort(int *arr,int low , int high)
29{
30
31if (low<high)
32{
33int pivotloc = Partition(arr,low,high);
34QuickSort(arr,low,pivotloc-1);
35QuickSort(arr,pivotloc+1,high);
36}
37}
38int CQuickSort::Partition(int * arr,int low ,int high)
39{
40int pivot = arr[low];
41while (low<high)
42{
43
44while(low<high && arr[high]>=pivot)
45high --;
46arr[low]= arr[high] ;
47while(low<high && arr[low]<=pivot)
48low ++;
49arr[high]= arr[low];
50}
51arr[low] = pivot;
52return low ;
53}

[文件] ShellSort.cpp ~ 851B 下载(99)


01// ShellSort.cpp: implementation of the CShellSort class.
02//
03//
04
05#include "ShellSort.h"
06
07//
08// Construction/Destruction
09//
10
11CShellSort::CShellSort()
12{
13
14}
15
16CShellSort::~CShellSort()
17{
18
19}
20void CShellSort::Sort (int * arr,const int size)
21{
22if(!arr)
23return ;
24int increment = size ;
25do {
26increment = increment/3 +1;
27ShellPass(arr,size,increment);
28} while(increment>1);
29}
30void CShellSort::ShellPass(int *arr ,int size ,int delta)
31{
32for (int i = delta;i<size;i++)
33{
34int tmp = arr[i];
35for (int j = i-delta; j>=0 && tmp < arr[j];j-=delta)
36{
37arr[j+delta] = arr[j];
38}
39arr[j+delta] = tmp ;
40}
41}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值