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] + " " );
}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值