冒泡排序
public static void bubbleSort(int[] a) {
int temp;
for(int i =0;i<a.length -1;i++){
for(int j=0;j<a.length-1-i;j++){
if(a[j]>a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
public static void bubblesort2(int[] a){
int len = a.length;
boolean flag = true;
while(flag){
flag = false;
for (int i = 0; i < len; i++) {
if(a[i]>a[i+1]){
int temp = a[i];
a[i+1] = a[i];
temp = a[i+1];
flag = true;
}
}
}
len--;
}
插入排序
public static void inserSort(int[] a){
int j;
for(int p =1;p<a.length;p++){
int temp = a[p];
for(j=p;temp<a[j-1];j--)
a[j] = a[j-1];
a[j] = temp;
}
}
二分插入排序
public static int[] binaryInsertSort2(int[] array) {
for (int i = 0; i < array.length - 1; i++) {
int temp = array[i + 1]; // 需要插入的数
if (array[i] > array[i + 1]) {
int l = 0; // 有序队列中左边标识
int r = i; // 有序队列中右边标识
while (l < r) {
int mid = (l + r) / 2; // 永远指向l->r中间的那个值,中间值与temp(需要插入的值)比较
if (array[mid] > temp) {
r--; // 通过while循环,二分折半搜索temp应该插入的位置
} else {
l++;
}
}
// 运行到这里,l==r,即是temp应该插入的位置是array[l](或者array[r])
for (int j = i + 1; j > l; j--) {
array[j] = array[j - 1]; // 将l -> i的数都往后移动一位
}
array[l] = temp; // 将temp插入到l位置
}
}
return array;
}
希尔排序
public static int[] shellSort(int[] a) {
int j;
for(int gap=a.length/2;gap>0;gap/=2){
for(int i = gap;i<a.length ;i++){
int temp = a[i];
for(j=i;j>=gap&&temp<a[j-gap];j-=gap){
a[j] = a[j-gap];
}
a[j] = temp;
}
}
return a;
}
二分查找
public static int binarySearch(int[] arr, int x) {
int low = 0;
int high = arr.length -1;
while (low <=high){
int middle = (low +high)/2;
if(x == arr[middle]){
return middle;
}else if(x<arr[middle]){
high = middle -1;
}else {
low = middle +1;
}
}
return -1;
}
快速排序
public static void sort(int[] a, int l, int r){
if(l<r){
int p = a[l];
int low = l;
int high =r;
while(low < high){
while(low<high&&a[high]>=p)
high--;
a[low] = a[high];
while(low<high&&a[low]<=p)
low ++;
a[high] = a[low];
}
a[low] = p;
sort(a,l,low-1);
sort(a,low+1,r);
}
}
直接选择排序
public static void directSelectSort(int[] a){
for (int i = 0; i < a.length - 1; i++) {
int min = a[i];
for (int j = i+1; j < a.length; j++) {
if(a[j]<min){
min = a[j];
a[j] = a[i];
a[i] = min;
}
}
}
}
堆排序
private static int leftChild(int i){
return 2*i+1;
}
private static void perceDown(int[] a, int i,int n){
int child;
int temp;
for(temp = a[i];leftChild(i)<n ;i=child){
child = leftChild(i);
if(child!=n-1&&a[child]<a[child+1])
child ++;
if(temp<a[child]) //构建最大堆
a[i] = a[child];
else
break;
}
a[i] = temp;
}
private static void swap(int a[],int i,int k){
if(i<a.length&&k<a.length){
int temp = a[i];
a[i] = a[k];
a[k] = temp;
}
}
public static void heapSort(int a[]){
for(int i=a.length/2-1;i>=0;i--){
perceDown(a, i, a.length); //构建初始堆
}
for(int i = a.length -1;i>=0;i--){
swap(a, 0, i); //删除最大
perceDown(a, 0, i); //构建最大堆
}
}
归并排序
public static void mergeSort(int[] array, int low, int high) {
int middle = (low + high) / 2;
if (low < high) {
mergeSort(array, low, middle);
mergeSort(array, middle + 1, high);
merge(array, low, middle, high);
}
}
public static void merge(int[] array, int low, int middle, int high) {
int[] temp = new int[high - low + 1];
int i = low;
int j = middle + 1;
int k = 0;
while (i <= middle && j <= high) {
if (array[i] < array[j]) {
temp[k++] = array[i++];
} else {
temp[k++] = array[j++];
}
}
while (i <= middle) {
temp[k++] = array[i++];
}
while (j <= high) {
temp[k++] = array[j++];
}
for (int m = 0; m < temp.length; m++) {
array[m + low] = temp[m];
}
}
由于基数排序很少接触,所以没有列入其中。