七种基本排序实现源码

本文详细介绍了两种基本的排序算法——插入排序和希尔排序,并提供了相应的源代码实现。通过测试,展示了这两种算法在不同场景下的应用和效果。
摘要由CSDN通过智能技术生成

1.【插入排序】

void InsertSort(int* p ,int size)
{
	for(int i=1;i<size;i++)
	{
		int j	=	i;
		int t	= p[i];
		for(; j > 0 && p[j-1] > t; j--)
			p[j] = p[j-1];
		p[j] = t;
	}	
}

2.【选择排序】

void SelectSort(int* arr,int size)
{
	for(int i=0;i<size;i++)
	{
		int psn=i;
		for(int j=i;j<size;j++)
		{
			if(arr[j]<arr[psn])
				psn = j;
		}	
		swap(arr[psn],arr[i]);
	}	
}

3.【冒泡排序】

void BubbleSort(int* arr,int size)
{
	bool cg = true;		
	int  n	= 0;
	do
	{
		cg = false;	
		for (int i=0;i<size-1-n;i++)
		{
			if (arr[i]>arr[i+1])
			{
				cg = true;
				swap(arr[i],arr[i+1]);		
			}
		}	
		n++;
	} while (cg);
}

4.【快速排序】

void QuickSort(int* arr,int size)
{
    int *L,*R;
    if(size <= 1) return;
    if(2 == size)
    {
       if(arr[0] > arr[1])
         swap(arr[0],arr[1]);
       return;
    } 
    L = &arr[1];
    R = &arr[size -1];
    while(L < R)
   {
      while(L<R && *L < *arr) ++L;
      while(R>arr && !(*R < *arr)) --R;
     if(L<R) swap(*R,*L);
   }
   swap(*R,*arr);
   QuickSort(arr, R-arr);
   QuickSort(R+1,size -1 -(R-arr));
}

5.【堆排序】

void Shift(int* heap, int start, int end)
{
       int tmp = heap[start]; 
       int parent = start;
       int child = parent * 2 + 1; 
       while(child <= end)
     {
       if(child < end && 
	         heap[child] < heap[child+1])
                child++;

         if(tmp < heap[child])
        {
             heap[parent] = heap[child];
             parent = child;
             child = 2 * parent +1;
        } else  break;
     }
    heap[parent] = tmp;
}

void BuildHeap(int* heap,int size)
{
      for(int i=size/2-1; i >=0; i--)
         Shift(heap,i,size-1);

}

void HeapSort(int* heap,int size)
{
    BuildHeap(heap,size);
    for(int i = size-1; i>0; i--)
    {
          swap(heap[0],heap[i]);
          Shift(heap,0,i-1);
    }
}

6.【归并排序】

void Merge(int* arr,int first,int mid,int last)
{
     int size = last -first + 1;
     int* tmp = new int[size];
     int   b1 = first;
     int   e1 = mid;
     int   b2 = mid + 1;
     int   e2 = last;
     int    k = 0;
     while(b1<=e1 && b2<=e2)
       if(arr[b1]<arr[b2])
          tmp[k++] = arr[b1++];
       else tmp[k++] = arr[b2++];
    
     while(b1<=e1)   tmp[k++]=arr[b1++];
     while(b2<=e2)   tmp[k++]=arr[b2++];
     for(int i=0; i<size; i++)
        arr[first + i] = tmp[i];
     delete tmp;   
}

void MergeSort(int* arr,int first,int last)
{
    if(first < last)
    {
       int  mid = (first +last) / 2;
       MergeSort(arr,first,mid);
       MergeSort(arr,mid+1,last);
       Merge(arr,first,mid,last);
    }
}


7.【希尔排序】

void ShellSort(int *p, int size)
{
   int iTemp;
   for( int iStep = size/2; iStep > 0; iStep = iStep/2 ) {
     for( int i = iStep; i < size; ++i ) {
	iTemp = p[i];
	int j = i;
	for( ; j >= iStep; j -= iStep ) {   
	  if( iTemp < p[j-iStep] ) {
	    p[j] = p[j-iStep];
	  } else {
            break;
          }
	}
	p[j] = iTemp;
      }
    }
}

【代码测试】

#include<iostream>
#include<string>
using namespace std;

typedef void(*SortFunc)(int*, int, int*, int*);

