排序----demo----

排序1---冒泡法:
单向冒泡排序的基本原理就是:对于给定的n个数据,从第一个数据开始一次对相邻的两个数据进行比较,当前面的数据大于后面的数据时,交换位置,进行一轮比较和换位后,n个数据中最大的那个被排在最后,即第n位。然后对前面n-1个数据进行第二次比较,重复该过程。

package com.etc.jichu;

public class MaoPaoPaiXuDemo1 
{
	public static int[] maopaoSort(int[] a)
	{
		int length=a.length;
        int i,j,temp;
        for(i=length-1;i>0;i--)
        {   //第一次限制在0-9循环,第二次限制在0-8循环,依次降低
            for(j=0;j<i;j++)
            {     
                if(a[j]>a[j+1])
                {   //将大的数字放在a[j+1]
                    temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                }
            }
        }
        return a;
    }
	
public static void main(String[] args) 
{
	int a[]={6,9,17,8,73,12,56,49,56,60};
	 a=maopaoSort(a);
     for(int i=0;i<a.length;i++)
     {
         System.out.print(a[i]+" ");
     }
}
}
========================================================================
排序2---插入法:
思想:
1.在原始数据中,将第一个数据作为已排序的数据序列
2.从数组中获取下一个元素,在已经排序好的元素中从后向前扫描,并判断该元素与已排列好的的大小
3.若排序序列的元素大于新元素,则将该元素移到下一位置
4.重复步骤三,直到找到已排序的元素小于或者等于新元素的位置
5.将新元素插入到该位置
6.重复步骤2~5,直到数据处理完毕

栗子:
【38,65,97,76,13,27,49】

排序过程

第一趟插入38:【38,65,97,76,13,27,49】

第二趟插入65:【38,65,97,76,13,27,49】

第三趟插入97:【38,65,97,76,13,27,49】

第四趟插入76:【38,65,76,97,13,27,49】

第五趟插入13:【13,38,65,76,97,27,49】

第六趟插入27:【13,27,38,65,76,97,49】

第七趟插入49:【13,27,38,49,65,76,97】

package com.etc.jichu;
public class ChaRuPaiXu 
{
	public static int[] charuSort(int[] a)
	{
		int length=a.length;
		int i,j,tmp;
		for(i=1;i<length;i++)
		{
			tmp=a[i];               //取出一个未排序的数据
			for(j=i-1;j>=0&&a[j]>tmp;j--)
			{ //在排序序列中查找位置
				a[j+1]=a[j];        //数据向后移动,无须担心被覆盖,因为tmp以保存第一个被覆盖的数据
			}
			a[j+1]=tmp;   //插入数据,因为上面的for循环最后执行了一次j--,所以赋值时a[j+1]
		}
		return a;
	}
	public static void main(String[] args) 
	{
		int a[]={22,94,20,28,17,41,35,14,83,66};
		a=charuSort(a);
		for(int i=0;i<a.length;i++)
		{
			System.out.print(a[i]+" ");
		}
	}
}
==============================================================================
排序3---快速排序法:
思想:
1.在一列数组中,选择其中一个数据作为“基准”。

2.所有小于“基准”的数据,都移到“基准”的左边,所有大于“基准”的数据,都移到“基准”的右边。

3.对于“基准”左边和右边的两个子集,不断重复第一步和第二步。直到所有的数据子集只剩下一个数据为止。

栗子:

现有一组数据如下:

[ 8 , 2 , 6 , 4 , 1 , 3 , 9 , 5 ]

第一步:(假如选4为基准),

[ 8 , 2 , 6 , 4 , 1 , 3 , 9 , 5 ]

第二步:将每个数据与4对比,"<=4"左边,“>4”放右边。

[ 2 , 1 , 3 ]  4  [ 8 , 6 , 9 , 5 ]

第三步:两个子数组,重复第一步选“选基准”和第二步“放置数据”。4的位置后面都不会动的。

