搞了好几年的软件开发,发现基础知识反而忘记了。今天熟悉熟悉。。/*** * *@author wjj *@version 1.0 * * * */ public class Sort { /** *选择排序 * */ public static int[] selectionSort(int[] m) { int theLength = m.length; int current = 0; while(current < theLength -1) { int small = current; for(int i = current+1; i <= theLength -1; i++) { if(m[i]< m[small]) { small = i ; } } int temp = m[current]; m[current] =m[small]; m[small] = temp; current ++; } return m; } /** *冒泡排序 * */ public static int[] bubbleSort(int [] m) { int theLength = m.length; int current =0; boolean sweap = false; do { print(m); sweap =false; for(int index = theLength -1; index >= current +1; index --) { if(m[index] < m[index-1]) { int temp = m[index-1]; m[index-1] = m[index]; m[index] =temp; sweap = true; } } current ++; } while(current < theLength-1 && sweap ); return m; } /** *快速排序 * */ public static int[] quickSort(int[] m,int start, int end) { if(end -start > 0) { int splitVal = m[start]; { int left = start +1; int right = end; do{ while(m[left] <= splitVal && left <= right ) left++; while(m[right] >= splitVal && left <= right ) right--; if(left < right) { int temp =m[right]; m[right] = m[left]; m[left] = temp; } }while(left<=right); int splitPoint = right; int temp =m[right]; m[right] = m[left]; m[left] = temp; quickSort( m, start, splitPoint-1); quickSort( m, splitPoint+1, end); } } return m; } public static void print(int[] m) { for (int i = 0; i < m.length; i++) { System.out.print(m[i] + " "); } System.out.println(); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int[] m = { 20, 12, 92, 13, 27, 329, 88, 100 }; int[] n = quickSort(m,0,m.length-1); print(m); } }