算法温习——手撕快速排序
1、快速排序介绍
2、Java实现(个人实现。。)
import org.junit.Test; public class QuickSortTest { public void quickSort(int[] array, int left, int right){ if (left>=right){ return; } int i = left, j = right; int temp = array[left]; while (i<j){ while (array[j] >= temp) { j--; } while (array[i] <= temp){ i++; } if (i < j){ int t = array[i]; array[i] = array[j]; array[j] = t; } } array[left] =array[j]; array[j] = temp; quickSort(array,left,j-1); quickSort(array,j+1,right); } @Test public void testQuickSort(){ int[] array = {6,1,2,7,9,3,4,5,10,8}; for (int i : array){ System.out.print(i+" "); } System.out.println(); quickSort(array,0,array.length-1); for (int i : array){ System.out.print(i+" "); } } }
运行结果:
下一篇:
Hash散列算法详细解析(一)