冒泡排序

 

package com.zj.exercise;

/**
 * use:冒泡排序
 * @author zhengjiong
 * time:2011-9-13 02:55:24
 */
public class MaoPaoTest {
	//按升序排序
	public static void sort(int[] x){
		
		for(int i = 0;i < x.length - 1;i++){
			
			for(int j = 0;j < x.length - 1 - i;j++){
				
				if(x[j] > x[j+1]){
					int temp = x[j+1];
					x[j+1] = x[j];
					x[j] = temp;
				}
			}
		}
	}
	//降序排序
	public static void descSort(int[] x){
		for(int i = 0;i < x.length-1;i++){
			
			for(int j = 0;j < x.length - 1 - i;j++){
				if(x[j] < x[j+1]){
					int temp = x[j+1];
					x[j+1] = x[j];
					x[j] = temp;
				}
			}
		}
	}
	public static void main(String[] args){
		
		int[] x = new int[]{6,3,13,11,9,8,10};
		MaoPaoTest.descSort(x);
		
		for(int i=0;i<x.length;i++){
			System.out.println(x[i]);
		}
	}
}

你可能感兴趣的:(java)