1、选择排序
选择排序法先找到数列中的最小的数,然后将它放在数列的最前面。接下来,在剩下的数中找到最小数,将它放在第一个数的后面,以此类推,知道数列中仅剩一个数为止。
具体代码如下:
package com.hyq.chapter01; /** * Created by Administrator on 2016/10/8. */ public class SelectionSort { public static void sort(int[] nums){ for (int i = 0;i<nums.length;i++){ int min = nums[i]; int index = i; for(int j = i+1;j<nums.length;j++){ if (nums[j]<min){ min = nums[j]; index = j; } } if (index!=i){ nums[index] = nums[i]; nums[i] = min; } } } public static void main(String[] args) { int[] nums = {13,15,52,25,6,11,37,43,12,1}; sort(nums); for (int n:nums) { System.out.print(n+" "); } } }
2、插入排序
插入排序法是在已经排好序的子数列中反复插入一个新元素来对数列值进行排序,直到整个数列全部排好序。代码如下:
package com.hyq.chapter01; import java.util.Random; /** * Created by Administrator on 2016/10/8. */ public class InsertionSort { public static void sort(int[] nums){ for(int i=1;i<nums.length;i++){ int currentNum = nums[i]; int j; for(j = i-1;j>=0&&nums[j]>currentNum;j--){ nums[j+1] = nums[j]; } nums[j+1] = currentNum; } } public static void main(String[] args) { int[] nums = {13,15,52,25,6,11,37,43,12,1}; sort(nums); for (int n:nums) { System.out.print(n+" "); } } }
3、冒泡排序
冒泡排序算法需要遍历几次数组。在每次遍历中,比较连续相邻的元素。如果某一对元素是降序,则互换它们的值;否则保持不变。代码如下:
package com.hyq.chapter03; /** * Created by Administrator on 2016/10/11. */ public class BubbleSort { public static void bubbleSort(int[] list){ boolean needNextPass = true; for(int k=1;k<list.length&&needNextPass;k++){ needNextPass = false; for (int i=0;i<list.length-k;i++){ if (list[i]>list[i+1]){ int temp = list[i]; list[i] = list[i+1]; list[i+1] = temp; needNextPass = true; } } } } public static void main(String[] args) { int[] list = {1,4,25,6,7,3,6,2,0}; bubbleSort(list); for (int i=0;i<list.length;i++){ System.out.print(list[i]+" "); } } }
4、归并排序
归并排序可以描述为:算法将数组分为两半,对每部分递归地应用归并排序。在两部分都排好序后,对它们进行归并。代码如下:
package com.hyq.chapter03; /** * Created by Administrator on 2016/10/11. */ public class MergeSort { public static void mergeSort(int[] list){ if (list.length>1){ int[] firstHalf = new int[list.length/2]; System.arraycopy(list,0,firstHalf,0,list.length/2); mergeSort(firstHalf); int secondHalfLength = list.length-list.length/2; int[] secondHalf = new int[secondHalfLength]; System.arraycopy(list,list.length/2,secondHalf,0,secondHalfLength); mergeSort(secondHalf); int[] temp = merge(firstHalf,secondHalf); System.arraycopy(temp,0,list,0,temp.length); } } private static int[] merge(int[] list1,int[] list2){ int[] temp = new int[list1.length+list2.length]; int current1 = 0; int current2 = 0; int current3 = 0; while (current1<list1.length&¤t2<list2.length){ if (list1[current1]<list2[current2]){ temp[current3++] = list1[current1++]; }else{ temp[current3++] = list2[current2++]; } } while (current1<list1.length){ temp[current3++] = list1[current1++]; } while (current2<list2.length){ temp[current3++] = list2[current2++]; } return temp; } public static void main(String[] args) { int[] list = {3,2,6,1,83,2,33,4,11}; mergeSort(list); for (int i = 0;i<list.length;i++){ System.out.print(list[i]+" "); } } }
5、快速排序
快速排序算法在数组中选择一个称为主元(pivot)的元素,将数组分为两部分,使得第一部分中的所有元素都小于或等于主元,而第二部分中的所有元素都大于主元。对第一部分递归的应用快速排序算法,然后对第二部分递归地应用快速排序算法。
package com.hyq.chapter03; /** * Created by Administrator on 2016/10/11. */ public class QuickSort { private static int num = 0; public static void quickSort(int[] list){ quickSort(list,0,list.length-1); } private static void quickSort(int[] list,int first,int last){ if (last>first){ int pivotIndex = partition(list,first,last); quickSort(list,first,pivotIndex-1); quickSort(list,pivotIndex+1,last); } } private static int partition(int[] list,int first,int last){ num++; int pivot = list[first]; int low = first+1; int high = last; while(high>low){ //查找第一个大于pivot的元素 while (low<=high&&list[low]<=pivot){ low++; } //查找第一个小于pivot的元素 while(low<=high&&list[high]>pivot){ high--; } //交换元素 if (high>low){ int temp = list[high]; list[high] = list[low]; list[low] = temp; } } while (high>first&&list[high]>=pivot){ high--; } if (pivot>list[high]){ list[first] = list[high]; list[high] = pivot; return high; }else{ return first; } } public static void main(String[] args){ int[] list = {21,32,55,216,4,62,2,26,57,11,44,-99,22,54,124,5,777,67,87,-9,10}; quickSort(list); for (int i:list) { System.out.print(i+" "); } System.out.println(); System.out.println(num); } }