排序法总结

首先记录一些基本的概念。

1.排序方法的稳定性。
假设i<j,且在排序前的序列中Ri领先于Rj,则称所用的排序方法是稳定的;
反之,若可能使排序后的序列中Rj领先于Ri,则称所用的排序方法是不稳
定的。
(这是严蔚敏书上的原话,怎么感觉像是什么都没说一样。)

2.内部排序。
待排序记录在存放在计算机随机存储器中进行的排序过程。

3.外部排序。
待排序记录数量很大,以致内存一次不能容纳全部记录。在排序过程中尚需
对外存进行访问的排序过程。


/*书上的直接插入排序。*/  
#include <iostream>  
using namespace std;  
void DirectInsertSort(int ForSort[])  
{  
    int i, j;  
    int temp;  
  
    for(i = 1; i < 10; i++)  
    {  
        j = i;  
        temp = ForSort[i];  
        while (j > 0 && temp < ForSort[j - 1])  
        {  
            ForSort[j] = ForSort[j - 1];  
            j --;  
        }  
        ForSort[j] = temp;  
    }  
    for(i = 0; i < 10; i++)  
        cout << ForSort[i] << endl;  
}  
void main()  
{  
    cout << "请输入十个数" << endl;  
    int Sort[10];  
    for (int ii = 0; ii < 10; ii++)  
    {  
        cin >> Sort[ii];  
    }  
    printf("\n");  
    DirectInsertSort(Sort);  
}  

/*我自己写的直接插入排序,明显没书上的好,但功能上是那个意思,呵呵*/
#include <iostream>
using namespace std;
void DirectInsertionSort(int Sort[], int n)
{
	int j, s;
	int temp;
	for(int i = 0; i < n; i++)
	{
		for(j = 0; j < i; j++)
		{
			if(Sort[i] <= Sort[j])
			{
				temp = Sort[i];
				for(s = i; s >= j; s--)
					Sort[s] = Sort[s - 1];
				Sort[j] = temp;
			}
		}
	}
    for(i = 0; i < 10; i++)  
        cout << Sort[i] << endl;
}

void main()  
{  
    cout << "请输入十个数" << endl;  
    int ForSort[10];  
    for (int ii = 0; ii < 10; ii++)  
    {  
        cin >> ForSort[ii];  
    }  
    printf("\n");  
    DirectInsertionSort(ForSort,10);  
}  

/*书上写的折半插入排序,在我看来这个程序非常糟糕,虽然很简洁。但其命名方
式实在让人不屑。为什么不取low,high这样的意义明显的名字呢?却取k,r这样的名字。*/  
#include <iostream>  
using namespace std;  
void BinaryInsertionSort(int ForSort[], int n)  
{  
    int i, k, r;  
    int temp;  
    for(i = 1; i < n; i++)  
    {  
        temp = ForSort[i];  
        k = 0;  
        r = i - 1;  
        while(k <= r)  
        {  
            int m;  
            m = (k + r) / 2;  
            if(temp < ForSort[m])  
                r = m - 1;  
            else  
                k = m + 1;  
        }  
        for(r = i; r > k; r --)  
            ForSort[r] = ForSort[r - 1];  
        ForSort[k] = temp;  
    }  
    for(i = 0; i < n; i++)  
        cout << ForSort[i] << endl;  
}  
void main()  
{  
    cout << "请输入十个数" << endl;  
    int Sort[10];  
    for (int ii = 0; ii < 10; ii++)  
    {  
        cin >> Sort[ii];  
    }  
    printf("\n");  
    BinaryInsertionSort(Sort,10);  
}  