[ 2 , 1 , 3 ]  4  [ 8 , 6 , 9 , 5 ]

1 [ 2 , 3 ]  4  [ 5 ] 6 [ 8 , 9 ]

最后:

[ 1 , 2 , 3 , 4 , 5 , 6 , 8 , 9 ]

package com.etc.jichu;
public class KuaiSuPaiXu 
{
	public static void kuaiSuSort(int[] array,int low,int high)
	{
		if(low>high) //一定要有这句
		{ 
			return;
		}
		int i=low,j=high;
		int index=array[i];  //index为"基准"
		while(i<j)
		{
			while(i<j&&array[j]>=index)
			{ //寻找到array[j]<index,即发现小于基准值的数字,才退出,此时寻找到的数字的下标为j
				j--;
			}
			if(i<j)
			{
				array[i]=array[j]; //将找到的那个小于基准值的数字覆盖了array[i],原先的array[i]存储在基准值index中
				i++;  //i下标右移
			}
			while(i<j&&array[i]<index)
			{  //寻找>=基准值的数字array[i],退出循环
				i++;
			}
			if(i<j)
			{    将找到的那个大于或等于基准值的数字覆盖了array[j],此时array[i]会有重复了
				array[j]=array[i];
				j--;
			}
		}  //这个一次循环结束:1.所有小于index的数字会都在index左边,所有大于或等于index的数字会在右边 
		array[i]=index;  //array[i]位置固定好了,刚好覆盖重复的那个值
		kuaiSuSort(array,low,i-1);  //递归左边的小于index但未排序的数据
		kuaiSuSort(array,i+1,high); //递归右边的大于index但未排序的数据
	}
	public static void quickSort(int[] array)
	{
		kuaiSuSort(array,0,array.length-1);

	}
	public static void main(String[] args) 
	{
		int a[]={26,49,30,88,27,31,85,64,33,86};
		quickSort(a);
		for(int i=0;i<a.length;i++)
		{
			System.out.print(a[i]+" ");
		}
	}
}
============================================================================
排序4---选择排序法:
思想:
对于给定的一组数据,经过第一轮比较后得到最小的数据,然后将该数据与第一个数据的位置进行交换;
然后对不包括第一个数据以外的其它数据进行第二轮比较,得到最小的数据并与第二个数据进行位置交换;
重复该过程,直到进行比较的数据只有一个时为止。

栗子:

待排序数组

【38,65,97,76,13,27,49】

排序过程

第一趟:【13,65,97,76,38,27,49】

第二趟:【13,27,97,76,38,65,49】

第三趟:【13,27,38,76,97,65,49】

第四趟:【13,27,38,49,97,65,76】

第五趟:【13,27,38,49,65,97,76】

第六趟:【13,27,38,49,65,76,97】

最后结果:【13,27,38,49,65,76,97】

package com.etc.jichu;
public class XuanZePX 
{
public static int[] xzSort(int[] a)
{
        int length=a.length; //先得到数组的长度
        int i,j,n,tmp,flag;
        for(i=0;i<length;i++)
        {
            tmp=a[i];  //tmp为每次循环的第一个位置
            flag=i;
            for(j=i+1;j<length;j++)
            {
                if(tmp>a[j])
                {
                    tmp=a[j]; //这一步不要忘记了,因为tmp不一定是最小的,必须用循环过的最小的和当前的数据比较
                    flag=j;   //如果发现比tmp小的数据,将flag标记为当前位置,循环结束,flag标志的是最小数字的下标
                }
            }
            if(flag!=i)
            {    //如果flag已经不再是刚开始第一个位置的i,那么将两者的数据调换位置
                n=a[i];
                a[i]=a[flag];
                a[flag]=n;
            }
        }
        return a;
    }
    public static void main(String[] args)
    {
        int a[]={21,49,20,28,67,13,53,64,23,66};
        a=xzSort(a);
        for(int i=0;i<a.length;i++)
        {
            System.out.print(a[i]+" ");
        }
    }
}
==========================================================================
排序5---归并排序法:
思想:
对于给定的一组数据(假设有n个数据),首先将每两个相邻长度为1的子序列进行归并,得到n/2个长度为2或者1的有序子序列,再将其两两合并,反复此过程,得到一个有序序列。