// 插入排序
void InsertSort(int* p ,int size, int* move_cnt, int* compare_cnt)  
{  
    for(int i=1;i<size;i++)  
    {  
        int j   =   i;  
        int t   = p[i]; 
        ++*move_cnt; 
        ++*compare_cnt;
        for(; j > 0 && p[j-1] > t; j--) { 
            ++*compare_cnt;
            p[j] = p[j-1];  
            ++*move_cnt; 
        }
        p[j] = t;  
        ++*move_cnt; 
    }     
}  

// 希尔排序
void ShellSort(int *p, int size, int* move_cnt, int* compare_cnt)
{
	int iTemp;

	for( int iStep = size/2; iStep > 0; iStep = iStep/2 )
	{
		for( int i = iStep; i < size; ++i )
		{
             
			iTemp = p[i];
            ++*move_cnt;
			int j = i;
			for( ; j >= iStep; j -= iStep )
			{   
                ++*compare_cnt;
				if( iTemp < p[j-iStep] )
				{
					p[j] = p[j-iStep];
					++*move_cnt;
				}
				else
				{
					break;
				}
			}
			p[j] = iTemp;
			++*move_cnt;
		}
	}
}

// 冒泡排序
void BubbleSort(int* arr,int size, int* move_cnt, int* compare_cnt)  
{  
    bool cg = true;       
    int  n  = 0;  
    do  
    {  
        cg = false;   
        for (int i=0;i<size-1-n;i++)  
        {   
            ++*compare_cnt;
            if (arr[i]>arr[i+1])  
            {  
                cg = true;  
                swap(arr[i],arr[i+1]);     
                *move_cnt += 3;   
            }  
        }     
        n++;  
    } while (cg);  
}  

// 快速排序
void QuickSort(int* arr,int size, int* move_cnt, int* compare_cnt)  
{  
    int *L,*R;  
    if(size <= 1) return;  
    if(2 == size)  
    {  
       ++*compare_cnt;
       if(arr[0] > arr[1]) { 
         swap(arr[0],arr[1]);
         *move_cnt += 3; 
       }  
       return;  
    }   
    L = &arr[1];  
    R = &arr[size -1];  
    while(L < R)  
   {  
      ++*compare_cnt;
      while(L<R && *L < *arr) {
        ++*compare_cnt;
        ++L;
      }  
      ++*compare_cnt;
      while(R>arr && !(*R < *arr)) {
         ++*compare_cnt;
         --R; 
      } 
     ++*compare_cnt;
     if(L<R){
       swap(*R,*L);
       *move_cnt += 3; 
     }  
   }  
   swap(*R,*arr);  
   *move_cnt += 3; 
   QuickSort(arr, R-arr, move_cnt, compare_cnt);  
   QuickSort(R+1,size -1 -(R-arr), move_cnt, compare_cnt);  
}  

// 简单选择排序
void SelectSort(int* arr,int size, int* move_cnt, int* compare_cnt)  
{  
    for(int i=0;i<size;i++)  
    {  
        int psn=i;  
        for(int j=i;j<size;j++)  
        {    
            ++*compare_cnt;
            if(arr[j]<arr[psn]) {  
               psn = j;  
            }
        }     
        swap(arr[psn],arr[i]);  
        *move_cnt += 3; 
    }     
}  

void Shift(int* heap, int start, int end, int* move_cnt, int* compare_cnt)  
{  
       int tmp = heap[start];   
       ++*move_cnt;
       int parent = start;  
       int child = parent * 2 + 1;   
       while(child <= end)  
     {  
       ++*compare_cnt;
       if(child < end && heap[child] < heap[child+1]) { 
         ++child;  
       }
       ++*compare_cnt;
       if(tmp < heap[child])  
       {  
             heap[parent] = heap[child];  
             ++*move_cnt;
             parent = child;  
             child = 2 * parent +1;  
       } else  break;  
     }  
    heap[parent] = tmp;  
    ++*move_cnt;
}  
  
void BuildHeap(int* heap,int size, int* move_cnt, int* compare_cnt)  
{  
      for(int i=size/2-1; i >=0; i--)  
         Shift(heap,i,size-1, move_cnt, compare_cnt);  
  
}  
  
void HeapSort(int* heap, int size, int* move_cnt, int* compare_cnt)  
{  
    BuildHeap(heap, size, move_cnt, compare_cnt);  
    for(int i = size-1; i>0; i--)  
    {  
          swap(heap[0],heap[i]);  
          Shift(heap,0,i-1, move_cnt, compare_cnt);  
    }  
}  

