java排序 冒泡

1.冒泡排序 思路:http://www.cnblogs.com/kkun/archive/2011/11/23/2260280.html

	public void bubble(int[] arr)
	{
		for(int i=1;i<arr.length;i++)
		{
			for(int j=0;j<arr.length-i;j++)
			{
				int temp=arr[j];
				if(arr[j]>arr[j+1])
				{
					arr[j]=arr[j+1];
					arr[j+1]=temp;
				}
			}
		}
	}

2.选择排序 思路:http://baike.baidu.com/view/547263.htm

 1      public void select(int[] arr)
 2      {
 3          int index=0;
 4          for(int i=0;i<arr.length-1;i++)
 5          {
 6              index=i;
 7              for(int j=i+1;j<arr.length;j++)
 8              {
 9                  if(arr[j]<arr[index])
10                  {
11                      index=j;
12                  }
13              }
14              if(i!=index)
15              {
16                  int temp=arr[index];
17                  arr[index]=arr[i];
18                  arr[i]=temp;
19              }
20          }
21      }

 3.快速排序的思想:见《数据结构》

    public void fastSortArray(int[] arr,int i,int j)
    {
        int start=i;
        int end=j;
        
        if(start<0||end>=arr.length||(start>=end))
            return ;
        int pivotKey=arr[start];
        
        boolean direction=false;//代表从后往前
        while(i<j)
        {
            if(direction)
            {
                if(arr[i]>pivotKey)
                {
                    arr[j]=arr[i];
                    direction=false;
                    j--;
                }else
                {
                    i++;
                }
                
            }else
            {
                if(arr[j]<pivotKey)
                {
                    arr[i]=arr[j];
                    direction=true;
                    i++;
                }else
                {
                    j--;
                }
                
            }
        }
        if(i==j)
        {
            arr[i]=pivotKey;
        }
        
        fastSortArray(arr,start,i-1);
        fastSortArray(arr, j+1, end);
    }

  

原文地址:https://www.cnblogs.com/passer1991/p/3182968.html