Java学习-常见排序算法
目录
- 选择排序实现原理
每次选择最小的元素,为了避免不必要的交换,每一轮采用比而不换的策略
- 代码实现
1 public class selectSort_practice { 2 private static void swap(int[] data,int i,int j) { 3 // TODO Auto-generated method stub 4 data[i]^=data[j]; 5 data[j]^=data[i]; 6 data[i]^=data[j]; 7 } 8 private static void selectsort(int[] data) { 9 // TODO Auto-generated method stub 10 int len=data.length; 11 int idx; 12 for(int i=0;i<len-1;++i) 13 { 14 idx=i; 15 for(int j=i+1;j<len;++j) 16 { 17 if(data[j]<data[idx]) 18 idx=j; 19 } 20 if(idx!=i) 21 swap(data, idx, i); 22 } 23 } 24 private static void disArr(int[] data) { 25 // TODO Auto-generated method stub 26 int len=data.length; 27 for(int i=0;i<len;++i) 28 System.out.println(data[i]+"\t"); 29 } 30 public static void main(String[] args) { 31 int[] data={1,32,12,43,54,2,54,767,678,3,434}; 32 disArr(data); 33 selectsort(data); 34 disArr(data); 35 } 36 }
- 实现原理
每次和相邻的元素进行比较
- 代码实现
1 public class popSort_practice { 2 private static void disArr(int[] data) { 3 // TODO Auto-generated method stub 4 int len=data.length; 5 for(int i=0;i<len;++i) 6 System.out.println(data[i]+"\t"); 7 } 8 private static void swap(int[] data,int i,int j) { 9 // TODO Auto-generated method stub 10 data[i]^=data[j]; 11 data[j]^=data[i]; 12 data[i]^=data[j]; 13 } 14 private static void mypopsort(int[]data) { 15 // TODO Auto-generated method stub 16 int len=data.length; 17 for(int i=0;i<len-1;++i) 18 { 19 for(int j=0;j<len-i-1;++j) 20 { 21 if(data[j]>data[j+1]) 22 swap(data, j, j+1); 23 } 24 } 25 } 26 public static void main(String[] args) { 27 int[]p={1,4,5,7,3,2,45,6,7,232,32}; 28 int len=p.length; 29 disArr(p); 30 System.out.println("after sort:"); 31 mypopsort(p); 32 disArr(p); 33 } 34 }
- 实现原理
分治思想来实现
- 代码实现
1 import java.nio.charset.MalformedInputException; 2 3 public class MergeSort { 4 private static void merge(int []src,int startIdx,int midIdx,int endIdx) { 5 // TODO Auto-generated method stub 6 int []temp=new int[src.length]; 7 int i=startIdx,j=midIdx+1,k=startIdx; 8 while(i<=midIdx&&k<=endIdx) 9 { 10 if(src[i]<src[j]) 11 temp[k++]=src[i++]; 12 else 13 temp[k++]=src[j++]; 14 } 15 while(i<=midIdx) 16 temp[k++]=src[i++]; 17 while(j<=endIdx) 18 temp[k++]=src[j++]; 19 while(startIdx<=endIdx) 20 { 21 src[startIdx]=temp[startIdx]; 22 ++startIdx; 23 } 24 } 25 26 private static void mergeSort(int[]src,int startIdx,int endIdx) { 27 // TODO Auto-generated method stub 28 if(startIdx<endIdx) 29 { 30 int midIdx=startIdx+(endIdx-startIdx)/2; 31 mergeSort(src,startIdx,midIdx); 32 mergeSort(src,midIdx+1,endIdx); 33 merge(src,startIdx,midIdx,endIdx); 34 } 35 36 } 37 private static void disArr(int[] arr) { 38 // TODO Auto-generated method stub 39 int len=arr.length; 40 for(int i=0;i<len;++i) 41 System.out.println(arr[i]+"\t"); 42 } 43 public static void main(String[] args) { 44 int []p={1,13,4,15,2,43,23,54,65,232,34,564}; 45 int len=p.length; 46 disArr(p); 47 System.out.println("after sort:"); 48 mergeSort(p, 0, len-1); 49 disArr(p); 50 } 51 }
- 实现原理
右边:大而移动,小而赋值
左边:小而移动,大而赋值
- 代码参考
1 public class quickSort_practice { 2 private static void disArr(int[] data) { 3 // TODO Auto-generated method stub 4 int len=data.length; 5 for(int i=0;i<len;++i) 6 System.out.println(data[i]+"\t"); 7 } 8 private static void swap(int[] data,int i,int j) { 9 // TODO Auto-generated method stub 10 data[i]^=data[j]; 11 data[j]^=data[i]; 12 data[i]^=data[j]; 13 } 14 private static void quicksort(int[]p,int low,int high) { 15 // TODO Auto-generated method stub 16 int l,h,pivot; 17 if(low<high) 18 { 19 l=low; 20 h=high; 21 pivot=p[low]; 22 while(l<h) 23 { 24 //右边:大而移动,小而赋值 25 while(p[h]>=pivot&&l<h) 26 --h; 27 p[l]=p[h]; 28 //左边:小而移动,大而赋值 29 while(p[l]<=pivot&&l<h) 30 ++l; 31 p[h]=p[l]; 32 } 33 p[l]=pivot; 34 quicksort(p, low, l-1); 35 quicksort(p, l+1, high); 36 } 37 } 38 public static void main(String[] args) { 39 int[] data=new int[]{1,3,5,7,9,2,4,6,8,0}; 40 disArr(data); 41 System.out.println("after sort:\n"); 42 quicksort(data,0,data.length-1); 43 disArr(data); 44 } 45 }