選擇排序:
- <span style="font-size:18px;">/**
- * 選擇排序
- */
- private static int[] insertArray(int[] a){
- for(int i=0;i<=a.length-1;i++){
- for(int j=i+1;j<=a.length-1;j++){
- if(a[i]>a[j]){
- int t=a[i];
- a[i]=a[j];
- a[j]=t;
- }
- }
- }
- return a;
- } </span>
- <span style="font-size:18px;">/**
- * 插入排序
- */
- private static int[] insertArray(int[] a){
- int i,j,t;
- for(i=1;i<=a.length-1;i++){
- t=a[i];
- for(j=i-1;j>=0 && t<a[j];j--){
- a[j+1]=a[j];
- }
- a[j+1]=t;
- }
- return a;
- } </span>
- <span style="font-size:18px;">
- </span>
- <span style="font-size:18px;">
- </span>
- <span style="font-size:18px;">/**
- * 冒泡排序
- */
- private static int[] insertArray(int[] a){
- for(int i=0;i<a.length-1;i++){
- for(int j=0;j<a.length-1-i;j++){
- if(a[j]>a[j+1]){
- int t=a[j];
- a[j]=a[j+1];
- a[j+1]=t;
- }
- }
- }
- return a;
- } </span>
快速排序:
- /**
- * 交換指定數組a的兩個變量的值
- * @param a 數組
- * @param i 數組下標
- * @param j 數組下標
- */
- public static void swap(int a[],int i,int j){
- if(i==j) return;
- int tmp=a[i];
- a[i]=a[j];
- a[j]=tmp;
- }
- /**
- *
- * @param array 待排序數組
- * @param low 數組下標下界
- * @param high 數組下標上界
- * @return
- */
- public static int partition(int array[],int low,int high){
- //當前位置爲第一個元素所在位置
- int p_pos=low;
- System.out.println("此時的p_pos爲"+p_pos);
- //採用第一個元素爲軸
- int pivot=array[p_pos];
- for(int i=low+1;i<=high;i++){
- if(array[i]<pivot){
- p_pos++;
- swap(array, i, p_pos);
- }
- }
- swap(array, low, p_pos);
- return p_pos;
- }
- /**
- * 快速排序實現
- * @param array
- * @param low
- * @param high
- */
- public static void quickSort(int array[],int low,int high){
- if(low<high){
- int povit=partition(array, low, high);
- quickSort(array, low, povit-1);
- quickSort(array, povit+1, high);
- }
- }