void Merge(int* arr,int first,int mid,int last, int* move_cnt, int* compare_cnt)  
{  
     int size = last -first + 1;  
     int* tmp = new int[size];  
     int   b1 = first;  
     int   e1 = mid;  
     int   b2 = mid + 1;  
     int   e2 = last;  
     int    k = 0;  

     while(b1<=e1 && b2<=e2) { 
       ++*compare_cnt;
       if(arr[b1]<arr[b2])  
          tmp[k++] = arr[b1++];  
       else tmp[k++] = arr[b2++];  
       ++*move_cnt;
     }
     while(b1<=e1) {  
       tmp[k++]=arr[b1++];
       ++*move_cnt;  
       ++*compare_cnt;
     }
     while(b2<=e2) { 
       tmp[k++]=arr[b2++];
       ++*move_cnt;
       ++*compare_cnt;
     }  
     for(int i=0; i<size; i++)  { 
        arr[first + i] = tmp[i]; 
        ++*move_cnt;
     } 
     delete tmp;     
}  
  
void MergeSort(int* arr,int size, int* move_cnt, int* compare_cnt)  
{  
    if(size > 1)  
    {  
       int  mid = size / 2;  
       MergeSort(arr, mid, move_cnt, compare_cnt);  
       MergeSort(arr + mid, size - mid, move_cnt, compare_cnt);
       Merge(arr, 0, mid - 1, size -1, move_cnt, compare_cnt);  
    }  
}  

// 输出数组
void ShowArray(int * p, int size)
{
	for(int i = 0; i < size; i++)
	{
		cout<< p[i];
		if(i < size-1)
		{
			cout<< ", ";
		}
	}
	cout<< endl;
}

void CopyToTmp(int* arr, int* temp, int length) {
   	for( int i = 0; i < length; ++i )
	{
		temp[i] = arr[i];
		
	} 
}

int main()
{
	int arr[4][20] = { {-20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100},
	               	   {100, 99, 96, 93,89, 87, 82, 79, 75, 65, 62, 51, 41, 24, 18, 10, 0, -10, -15, -20},
		               {99, 75, -10, 87, 41,100, 89, 65, 18, 93, 0, 51, 62, 10, -20, 82, 24, 96, -15, 79},
	                   {0} };
    const string arr_name[3] = {"顺序的数组" ,"逆序的数组", "随机的数组"};
    
    SortFunc sort_functions[7] = {InsertSort, SelectSort, BubbleSort, QuickSort, ShellSort, HeapSort, MergeSort };
    const string sort_func_name[7] = {"插入排序", "选择排序", "冒泡排序", "快速排序", "希尔排序", "堆排序", "归并排序" };
    int compare_cnt = 0, move_cnt = 0;
    cout <<  "========================================================" << endl;
    for (int i = 0; i < 7; ++i) {
        cout << "         **** " << sort_func_name[i] << " ****" << "       测试结果如下:" << endl ;
        cout <<  "========================================================" << endl;
      for (int j = 0; j < 3; ++j) {
          compare_cnt = 0;
          move_cnt = 0;
          CopyToTmp(arr[j], arr[3], 20);
          cout << endl << arr_name[j] << ":"; 
          ShowArray(arr[3], 20);
          sort_functions[i](arr[3], 20, &move_cnt, &compare_cnt);
          cout << sort_func_name[i] << "后: " ; 
          ShowArray(arr[3], 20);
          cout << sort_func_name[i] << "对" << arr_name[j] << "排序,共进行 " << compare_cnt << " 次比较 和 " << move_cnt << " 次移动" << endl; 
      }    
    cout << endl << "========================================================" << endl;
    } 
	system("pause");
	return 0;
}


【测试结果】

========================================================
         **** 插入排序 ****       测试结果如下:
========================================================


顺序的数组:-20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100
插入排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100
插入排序对顺序的数组排序,共进行 19 次比较 和 38 次移动


逆序的数组:100, 99, 96, 93, 89, 87, 82, 79, 75, 65, 62, 51, 41, 24, 18, 10, 0, -10, -15, -20
插入排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100
插入排序对逆序的数组排序,共进行 209 次比较 和 228 次移动


随机的数组:99, 75, -10, 87, 41, 100, 89, 65, 18, 93, 0, 51, 62, 10, -20, 82, 24, 96, -15, 79
插入排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100
插入排序对随机的数组排序,共进行 132 次比较 和 151 次移动


========================================================
         **** 选择排序 ****       测试结果如下:
========================================================


顺序的数组:-20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100
选择排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100
选择排序对顺序的数组排序,共进行 210 次比较 和 60 次移动


