排序算法

来源:互联网 发布:冰川网络手游怎么样 编辑:程序博客网 时间:2024/04/29 21:48

//直接插入排序

int currentValue;int count=0;for (int i = 1; i < array.length; i++) {currentValue=array[i];int position=i;for (int j = i-1; j >=0; j--) {count++;if(currentValue<array[j]){array[j+1]=array[j];position--;}else {break;}array[position]=currentValue;}}System.out.println(count);

//快速排序

public void SortList(int[] array) {// TODO Auto-generated method stubsubSort(array,0,array.length-1);System.out.println(count);}private void subSort(int[] array, int low, int high) {if(array==null||low>=high){return;}int part=partion(array,low,high);if(part==low){subSort(array,low+1,high);}else if (part==high) {subSort(array, low, high-1);}else {subSort(array, low, part-1);subSort(array, part+1, high);}}private int partion(int[] array, int low, int high) {int position=low;while(low<high){count++;if(position<high){if (array[low]<=array[high]) {high--;}else {int temp=array[high];array[high]=array[low];array[low]=temp;position=high;low++;}}else {if (array[low]<=array[high]) {low++;}else {int temp=array[high];array[high]=array[low];array[low]=temp;position=low;high--;}}}return position;}

//冒泡排序

public void SortList(int[] array) {int count=0;if(array==null||array.length<1){return;}for (int i = 1; i < array.length; i++) {for (int j = 0; j < array.length-i; j++) {count++;if (array[j]>array[j+1]) {int temp=array[j];array[j]=array[j+1];array[j+1]=temp;}}}System.out.println(count);}



1 0
原创粉丝点击