package data; public class Sort { String endline = System.getProperty("line.separator"); //选择排序 public void selectSort(int[] arr){ int len = arr.length; int k; int tmp; for(int i = 0;i<len;i++){ for(int l:arr){ System.out.print(l+" "); } System.out.print(endline); k= i; for(int j =i;j<len;j++){ if(arr[j]<arr[k]){ k=j; } } tmp= arr[i]; arr[i]=arr[k]; arr[k]=tmp; } } //冒泡排序 public void popSort(int[] arr){ int len =arr.length; int tmp; for(int i=0;i<len;i++){ for(int l:arr){ System.out.print(l+" "); } System.out.print(endline); for(int j=0;j<len-i-1;j++){ if(arr[j] > arr[j+1]){ tmp =arr[j]; arr[j] = arr[j+1]; arr[j+1] = tmp; } } } } //insertSort插入排序 public void insertSort(int[] arr){ int len = arr.length; int tmp; for(int i =1;i<len;i++){ for(int l:arr){ System.out.print(l+" "); } System.out.print(endline); for(int j =i;j>0;j--){ if(arr[j]<arr[j-1]){ tmp = arr[j-1]; arr[j-1] = arr[j]; arr[j] = tmp; } else{ break; } } } } //shellSort希尔排序 public void shellSort(int[] arr){ int len = arr.length; int tmp; int k; for(int l:arr){ System.out.print(l+" "); } System.out.print(endline); for(int d =len/2;d >0; d /= 2 ){ for(int i =0;i<=d;i++){ for(int j=i+d;j<len;j +=d){ for(k = j;k>=d;k -=d){ if(arr[k]<arr[k-d]){ tmp = arr[k-d]; arr[k-d]=arr[k]; arr[k]=tmp; } } } } for(int l:arr){ System.out.print(l+" "); } System.out.print(endline); } } }