# JSE基础---基础小算法

## 排序

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));
}
}

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[i];
arr[i]=arr[j];
arr[j]=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]<key)
min=mid+1;
else
return mid;
}
return -1;
}
}

/**
* 使用递归的方式使用二分查找法
*/
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(key<a[mid])
return rank(key, a, min, mid-1);
else if(key>a[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]<key)
min=mid+1;
else
return mid+1;
}
return min;
}
}

### 进制转换

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;
}
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120