/*我自己写的折半插入排序算法实现程序,在我看来还可以,至少完美实现了功能和算法的思想。*/
#include <iostream>
using namespace std;
void BinaryInsertionSort(int Sort[], int n)
{
	int between;
	int high,low;
	int s;
	int temp;
	for(int i = 1; i < n; i++)
	{
		temp = Sort[i];
		low = 0;
		high = i - 1;
         between = 0.5*(low + high);
		while (low < high)
		{
			if(Sort[i] < Sort[between])
			{   
			    high = between - 1;
			    between = 0.5*(low + high); 
			}
			else
			{   
				low = between + 1;  
				between = 0.5*(low + high); 
			}
		}
		if(Sort[i] > Sort[between]) //找到要插入的位置
		{
			for(s = i; s > between+1; s--)
				Sort[s] = Sort[s - 1];
			Sort[between + 1] = temp;
		}
		else
		{
			for(s = i; s > between; s--)
				Sort[s] = Sort[s - 1];
	     	Sort[between] = temp;
		}
	}
    for(i = 0; i < 10; i++)  
        cout << Sort[i] << endl;
}

void main()  
{  
    cout << "请输入十个数" << endl;  
    int ForSort[10];  
    for (int ii = 0; ii < 10; ii++)  
    {  
        cin >> ForSort[ii];  
    }  
    printf("\n");  
    BinaryInsertionSort(ForSort,10);  
}  

/*书上的希尔排序*/    
#include <iostream>    
using namespace std;    
void SellSort(int ForSort[], int n, int s)    
{    
    int i, j, k;    
    int temp;    
    /*分组排序,初始增量为s,每循环一次增量减半,直到增量为0时结束*/  
    for(k = s; k > 0; k --)  
    {  
        for(i = k; i < n; i++)  
        {  
            temp = ForSort[i];  
            j = i - k;  
            /*组内排序,将temp直接插入组内合适的记录位置*/  
            while(j >= 0 && temp < ForSort[j])  
            {  
                ForSort[j + k] = ForSort[j];  
                j -= k;  
            }  
            ForSort[j + k] = temp;  
        }  
    }  
    for(i = 0; i < n; i++)    
        cout << ForSort[i] << endl;   
}    
void main()    
{    
    cout << "请输入十个数" << endl;    
    int Sort[10];    
    for (int ii = 0; ii < 10; ii++)    
    {    
        cin >> Sort[ii];    
    }    
    printf("\n");    
    SellSort(Sort,10,5);    //初始增量设为5  
}    

/*别人奉献的*/
#include <iostream>        
using namespace std;        
void SellSort(int a[], int M)
{
    int i,j,d;
    int tmp;
     
    for(d = M/2 ; d >= 1; d /= 2) //对记录分组排序
    {
        for( i = d; i <= M; i++) //对每一组记录排序
        {
            tmp = a[i];
            for(j = i - d; j >= 0 && tmp < a[j]; j -= d)
                a[j+d] = a[j];    
            a[j+d] = tmp;
        }
    }
    for(int ii = 0; ii < M; ii++)        
        cout << a[ii] << endl;
}  
   
       
void main()        
{        
    cout << "请输入十个数" << endl;        
    int ForSort[10];        
    for (int ii = 0; ii < 10; ii++)        
    {        
        cin >> ForSort[ii];        
    }        
    printf("\n");        
    SellSort(ForSort,10);    //初始增量设为5      
}      

/*我写的希尔排序,希尔排序就是分成组以后排序(按增量分组,增量要每次折半)
每组内采用直接插入排序,主要思想就是这样……*/
      
#include <iostream>      
using namespace std;      
void SellSort(int sort[], int length)      
{      
	int distance;              //增量
	int group_sort;            //分组数
	int group_number;        //组内元素数
    int i, j;                  //直接插入排序时使用
	int temp;                  //交换时使用
	for(distance = length / 2; distance > 0; distance = distance / 2)  
	{                                        //增量每次折半
		for(group_sort = 1; group_sort * distance < length; group_sort ++)
		{                                    //分组数
			for(group_number = distance + group_sort - 1; group_number< length; group_number += distance)
			{                                //distance + group_sort - 1 为每组起始位置                                          
				for(i = 0; i < group_number; i += distance)
				{                            //组内为直接插入排序
					if(sort[group_number] < sort[i])
					{
						temp = sort[group_number];
						for(j = group_number; j > i; j -= distance)
							sort[j] = sort[j - distance];
						sort[j] = temp;
					}
				}
			}
		}
	}
    for(int ii = 0; ii < length; ii++)      
        cout << sort[ii] << endl; 
}      
void main()      
{      
    cout << "请输入十个数" << endl;      
    int ForSort[10];      
    for (int ii = 0; ii < 10; ii++)      
    {      
        cin >> ForSort[ii];      
    }      
    printf("\n");      
    SellSort(ForSort,10);    //初始增量设为5    
}      

