import java.util.*;
publicclass Main {
//直接插入排序publicstaticvoidInsertSort(int[] arr, int n) {
for(int i = 1; i < n; i++) if(arr[i] < arr[i-1]) {
int temp = arr[i];
arr[i] = arr[i-1];
int j;
for(j = i-2; j > -1 && temp < arr[j]; j--) {
arr[j+1] = arr[j];
}
arr[j+1] = temp;
}
}
//冒泡排序publicstaticvoidBubbleSort(int[] arr, int n) {
//标记是否有交换,没有交换就不循环了int flag = 1;
//当数组剩下大于1个,需要继续冒泡while(n > 1 && flag == 1) {
flag = 0;
for(int i = 0; i < n-1; i++) {
if(arr[i] > arr[i+1]) {
flag = 1;
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
n--;
}
}
//快速排序publicstaticintpart(int[] arr, int l, int r) {
int temp = arr[l];
while(l < r) {
while(l < r && arr[r] > temp) r--;
arr[l] = arr[r];
while(l < r && arr[l] <= temp) l++;
arr[r] = arr[l];
}
arr[l] = temp;
return l;
}
publicstaticvoidqsort(int[] arr, int l, int r) {
if(l < r) {
int mid = part(arr, l, r);
qsort(arr, l, mid-1);
qsort(arr, mid+1, r);
}
}
//归并排序publicstaticvoidmerge(int[] arr, int l, int mid, int r) {
int[] temp = newint[r-l+10];
int i = l, j = mid+1;
int num = 0;
while(i <= mid && j <= r) {
if(arr[i] <= arr[j]) temp[num++] = arr[i++];
else temp[num++] = arr[j++];
}
while(i <= mid)temp[num++] = arr[i++];
while(j <= r)temp[num++] = arr[j++];
for(int k = 0; k < num; k++)arr[l+k] = temp[k];
}
publicstaticvoidmsort(int[] arr, int l, int r) {
if(l < r) {
int mid = (l+r)/2;
msort(arr, l, mid);
msort(arr, mid+1, r);
merge(arr, l, mid, r);
}
}
//堆排序publicstaticvoidadjust(int[] arr, int s, int n) {
while(s < n) {
int i = 2*s+1;
if(i >= n) break;
if(i+1 < n && arr[i] < arr[i+1]) i++;
if(arr[s] >= arr[i]) break;
int temp = arr[s];
arr[s] = arr[i];
arr[i] = temp;
s = i;
}
}
publicstaticvoidhsort(int[] arr, int n) {
for(int i = n/2; i >= 0; i--) {
adjust(arr, i, n);
}
for(int i = n-1; i > 0; i--) {
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
adjust(arr, 0, i);
}
}
//测试publicstaticvoidmain(String[] args) {
int[] arr = {49, 38, 65, 97, 76, 13, 27, 49};
int n = 8;
//快速排序//qsort(arr, 0, n-1);//for(int a : arr) System.out.print(a+(a == arr[arr.length-1] ? "\n" : " "));//归并排序//msort(arr, 0, n-1);//for(int a : arr) System.out.print(a+(a == arr[arr.length-1] ? "\n" : " "));//堆排序
hsort(arr, n);
for(int a : arr) System.out.print(a+(a == arr[n-1] ? "\n" : " "));
}
}
查找
二分查找
C++中的lower_bound函数
C++中的upper_bound函数
import java.util.*;
publicclass Main {
//二分查找,找不到返回-1publicstaticintbitSearch(int[] arr, int target) {
int l = 0, r = arr.length-1;
while(l <= r) {
int mid = (l+r)/2;
if(target == arr[mid]) return mid;
elseif(target < arr[mid]) r = mid-1;
else l = mid+1;
}
return -1;
}
//二分查找,找出不小于target的第一个元素位置publicstaticintlower_bound(int[] arr, int target) {
int l = 0, r = arr.length;
while(l < r) {
int mid = (l+r)/2;
if(arr[mid] >= target) r = mid;
else l = mid+1;
}
return r;
}
//二分查找,找出大于target的第一个元素位置publicstaticintupper_bound(int[] arr, int target) {
int l = 0, r = arr.length;
while(l < r) {
int mid = (l+r)/2;
if(arr[mid] > target) r = mid;
else l = mid+1;
}
return l;
}
//测试publicstaticvoidmain(String[] args) {
int[] arr1 = {13, 27, 38, 49, 65, 76, 97};
System.out.print("In arr1: ");
for(int a : arr1) System.out.print(a+(a == arr1[arr1.length-1] ? "\n" : " "));
int index;
if((index = bitSearch(arr1, 28)) != -1)System.out.println("the index of element in the arr1 that is 28: "+index);
else System.out.println("Not element!");
if((index = bitSearch(arr1, 76)) != -1)System.out.println("the index of element in the arr1 that is 76: "+index);
else System.out.println("Not element!");
int[] arr2 = {13, 27, 38, 38, 38, 49, 65, 65, 65, 65, 76, 97};
System.out.println("-----------------------------------------------------------");
System.out.print("In arr2: ");
for(int a : arr2) System.out.print(a+(a == arr2[arr2.length-1] ? "\n" : " "));
//测试lower_bound函数
System.out.println("the test of lower_bound function: ");
System.out.println("the index of first element in the arr2 that is not less than 12: "+lower_bound(arr2, 12));
System.out.println("the index of first element in the arr2 that is not less than 65: "+lower_bound(arr2, 65));
System.out.println("the index of first element in the arr2 that is not less than 38: "+lower_bound(arr2, 38));
System.out.println("the index of first element in the arr2 that is not less than 29: "+lower_bound(arr2, 29));
//返回最后一个元素
System.out.println("the index of first element in the arr2 that is not less than 97: "+lower_bound(arr2, 97));
System.out.println("the index of first element in the arr2 that is not less than 98: "+lower_bound(arr2, 98));
System.out.println();
//测试upper_bound函数
System.out.println("the test of upper_bound function: ");
System.out.println("the index of first element in the arr2 that is greater than 12: "+upper_bound(arr2, 12));
System.out.println("the index of first element in the arr2 that is greater than 65: "+upper_bound(arr2, 65));
System.out.println("the index of first element in the arr2 that is greater than 38: "+upper_bound(arr2, 38));
System.out.println("the index of first element in the arr2 that is greater than 29: "+upper_bound(arr2, 29));
//返回最后一个元素
System.out.println("the index of first element in the arr2 that is greater than 97: "+upper_bound(arr2, 97));
System.out.println("the index of first element in the arr2 that is greater than 98: "+upper_bound(arr2, 98));
}
}
In arr1: 13273849657697
Not element!
the index of element inthe arr1 thatis76: 5-----------------------------------------------------------
In arr2: 132738383849656565657697the test of lower_bound function:
the index offirst element inthe arr2 thatisnotless than12: 0the index offirst element inthe arr2 thatisnotless than65: 6the index offirst element inthe arr2 thatisnotless than38: 2the index offirst element inthe arr2 thatisnotless than29: 2the index offirst element inthe arr2 thatisnotless than97: 11the index offirst element inthe arr2 thatisnotless than98: 12the test of upper_bound function:
the index offirst element inthe arr2 thatisgreater than12: 0the index offirst element inthe arr2 thatisgreater than65: 10the index offirst element inthe arr2 thatisgreater than38: 5the index offirst element inthe arr2 thatisgreater than29: 2the index offirst element inthe arr2 thatisgreater than97: 12the index offirst element inthe arr2 thatisgreater than98: 12
排序直接插入排序冒泡排序快速排序归并排序import java.util.*;public class Main { //直接插入排序 public static void InsertSort(int[] arr, int n) { for(int i = 1; i < n; i++) if(arr[i] < arr[i-1]) {