冒泡、直接插入、歸併排序、快速排序
阿新 • • 發佈:2018-12-04
參考:https://www.cnblogs.com/chengxiao/p/6103002.html
http://www.cnblogs.com/chengxiao/p/6104371.html
https://www.cnblogs.com/chengxiao/p/6129630.html
https://www.cnblogs.com/chengxiao/p/6194356.html
氣泡排序:
package com.sxj.Paixu;
/**
* <pre>
* author: sxj
* blog : https://www.cnblogs.com/sxj1/
* time : 2018/11/28
* desc :
* </pre>
*/
public class Maopao {
public static void bubblesort(int[] arr) {
//設定一個標誌
boolean flag=true;
for(int i=0;i<arr.length-1&&flag;i++) {
//無交換
flag=false;
for(int j=arr.length-2;j>=i;j--) {
if(arr[j]>arr[j+1]) {
Kuaishu.swap(arr, j, j+1);
//有交換
flag=true;
}
}
}
}
}
直接插入:
package com.sxj.Paixu;
/**
* @author sxj
* @param arr
*
*/
public class Charu {
public static void insertionSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int j = i;
while (j > 0 && arr[j] < arr[j - 1]) {
Kuaishu.swap(arr,j,j-1);
j--;
}
}
}
}
快速排序:(關鍵字的比較和交換是跳躍進行的,因此不穩定)
package com.sxj.Paixu;
/**
* <pre>
* author: sxj
* blog : https://www.cnblogs.com/sxj1/
* time : 2018/11/28
* desc :
* </pre>
*/
public class Kuaishu {
public static void sort(int[] array,int low ,int hig) {
int pivot;
if(low<hig) {
//算出樞軸值
pivot=parttion(array,low,hig);
//分別對低、高子表遞迴排序
sort(array,low,pivot-1);
sort(array,pivot+1,hig);
}
}
public static void sort(int []arr){
sort(arr,0,arr.length-1);
}
public static int parttion(int[] array,int low,int hig) {
// int pivot;
//三數取中法
int mid=low+(hig-low)/2;
//現將hig置最高點,那麼中間值只有low或者mid,接下來就是兩個數的比較問題
if(array[low]>array[hig]) {
swap(array,low,hig);
}
if(array[mid]>array[hig]) {
swap(array,mid,hig);
}
if(array[low]<array[mid]) {
swap(array,low,mid);
}
int key=array[low];
//排序過程,key值左邊的數都小於key,右邊的數大於key,最後排於low=hig
while(low<hig) {
while(low<hig&&array[hig]>=key) {
hig--;
}
array[low]=array[hig];
while(low<hig&&array[low]<=key) {
low++;
}
array[hig]=array[low];
}
//low為
array[low]=key;
return low;
}
public static void swap(int[] arr,int a,int b) {
int temp=arr[a];
arr[a]=arr[b];
arr[b]=temp;
}
}
歸併排序:
package com.sxj.Paixu;
//import java.util.Arrays;
/**
* <pre>
* author: sxj
* blog : https://www.cnblogs.com/sxj1/
* time : 2018/11/28
* desc :
* </pre>
*/
//import java.util.Arrays;
public class Guibing {
/*
*
public static void main(String []args){
int []arr = {9,8,7,6,5,4,3,2,1};
sort(arr);
System.out.println(Arrays.toString(arr));
}
*/
public static void sort(int []arr){
int []temp = new int[arr.length];//在排序前,先建好一個長度等於原陣列長度的臨時陣列,避免遞迴中頻繁開闢空間
sort(arr,0,arr.length-1,temp);
}
private static void sort(int[] arr,int left,int right,int []temp){
if(left<right){
int mid = (left+right)/2;
sort(arr,left,mid,temp);//左邊歸併排序,使得左子序列有序
sort(arr,mid+1,right,temp);//右邊歸併排序,使得右子序列有序
merge(arr,left,mid,right,temp);//將兩個有序子數組合並操作
}
}
private static void merge(int[] arr,int left,int mid,int right,int[] temp){
int i = left;//左序列指標
int j = mid+1;//右序列指標
int t = 0;//臨時陣列指標
while (i<=mid && j<=right){
if(arr[i]<=arr[j]){
temp[t++] = arr[i++];
}else {
temp[t++] = arr[j++];
}
}
while(i<=mid){//將左邊剩餘元素填充進temp中
temp[t++] = arr[i++];
}
while(j<=right){//將右序列剩餘元素填充進temp中
temp[t++] = arr[j++];
}
t = 0;
//將temp中的元素全部拷貝到原陣列中
while(left <= right){
arr[left++] = temp[t++];
}
}
}
測試程式碼:
package com.sxj.Paixu;
import java.util.Arrays;
/**
* <pre>
* author: sxj
* blog : https://www.cnblogs.com/sxj1/
* time : 2018/11/28
* desc :
* </pre>
*/
public class Test {
public static void main(String[] args) {
// TODO 自動生成的方法存根
int[] arr1 = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
int[] arr2= {1,3,4,9,4,2,5,1,0,3,7};
// Guibing s=new Guibing();
Guibing.sort(arr1);
Kuaishu.sort(arr2);
System.out.println(Arrays.toString(arr1));
System.out.println(Arrays.toString(arr2));
}
}