版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Slow_Wakler/article/details/69732632
排序
- 直接插入排序
- 冒泡排序
- 快速排序
- 归并排序
- 堆排序
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]) {
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;
}
}
//冒泡排序
public static void BubbleSort(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--;
}
}
//快速排序
public static int part(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;
}
public static void qsort(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);
}
}
//归并排序
public static void merge(int[] arr, int l, int mid, int r) {
int[] temp = new int[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];
}
public static void msort(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);
}
}
//堆排序
public static void adjust(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;
}
}
public static void hsort(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);
}
}
//测试
public static void main(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.*;
public class Main {
//二分查找,找不到返回-1
public static int bitSearch(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;
else if(target < arr[mid]) r = mid-1;
else l = mid+1;
}
return -1;
}
//二分查找,找出不小于target的第一个元素位置
public static int lower_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的第一个元素位置
public static int upper_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;
}
//测试
public static void main(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: 13 27 38 49 65 76 97
Not element!
the index of element in the arr1 that is 76: 5
-----------------------------------------------------------
In arr2: 13 27 38 38 38 49 65 65 65 65 76 97
the test of lower_bound function:
the index of first element in the arr2 that is not less than 12: 0
the index of first element in the arr2 that is not less than 65: 6
the index of first element in the arr2 that is not less than 38: 2
the index of first element in the arr2 that is not less than 29: 2
the index of first element in the arr2 that is not less than 97: 11
the index of first element in the arr2 that is not less than 98: 12
the test of upper_bound function:
the index of first element in the arr2 that is greater than 12: 0
the index of first element in the arr2 that is greater than 65: 10
the index of first element in the arr2 that is greater than 38: 5
the index of first element in the arr2 that is greater than 29: 2
the index of first element in the arr2 that is greater than 97: 12
the index of first element in the arr2 that is greater than 98: 12