JSE基礎---基礎小演算法
阿新 • • 發佈:2019-01-05
排序
選擇排序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; 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;
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;
}
return -1;
}
}
面試題:
給定一個有序的陣列,如果往該陣列中儲存一個元素,並保證這個陣列還是有序的,
那麼這個元素的儲存角標如何獲取。
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]);
}
}
}