1.冒泡排序
package 冒泡排序;
public class Bubble_sort {
public static void sort(int[] array){
int len=array.length;
for (int i=0;i<len;i++){
for (int j = 0;j<len-1-i;j++){
if(array[j]>array[j+1]){
int t=array[j];
array[j]=array[j+1];
array[j+1]=t;
}
}
}
}
public static void print(int[] array){
System.out.print("array:");
for (int i=0;i<array.length;i++){
System.out.print(array[i]+" ");
}
System.out.println();
}
public static void main(String[] args) {
int[] array=new int[20];
for (int i=0;i<20;i++){
array[i]=(int)(Math.random()*100);
}
print(array);
sort(array);
print(array);
}
}
2.选择排序
package 选择排序;
public class Select_sort {
public static void sort(int[] array){
int len=array.length;
int minIndex;
for (int i=0;i<len-1;i++){
minIndex=i;
for (int j=minIndex+1;j<len;j++){
if(array[j]<array[minIndex]){
minIndex=j;
}
}
int t=array[minIndex];
array[minIndex]=array[i];
array[i]=t;
}
}
public static void print(int[] array){
System.out.print("array:");
for (int i=0;i<array.length;i++){
System.out.print(array[i]+" ");
}
System.out.println();
}
public static int[] gen(int n){
int[] array=new int[n];
for (int i=0;i<n;i++){
array[i]=(int)(Math.random()*100);
}
return array;
}
public static void main(String[] args) {
int[] array=gen(20);
print(array);
sort(array);
print(array);
}
}
3.插入排序
package 插入排序;
public class Insert_sort {
public static void sort(int[] array){
int len=array.length;
int preIndex,cur;
for (int i=1;i<len;i++){
preIndex=i-1;
cur=array[i];
while (preIndex>=0&&cur<array[preIndex]){
array[preIndex+1]=array[preIndex];
preIndex--;
}
array[++preIndex]=cur;
}
}
public static void print(int[] array){
System.out.print("array:");
for (int i=0;i<array.length;i++){
System.out.print(array[i]+" ");
}
System.out.println();
}
public static int[] gen(int n){
int[] array=new int[n];
for (int i=0;i<n;i++){
array[i]=(int)(Math.random()*100);
}
return array;
}
public static void main(String[] args) {
int[] array=gen(20);
print(array);
sort(array);
print(array);
}
}
4.希尔排序
package 希尔排序;
public class Shell_sort {
public static void sort(int[] array,int m){
int len=array.length;
for (int gap=len/m;gap>0;gap=gap/2){
for (int j=gap;j<len;j++){
int t=j;
int curVal=array[t];
while(t-gap>=0&&curVal<array[t-gap]){
array[t]=array[t-gap];
t-=gap;
}
array[t]=curVal;
}
}
}
public static void print(int[] array){
System.out.print("array:");
for (int i=0;i<array.length;i++){
System.out.print(array[i]+" ");
}
System.out.println();
}
public static int[] gen(int n){
int[] array=new int[n];
for (int i=0;i<n;i++){
array[i]=(int)(Math.random()*100);
}
return array;
}
public static void main(String[] args) {
int[] array=gen(20);
print(array);
sort(array,7);
print(array);
}
}
5.归并排序
package 归并排序;
import oracle.jrockit.jfr.parser.FLREventInfo;
public class Merge_sort {
public static int[] sort(int[] array){
int len=array.length;
return merge(array,0,len-1);
}
public static int[] merge(int[] array,int left,int right){
int mid=left+(right-left)/2;
if(right==left){
return new int[]{array[left]};
}
int[] leftArray=merge(array,left,mid);
int[] rightArray=merge(array,mid+1,right);
return mergeSort(leftArray,rightArray);
}
public static int[] mergeSort(int[] left,int[] right){
int len1=left.length;
int len2=right.length;
int[] rs=new int[len1+len2];
int lefti=0,righti=0;
int i;
for (i=0;lefti<len1&righti<len2;i++){
if(left[lefti]<right[righti]){
rs[i]=left[lefti++];
}else{
rs[i]=right[righti++];
}
}
if(lefti==len1){
while (righti<len2){
rs[i++]=right[righti++];
}
}
if(righti==len2){
while (lefti<len1){
rs[i++]=left[lefti++];
}
}
return rs;
}
public static void print(int[] array){
System.out.print("array:");
for (int i=0;i<array.length;i++){
System.out.print(array[i]+" ");
}
System.out.println();
}
public static int[] gen(int n){
int[] array=new int[n];
for (int i=0;i<n;i++){
array[i]=(int)(Math.random()*100);
}
return array;
}
public static void main(String[] args) {
int[] array=gen(20);
print(array);
int[] rs=sort(array);
print(rs);
}
}
6.快速排序
package 快速排序;
public class Quick_sort {
public static void sort(int[] array){
quictSort(array,0,array.length-1);
}
public static void quictSort(int[] array,int left,int right){
if(left>=right){
return;
}
int l=left;
int r=right;
int curVal=array[left];
while (l<r){
while (l<r&&array[r]>curVal){
r--;
}
if (l<r){
array[l++]=array[r];
}
while (l<r&&array[l]<curVal){
l++;
}
if(l<r){
array[r--]=array[l];
}
}
array[l]=curVal;
quictSort(array,left,l-1);
quictSort(array,l+1,right);
}
public static void print(int[] array){
System.out.print("array:");
for (int i=0;i<array.length;i++){
System.out.print(array[i]+" ");
}
System.out.println();
}
public static int[] gen(int n){
int[] array=new int[n];
for (int i=0;i<n;i++){
array[i]=(int)(Math.random()*100);
}
return array;
}
public static void main(String[] args) {
int[] array=gen(20);
print(array);
sort(array);
print(array);
}
}
7.堆排序
package 堆排序;
public class Heap_sort {
public static void sort(int[] array){
int len=array.length;
for (int i=len/2;i>=0;i--){
adjustHeap(array,i,len);
}
for (int i=len-1;i>0;i--){
int t=array[0];
array[0]=array[i];
array[i]=t;
adjustHeap(array,0,i);
}
}
public static void adjustHeap(int[] array,int i,int len){
int t=array[i];
for (int k=2*i+1;k<len;k=2*k+1){
if(k+1<len&&array[k]<array[k+1]){
k=k+1;
}
if(array[k]>t){
array[i]=array[k];
i=k;
}else{
break;
}
}
array[i]=t;
}
public static void print(int[] array){
System.out.print("array:");
for (int i=0;i<array.length;i++){
System.out.print(array[i]+" ");
}
System.out.println();
}
public static int[] gen(int n){
int[] array=new int[n];
for (int i=0;i<n;i++){
array[i]=(int)(Math.random()*100);
}
return array;
}
public static void main(String[] args) {
int[] array=gen(20);
print(array);
sort(array);
print(array);
}
}
具体算法流程详细
|