public class TestMergeSort {   
    
    //目的就是将一个数组分成两组,按从小到大顺序排列后重新组合成新数组,即“并”
    public static void mergeArray(int[] a,int first,int mid,int last,int[] temp){
        int i=first,j=mid+1;
        int m=mid,n=last;  //以mid为分界点,将a[]分为两组 : a[i...m]和a[j...n] 
        int k=0;
        while(i<=m&&j<=n){   //其中一个条件不满足,可能就是一个数组的数据全部赋值到temp中,另一个还有残留数据
            if(a[i]<=a[j]){
                temp[k++]=a[i++];
            }else{
                temp[k++]=a[j++];
            }
        }
        
        while(i<=m){
            temp[k++]=a[i++];
        }
        while(j<=n){
            temp[k++]=a[j++];
        }
        
        for(i=0;i<k;i++){        //将temp[]中的排序好的值 重新放回 a[]
            a[first+i]=temp[i];
        }
    }
    //利用递归的方式拆开数据,即“归”
    public static void mergeSort(int a[],int first,int last,int[] temp){
        if(first<last){    //这里的last是最后一个数据的位置坐标,比长度小1
            int mid=(first+last)/2;  //寻找中间的位置坐标值
            mergeSort(a,first,mid,temp); //左边有序
            mergeSort(a,mid+1,last,temp); //右边有序
            mergeArray(a,first,mid,last,temp); //有序之后合并
        }
    }
    
    public static void mergeSortFinal(int[] a){  //传入一个数据即可
        int last=a.length;  //这里的last是长度
        int[] temp = new int[last];
        mergeSort(a,0,last-1,temp); 
    }
    
    public static void main(String[] args){
        int a[]={2,9,0,8,7,1,5,4,3,6};
        mergeSortFinal(a);
        for(int i=0;i<a.length;i++){
            System.out.print(a[i]+" ");
        }
    }
}
=================================================================================
排序6---希尔排序法:
思想:
1.选择一个步长序列T1,T2,T3...,Tk,满足Ti>Tj(i<j),Tk=1。(步长:即每次插入排序的步长)

2.按步长序列的个数K,对待排序序列进行K趟排序。

3.每趟排序,根据对应的步长Ti,对待排序列分割成Ti个子序列,分别对子序列进行插入排序。

public class TestShellSort {
    public static void  shellSort(int[] array){
        int length=array.length;
        int i,j;
        int h;    //h为每次的步长
        int temp;
        for(h=length/2;h>0;h=h/2){  //步长每次减小为原来的1/2 
            for(i=h;i<length;i++){ //从array[h]开始,往后。第一次即从中间位置往后开始,第二次往后挪一位
                temp=array[i];    //保存数据至temp
                for(j=i-h;j>=0;j=j-h){  //第一次j从0开始,结束该循环。第二次进入该循环j从1开始,因为i增加1了,这段代码保证每次都是步长为h的两个数据在比较
                    if(temp<array[j]){    //如果发现比temp大的数据,将该数据放置在位置array[j+h]
                        array[j+h]=array[j];
                    }else{
                        break;
                    }
                }
                array[j+h]=temp;
            }
        }
    }
    public static void main(String[] args){
        int a[]={2,9,0,8,7,1,5,4,3,6};
        shellSort(a);
        for(int i=0;i<a.length;i++){
            System.out.print(a[i]+" ");
        }
    }
}
===========================================================================

  

转载于:https://www.cnblogs.com/ipetergo/p/6292553.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值