/*直接选择排序*/  
#include <iostream>  
  
using namespace std;  
  
void DirectSelectSort(int ForSort[], int n)  
{  
    int i, j, k;  
    int temp;  
  
    for(i = 0; i < n-1 ; i++)  
    {  
        k = i;  
        for(j = i + 1; j < n ; j++)  
            if(ForSort[j] < ForSort[k])  
                k = j;  
        if(i!=k)  
        {  
            temp = ForSort[k];  
            ForSort[k] = ForSort[i];  
            ForSort[i] = temp;  
        }  
    }  
    for(i = 0; i < n; i++)      
        cout << ForSort[i] << endl;  
}  
  
void main()      
{      
    cout << "请输入十个数" << endl;      
    int Sort[10];      
    for (int ii = 0; ii < 10; ii++)      
    {      
        cin >> Sort[ii];      
    }      
    printf("\n");      
    DirectSelectSort(Sort,10);    //初始增量设为5    
}      


/*我写的直接选择排序。这个比较简单,几分钟搞定。*/
#include <iostream>
using namespace std;

void DirectSelectSort(int Sort[], int length)
{
	int temp;
	for(int i = 0; i < length; i++)
	{
		for(int j = i + 1; j < length; j++)
		{
			if(Sort[j] < Sort[i])
			{
				temp = Sort[j];
				Sort[j] = Sort[i];
				Sort[i] = temp;
			}
		}
	}
    for(i = 0; i < length; i++)        
        cout << Sort[i] << endl; 
}

void main()        
{        
    cout << "请输入十个数" << endl;        
    int ForSort[10];        
    for (int ii = 0; ii < 10; ii++)        
    {        
        cin >> ForSort[ii];        
    }        
    printf("\n");        
    DirectSelectSort(ForSort,10);    //初始增量设为5      
} 


树形选择排序。目前还未学过树的知识,等以后学过二叉树后再来解决这个排序方法。

先空着……



/*冒泡排序*/  
#include<stdio.h>     
void bubbleSort(int arr[],int n)   
{       
    int i,j,t;        
    for(i=0;i<n-1;i++)       
    {           
        for(j=0;j<n-i-1;j++)           
        {               
            if(arr[j+1]<arr[j])               
            {                 
                t=arr[j+1];                 
                arr[j+1]=arr[j];                 
                arr[j]=t;                
            }           
        }       
    }     
}     
void print(int arr[],int n)    //打印数组   
{       
    int i=0;       
    for(;i<n;i++)       
    {           
        printf("%d  ",arr[i]);       
    }       
    printf("\n");   
}   
int main()   
{       
    int arr[]={49,15,52,64,98};    //测试数据       
    print(arr,5);       
    bubbleSort(arr,5);           
    printf("排序后的结果:\n");       
    print(arr,5);       
    return 0;   
}  


/*我写的冒泡排序*/
#include <iostream>
using namespace std;

void BubbleSort(int Sort[], int length)
{
	int temp;
	int i;
	for(int ii = 0; ii < length; ii++)
	{
		for(i = 0; i < length; i++)
		{
			if(Sort[i] < Sort[i - 1])
			{
				temp = Sort[i];
			    Sort[i] = Sort[i - 1];
			    Sort[i - 1] = temp;
			}
		}
	}
    for(i = 0; i < length; i++)        
        cout << Sort[i] << endl; 
}

void main()        
{        
    cout << "请输入十个数" << endl;        
    int ForSort[10];        
    for (int ii = 0; ii < 10; ii++)        
    {        
        cin >> ForSort[ii];        
    }        
    printf("\n");        
    BubbleSort(ForSort,10);          
} 


/*快速排序,能力有限,为了追赶计划进度,就记了书上的代码。*/
#include <iostream>
using namespace std;

