选择排序1:
public class SortDemo1 {
public static void main(String[] args) {
int []arr= {12,16,3,64,-5,64};
for (int i = 0; i < arr.length-1; i++) {
for (int j = i+1; j < arr.length; j++) {
if(arr[i]>arr[j]) {
int t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
}
}
System.out.println(Arrays.toString(arr));
}
}
选择排序2: 提高效率的选择排序
改进的选择排序法,增加了两个变量,一个代表最小值,一个代表最小值的角标
减少了在堆中的换位次数。
public class SortDemo3 {
public static void main(String[] args) {
int []arr= {12,16,3,64,-5,69};
for (int i = 0; i < arr.length-1; i++) {
//定义两个变量,记住最小值和角标
int num=arr[i];
int index=i;
for (int j = i+1; j < arr.length; j++) {
if(num>arr[j]) {
num=arr[j];
index=j;
}
}
//用记录的最小值角标所对应的值和当前数组值互换,减少换位次数
if(i!=index){
int t=arr[i];
arr[i]=arr[index];
arr[index]=t;
}
}
System.out.println(Arrays.toString(arr));
}
}
冒泡排序:
public class SortDemo2 {
public static void main(String[] args) {
int []arr= {12,16,3,64,-5,69};
for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; j < arr.length-i-1; j++) {
if(arr[j]>arr[j+1]) {
int t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
}
}
System.out.println(Arrays.toString(arr));
}
}
二分法查找,使用前提必须要排序:
public class HalfSearch {
public static void main(String[] args) {
int arr[]= {12,16,19,64,65,69};
int rs=getKey(arr,12);
System.out.println(rs);
}
private static int getKey(int[] arr, int key) {
int min=0;
int max=arr.length-1;
while(max>min)
{
int mid=(min+max)>>1;
if(arr[mid]>key)
max=mid-1;
else if(arr[mid]
使用递归的二分法查找
/**
* 使用递归的方式使用二分查找法
*/
public class Test5 {
public static void main(String[] args) {
int arr[]= {12,16,19,64,65,69};
int index=rank(12,arr);
System.out.println(index);
}
public static int rank(int key,int[]a) {
return rank(key, a,0,a.length-1);
}
private static int rank(int key, int[] a, int min, int max) {
if(min>max)
return -1;
int mid=(min+max)>>1;
if(keya[mid])
return rank(key, a, mid+1, max);
else return mid;
}
}
面试题:
给定一个有序的数组,如果往该数组中存储一个元素,并保证这个数组还是有序的,
那么这个元素的存储角标如何获取。
public class TestHalfSerarch {
public static void main(String[] args) {
int arr[]= {12,16,19,64,65,69};
int rs=getKey(arr,15);
System.out.println(rs);
}
private static int getKey(int[] arr, int key) {
int min=0;
int max=arr.length-1;
int mid;
while(max>min)
{
mid=(min+max)>>1;
if(arr[mid]>key)
max=mid-1;
else if(arr[mid]
把一个十进制数变成十六进制数
public class ToHexDemo {
public static void main(String[] args) {
toHex(60);
}
private static void toHex(int num) {
if(num==0) {
System.out.println('0');
return;
}
char[]chs= {'0','1','2','3',
'4','5','6','7',
'8','9','A','B',
'C','D','E','F'};
char[]arrs= new char[8];
int pos=arrs.length;
while(num!=0)
{
int temp=num&15;
arrs[--pos]=chs[temp];
num=num>>>4;
}
for (int i = pos; i < arrs.length; i++) {
System.out.print(arrs[i]);
}
}
}
通用进制转换:
public class Tranfer {
public static void main(String[] args) {
toBinary(60);
}
//十进制-->>二进制
public static void toBinary(int num) {
tranfer(num, 1, 1);
}
//十进制-->>八进制
public static void toOctal(int num) {
tranfer(num, 7, 3);
}
//十进制-->>十六进制
public static void toHex(int num) {
tranfer(num, 15, 4);
}
//转换函数
private static void tranfer(int num,int base,int offset) {
if(num==0) {
System.out.println('0');
return;
}
char[]chs= {'0','1','2','3',
'4','5','6','7',
'8','9','A','B',
'C','D','E','F'};
char[]arrs= new char[8];
int pos=arrs.length;
while(num!=0)
{
int temp=num&base;
arrs[--pos]=chs[temp];
num=num>>>offset;
}
for (int i = pos; i < arrs.length; i++) {
System.out.print(arrs[i]);
}
}
}
欧几里得算法计算最大公约数
public class Test1 {
public static void main(String[] args) {
int x,y;
x=25;
y=65;
int t=gcd(x,y);
System.out.println(t);
}
public static int gcd(int p, int q) {
if(q==0)
return p;
int r=p%q;
return gcd(q, r);
}
}
判断一个数是否是素数(质数)
public class Test3 {
public static void main(String[] args) {
int N=6;
boolean r=isPrime(N);
System.out.println(r);
}
public static boolean isPrime(int N) {
if(N<2)
return false;
for(int i=2;i*i<=N;i++) {
if(N%i==0)
return false;
}
return true;
}
}
牛顿迭代法求平方根
public class Test4 {
public static void main(String[] args) {
double c=9.0;
double t=mysqrt(c);
System.out.println(t);
}
public static double mysqrt(double c) {
if(c<0)return Double.NaN;
double t=c;
double err=1e-15;
while(Math.abs((t-c/t))>err*t) {
t=(t+c/t)/2.0;
}
return t;
}
}