i-world-i-dream

 

常用排序方法CommonSort



public class CommonSort {
    
     
int[] srcArray = new int[]{49,386597 ,76 ,13 ,0,-1,65464,4654,575,4,46,45654,45654,45,-100,6456,45654,45654,45645,64,2749,2423,456,465,5765,5756,5756,567,5,2342,234245,43,45}
     
int[] sortArray;
     
int[] getArray;
     
int[] tempArray;
     
long beginTime;
     
long endTime;
     
long spendTime;
    
    
public  int[] InsertSort(int[] sa)
    
{
        System.out.println(
"++InsertSort++");
        
//startsort(sa);
        int length = sa.length;
        tempArray 
=null;
        tempArray 
=new int[length];
        System.arraycopy(sa, 
0, tempArray, 0, length);
        sortArray 
= null;
        sortArray 
= new int[length+1]; //sortArray[0]为监视窗,交换用
        int j;
        
for(int i=0;i<length;i++)
        
{
            sortArray[
0]=tempArray[i];
            j
=i;
            
while(j>0&&sortArray[0]<sortArray[j])//比小
            {
                sortArray[j
+1]=sortArray[j];//后移一位
                j--;
            }

            sortArray[j
+1]=sortArray[0];//插入
        }
       
        System.arraycopy(sortArray, 
1, tempArray, 0, length);
        
//endsort(sa);       
        return tempArray;
    }

    
    
public  int[] SelectSort(int[] sa)
    
{
        System.out.println(
"++SelectSort++");
        
//startsort(sa);
        int length = sa.length;
        tempArray 
=null;
        tempArray 
=new int[length];
        System.arraycopy(sa, 
0, tempArray, 0, length);
        
int i=0;
        
int j=0;
        
int k=0;
        
for(i=0;i<length-1;i++)
        
{
            k
=i;
            
for(j=i+1;j<length;j++)
            
{
                
if(tempArray[j]<tempArray[k])//选最小的k
                    k=j;
            }

            
if(k!=i)
                swap(tempArray,i,k);
        }

        
//endsort(sa);
        return tempArray;
    }

    
    
public  int[] BubbleSort(int[] sa)
    
{
        System.out.println(
"++BubbleSort++");
        
//startsort(sa);
        int length = sa.length;
        tempArray 
=null;
        tempArray 
=new int[length];
        System.arraycopy(sa, 
0, tempArray, 0, length);
        
for(int i=0;i<length;i++)
        
{
            
for(int j=length-1;j>0;j--)
            
{
                
if(tempArray[j]<tempArray[j-1])//逐个比
                    swap(tempArray,j,j-1);
            }

        }

        
//endsort(sa);
        return tempArray;
    }


    
public  int[] QuickSort(int low,int high,int[] sa)
    
{
        System.out.println(
"++QuickSort++");
        
//startsort(sa);
        int length=sa.length;
        tempArray 
=null;
        tempArray 
=new int[length];
        System.arraycopy(sa, 
0, tempArray, 0, length);
        
int pivot; 
        
if (low < high) 
          pivot 
= partition(low, high,tempArray); 
          QuickSort(low, pivot 
- 1,tempArray); 
          QuickSort(pivot 
+ 1, high,tempArray); 
        }
 
        
//endsort(sa);
        return tempArray;
    }

    
    
public int partition(int low, int high,int sa[]) 
        
int pivot, p_pos, i; 
        p_pos 
= low; 
        pivot 
= sa[p_pos]; 
        
for (i = low + 1; i <= high; i++
          
if (sa[i]< pivot) 
            p_pos
++
            swap(sa, p_pos, i); 
          }
 
        }
 
        swap(sa, low, p_pos); 
        
return p_pos; 
      }

    

    
public  int[] HeapSort(int[] sa)
    
{
        System.out.println(
"++HeapSort++");
        
int length=sa.length;
        tempArray 
=null;
        tempArray 
=new int[length];
        System.arraycopy(sa, 
0, tempArray, 0, length);
        
//startsort(sa);

        
//endsort(sa);
        return tempArray;
    }

    
    
    
public  int[] MeregSort(int low,int high,int[] sa)
    
{
        System.out.println(
"++MeregSort++");
        
//startsort(sa);
        int length=sa.length;
        tempArray 
=null;
        tempArray 
=new int[length];
        System.arraycopy(sa, 
0, tempArray, 0, length);
        
int mid;
        
if(low<high)
        
{
            mid
=(low+high)/2;//求中值
            MeregSort(low,mid,tempArray);//递归合并
            MeregSort(mid+1,high,tempArray);//递归合并
            Mereg(low,mid,high,tempArray);//合并
        }

        
//endsort(sa);
        return tempArray;
    }

    
    
public void Mereg(int low,int mid,int high,int[]sa)
    
{
        
int length = sa.length;
        sortArray 
= null;
        sortArray 
= new int[length+1];
        
int h,i,j,k;
        h
=low;
        i
=low; 
        j
=mid+1;
        
while((h<=mid)&&(j<=high))
        
{
            
            
if (sa[h]<=sa[j])
            
{
                sortArray[i]
=sa[h]; 
                h
=h+1;
            }

            
else
            
{
                sortArray[i]
=sa[j]; 
                j
=j+1;
            }

            i
++;
        }

        
if (h>mid)
        
{
               
for(k=j;k<=high;k++)
               
{
                   sortArray[i]
=sa[k];
                   i
++;
               }

        }

        
else
        
{
              
for(k=h;k<=mid;k++)
              
{
                  sortArray[i]
=sa[k]; 
                  i
++;
              }

        }

        
for(k=low;k<=high;k++)
          sa[k]
=sortArray[k];
    }

    
    
public  void printArray(int[] pa)
    
{
        
int length = pa.length;
        
for(int i=0;i<length;i++)
        
{
            System.out.println(
"array["+i+"]"+pa[i]);
        }

        System.out.println(
"length:"+length+":----");
    }

    
    
public void startsort(int[] sa)
    
{
        System.out.println(
"--------------");
        printArray(sa);
        beginTime
=System.currentTimeMillis();
        System.out.println(
"starttime:"+beginTime);
    }

    
    
public void endsort(int[] sa)
    
{
        printArray(sa);
        endTime
=System.currentTimeMillis();
        System.out.println(
"endtime:"+endTime);
        spendTime
=endTime-beginTime;
        System.out.println(
"spendtime:"+spendTime+":-------");
        System.out.println();
    }

    
     
public int[] swap(int[] sa, int i, int j) 
     

        
int tmp = sa[i]; 
        sa[i] 
= sa[j]; 
        sa[j] 
= tmp; 
        
return sa;
     }


    
public CommonSort()
    
{
        getArray
=null;
        startsort(srcArray);
        getArray
=InsertSort(srcArray);
        endsort(getArray);
        startsort(srcArray);
        getArray
=SelectSort(srcArray);
        endsort(getArray);
        startsort(srcArray);
        getArray
=BubbleSort(srcArray);
        endsort(getArray);
        startsort(srcArray);
        getArray
=MeregSort(0,srcArray.length-1,srcArray);
        endsort(getArray);
        startsort(srcArray);
        getArray
=QuickSort(0,srcArray.length-1,srcArray);
        endsort(getArray);
    }

    
    
public static void main(String[] args)
    
{
        
new CommonSort();
    }


}

posted on 2007-08-31 16:09  exce4  阅读(306)  评论(1编辑  收藏  举报

导航