void QuickSort(int Sort[], int low, int high)
{
	int temp;
	int i,j;

	if(low >= high)
		return;

	i = low;
	j = high;
	temp = Sort[i];
	while( i < j)
	{
		/*从后往前进行比较,直到当前记录的排序码小于等于中心值*/
		while( i < j && temp < Sort[j])
			j--;
		if(i < j)               //将排序码小于等于中心值的记录交换到前面当前空出的记录位置
			Sort[i++] = Sort[j];
		/*从前往后进行比较,直到当前记录的排序码大于中心值*/
		while(i < j && Sort[i] < temp)
			i++;
		if(i < j)              //将排序码大于中心值的记录交换到后面当前空出的记录位置
			Sort[j --] = Sort[i];
	}
	Sort[i] = temp;
    QuickSort(Sort, low, --j);
    QuickSort(Sort, ++i, high);
}

void main()        
{        
    cout << "请输入十个数" << endl;        
    int ForSort[10];        
    for (int ii = 0; ii < 10; ii++)        
    {        
        cin >> ForSort[ii];        
    }        
    printf("\n");        
    QuickSort(ForSort, 0, 9);  
    for(int i = 0; i < 10; i++)    //输出      
        cout << ForSort[i] << endl; 
} 


/*归并排序,以下算法是网上借鉴的。
原网址:http://icarusliu.iteye.com/blog/1439326*/

#include <iostream.h> 
#include <malloc.h>
#include <stdio.h>
  
void merge(int *data, int start, int middle, int end)  
{  
    int i = start, j = middle + 1;  
    int pos = 0;  
    int *temp = NULL;  
      
    if (start >= end)  
    {  
        return;  
    }  
  
    temp = (int *)malloc((end - start + 1) * sizeof(int));  
  
    while (i <= middle && j <= end)  
    {  
        temp[pos++] = data[i] <= data[j] ? data[i++] : data[j++];  
    }  
  
    while (i <= middle)  
    {  
        temp[pos++] = data[i++];  
    }  
  
    while (j <= end)  
    {  
        temp[pos++] = data[j++];  
    }  
  
    pos = 0;  
    while (pos < end - start + 1)  
    {  
        data[start + pos] = temp[pos++];  
    }  
  
    free(temp);  
}  
  
void mergeSort(int *data, int length)  
{  
    int i, step;  
  
    for (step = 1; step < length; step *= 2)  
    {  
        for (i = 0; i + step < length; i += 2 * step)  
        {  
            (i + 2 * step - 1 < length) ?   
                merge(data, i, i + step - 1, i + 2 * step - 1) : merge(data, i, i + step - 1, length - 1);  
        }  
    }  
}  

void main()        
{        
    cout << "请输入十个数" << endl;        
    int ForSort[10];        
    for (int ii = 0; ii < 10; ii++)        
    {        
        cin >> ForSort[ii];        
    }        
    printf("\n");        
    mergeSort(ForSort, 10);  
    for(int i = 0; i < 10; i++)    //输出      
        cout << ForSort[i] << endl; 
} 

/*基数排序还是挺简单的,虽然不明白与希尔排序有什么
大区别,都是分组嘛。呵呵*/

#include <stdio.h>
#include <stdlib.h>
int main()
{
	int data[10]={73,22,93,43,55,14,28,65,39,81};
	int temp[10][10]={0};
	int order[10]={0};
	int i,j,k,n,lsd;
	k=0;n=1;
	printf("\n排序前: ");
	for (i=0;i<10;i++)
		printf("%d ",data[i]);
	putchar('\n');
	while (n<=10){
		for (i=0;i<10;i++){
			lsd=((data[i]/n)%10);
			temp[lsd][order[lsd]]=data[i];
			order[lsd]++;
		}
		printf("\n重新排列: ");
		for (i=0;i<10;i++){
			if(order[i]!=0)
				for (j=0;j<order[i];j++){
					data[k]=temp[i][j];
					printf("%d ",data[k]);
					k++;
				}
				order[i]=0;
		}
		n*=10;
		k=0;
	}
	putchar('\n');
	printf("\n排序后: ");
	for (i=0;i<10;i++) printf("%d ",data[i]);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值