java常用的排序

冒泡排序
package test;

/**
 * @descript BubbleSortDemo.java
 * @author sinclair
 * @date Jun 11, 2010
 */
class BubbleSort {
    private int[] temp;

    public BubbleSort( int[] src ) {
	temp = src;
    }

    /**
     * 输出显示
     */
    public void display() {
	for ( int i = 0; i < temp.length; i++ ) {
	    System.out.print( temp[i] + "," );
	}
    }

    /**
     * 排序
     */
    public void sort() {
	for ( int i = temp.length - 1; i > 1; i-- ) {
	    for ( int j = 0; j < i; j++ ) {
		if (temp[j] > temp[j + 1]) {
		    swap( j, j + 1 );
		}
	    }
	}
    }

    /**
     * 交换
     * 
     * @param a
     * @param b
     */
    public void swap( int a, int b ) {
	int c = temp[a];
	temp[a] = temp[b];
	temp[b] = c;
    }
}

public class BubbleSortDemo {
    public static void main( String[] args ) {
	int[] a = new int[] { 2, 5, 3, 6, 1, 7, 9, 6 };
	BubbleSort bubble = new BubbleSort( a );
	bubble.sort();
	bubble.display();
    }
}



选择排序
package test;

class SelectSort {
    private int[] temp;

    public SelectSort( int[] src ) {
	this.temp = src;
    }

    /**
     * 排序
     */
    public void sort() {
	int in, out, min;
	for ( out = 0; out < temp.length - 1; out++ ) {
	    min = out;
	    for ( in = out + 1; in < temp.length; in++ ) {
		if (temp[in] < temp[min]) {
		    min = in;
		    swap( out, min );
		}

	    }
	}
    }

    /**
     * 交换
     * 
     * @param a
     * @param b
     */
    public void swap( int a, int b ) {
	int c = temp[a];
	temp[a] = temp[b];
	temp[b] = c;
    }

    /**
     * 输出显示
     */
    public void display() {
	for ( int i = 0; i < temp.length; i++ ) {
	    System.out.print( temp[i] + "," );
	}
    }
}

public class SelectSortDemo {
    /**
     * @param args
     */
    public static void main( String[] args ) {
	int[] a = new int[] { 2, 5, 3, 6, 1, 7, 9, 6 };
	SelectSort bubble = new SelectSort( a );
	bubble.sort();
	bubble.display();
    }

}



快速排序
/**
 * 
 */
package test;

/**
 * @descript QuickSortDemo.java
 * @author sinclair
 * @date May 13, 2010
 */
public class QuickSortDemo {
    /**
     * 排序
     * 
     * @param pData
     * @param left
     * @param right
     */
    public static void QuickSort( int[] pData, int left, int right ) {
	int i, j;
	i = left;
	j = right;
	while ( true ) {
	    while ( ( ++i ) < right - 1 && pData[i] < pData[left] )
		;
	    while ( ( --j ) > left && pData[j] > pData[left] )
		;
	    if (i >= j)
		break;
	    swap( pData, i, j );
	}
	swap( pData, left, j );
	if (left < j)
	    QuickSort( pData, left, j );
	if (right > i)
	    QuickSort( pData, i, right );
    }

    /**
     * 交换
     * 
     * @param temp
     * @param a
     * @param b
     */
    public static void swap( int temp[], int a, int b ) {
	int c = temp[a];
	temp[a] = temp[b];
	temp[b] = c;
    }

    public static void main( String[] args ) {
	int[] pData = new int[30];
	for ( int i = 0; i < pData.length; i++ )
	    pData[i] = ( int ) ( Math.random() * 100 );
	for ( int i = 0; i < pData.length; i++ )
	    System.out.print( pData[i] + " " );
	QuickSortDemo.QuickSort( pData, 0, pData.length );
	System.out.println( "\n快速排序处理..." );
	for ( int i = 0; i < pData.length; i++ )
	    System.out.print( pData[i] + " " );
    }
}

你可能感兴趣的:(java,C++,c,C#,J#)