逆序的数组:100, 99, 96, 93, 89, 87, 82, 79, 75, 65, 62, 51, 41, 24, 18, 10, 0, -10, -15, -20
选择排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100
选择排序对逆序的数组排序,共进行 210 次比较 和 60 次移动


随机的数组:99, 75, -10, 87, 41, 100, 89, 65, 18, 93, 0, 51, 62, 10, -20, 82, 24, 96, -15, 79
选择排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100
选择排序对随机的数组排序,共进行 210 次比较 和 60 次移动


========================================================
         **** 冒泡排序 ****       测试结果如下:
========================================================


顺序的数组:-20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100
冒泡排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100
冒泡排序对顺序的数组排序,共进行 19 次比较 和 0 次移动


逆序的数组:100, 99, 96, 93, 89, 87, 82, 79, 75, 65, 62, 51, 41, 24, 18, 10, 0, -10, -15, -20
冒泡排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100
冒泡排序对逆序的数组排序,共进行 190 次比较 和 570 次移动


随机的数组:99, 75, -10, 87, 41, 100, 89, 65, 18, 93, 0, 51, 62, 10, -20, 82, 24, 96, -15, 79
冒泡排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100
冒泡排序对随机的数组排序,共进行 189 次比较 和 339 次移动


========================================================
         **** 快速排序 ****       测试结果如下:
========================================================


顺序的数组:-20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100
快速排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100
快速排序对顺序的数组排序,共进行 244 次比较 和 54 次移动


逆序的数组:100, 99, 96, 93, 89, 87, 82, 79, 75, 65, 62, 51, 41, 24, 18, 10, 0, -10, -15, -20
快速排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100
快速排序对逆序的数组排序,共进行 235 次比较 和 57 次移动


随机的数组:99, 75, -10, 87, 41, 100, 89, 65, 18, 93, 0, 51, 62, 10, -20, 82, 24, 96, -15, 79
快速排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100
快速排序对随机的数组排序,共进行 140 次比较 和 54 次移动


========================================================
         **** 希尔排序 ****       测试结果如下:
========================================================


顺序的数组:-20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100
希尔排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100
希尔排序对顺序的数组排序,共进行 62 次比较 和 124 次移动


逆序的数组:100, 99, 96, 93, 89, 87, 82, 79, 75, 65, 62, 51, 41, 24, 18, 10, 0, -10, -15, -20
希尔排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100
希尔排序对逆序的数组排序,共进行 80 次比较 和 160 次移动


随机的数组:99, 75, -10, 87, 41, 100, 89, 65, 18, 93, 0, 51, 62, 10, -20, 82, 24, 96, -15, 79
希尔排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100
希尔排序对随机的数组排序,共进行 88 次比较 和 163 次移动


========================================================
         **** 堆排序 ****       测试结果如下:
========================================================


顺序的数组:-20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100
堆排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100
堆排序对顺序的数组排序,共进行 126 次比较 和 119 次移动


逆序的数组:100, 99, 96, 93, 89, 87, 82, 79, 75, 65, 62, 51, 41, 24, 18, 10, 0, -10, -15, -20
堆排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100
堆排序对逆序的数组排序,共进行 108 次比较 和 101 次移动


随机的数组:99, 75, -10, 87, 41, 100, 89, 65, 18, 93, 0, 51, 62, 10, -20, 82, 24, 96, -15, 79
堆排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100
堆排序对随机的数组排序,共进行 118 次比较 和 108 次移动


========================================================
         **** 归并排序 ****       测试结果如下:
========================================================


顺序的数组:-20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100
归并排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100
归并排序对顺序的数组排序,共进行 88 次比较 和 176 次移动


逆序的数组:100, 99, 96, 93, 89, 87, 82, 79, 75, 65, 62, 51, 41, 24, 18, 10, 0, -10, -15, -20
归并排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100
归并排序对逆序的数组排序,共进行 88 次比较 和 176 次移动


随机的数组:99, 75, -10, 87, 41, 100, 89, 65, 18, 93, 0, 51, 62, 10, -20, 82, 24, 96, -15, 79
归并排序后: -20, -15, -10, 0, 10, 18, 24, 41, 51, 62, 65, 75, 79, 82, 87, 89, 93, 96, 99, 100
归并排序对随机的数组排序,共进行 88 次比较 和 176 次移动



            原创文章,转载请注明: 转载自 IIcyZhao’s Road

          本文链接地址:http://blog.csdn.net/iicy266/article/details/11905851


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值