Java实现基本的算法

1、队列
package com.test.luo;

public final class Queue2 {
private Object[] objects;
    private int size;
    private int head;
    private int end;

    public Queue2(int size) {
        this.objects = new Object[size];
        this.head = 0;
        this.end = 0;
        this.size = 0;
    }

    public void push(Object object) throws Exception {
        if (this.size > objects.length)
            throw new Exception("Queue is full!");
        objects[end++] = object;
        size++;
    }

    public Object pop() throws Exception {
        if (this.size == 0)
            throw new Exception("Queue is empty!");
        if (head == objects.length)
            this.head = 0;
        size--;
        return objects[head++];
    }

    public Object peek() throws Exception {
        if (this.size == 0)
            throw new Exception("Queue is empty!");
        return objects[head];
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public boolean isFull() {
        return size == objects.length;
    }

    public int getSize() {
        return size;
    }
}
2、栈
package com.test.luo;

public class Stack {
private Object[] objects;
    private int size;
    private int head;
    public Stack(int size) {
        this.objects = new Object[size];
        this.head = 0;
        this.size = 0;
    }
//进
    public void push(Object object) throws Exception {
        if (this.size > objects.length)
            throw new Exception("stack is full!");
        objects[head++] = object;
        size++;
    }
    //出
    public Object pop() throws Exception {
        if (this.size == 0)
            throw new Exception("stack is empty!");
        size--;
        return objects[head--];
    }
}
3、斐波那契数列
package com.test.luo;
public class feibona {
public static void main(String[] args) {
System.out.println(fib(7));
System.out.println(fib2(7,1,1));
}
//递归方法
public static int fib(int month){
//if(month <= 0) return 0;
        if(month == 2 || month == 1){
        return 1;
        }
        else{
            return fib(month-1)+fib(month-2);
        }
    }
//for循环方法
public static int fib2(int month,int first,int second) {
int sum=0;
for (int i = 3; i <= month; i++) {
sum = first + second;
first = second;
second = sum;
}
return sum;
}
//1,1,2,3,5,8,13,21
}
4、冒泡排序
package com.java.luo;

import java.util.Arrays;
public class xuanzhepaixu {
public static void main(String[] args) {
int[] arr = {34,19,11,109,3,56};
bubbleSort(arr);
}
public static void bubbleSort(int[] arr)
{
for(int x=0; x<arr.length-1; x++)
{
for(int y=0; y<arr.length-1-x; y++)
{
if(arr[y]>arr[y+1])
{
int temp  = arr[y];
arr[y] = arr[y+1];
arr[y+1] = temp;
}
}
}
System.out.println(Arrays.toString(arr));
}
}
5、选择排序
package com.java.luo;

public class Test {
public static void main(String[] args) {
int[] arr = {34,19,11,109,3,56};
selectSort(arr);
selectSort2(arr);
}
//选择排序第一种
private static void selectSort2(int[] arr) {
for(int x =0;x<arr.length-1;x++) {
int num = arr[x];
int index = x;
for(int y=x+1;y<arr.length;y++) {
if(num > arr[y] ) {
num = arr[y];
index = y;
}
}
if(index != x) {
int temp = arr[x];
arr[x] = arr[index];
arr[index] =temp;
}
}
System.out.print("[");
for(int x=0; x<arr.length; x++)
{
if(x!=arr.length-1)
System.out.print(arr[x]+", ");
else
System.out.println(arr[x]+"]");
}

}
//选择排序第二种
private static void selectSort(int[] arr) {
for(int x=0;x<arr.length-1 ;x++) {
for(int y=x+1;y<arr.length;y++) {
if(arr[x]>arr[y]) {
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
}
System.out.print("[");
for(int x=0; x<arr.length; x++)
{
if(x!=arr.length-1)
System.out.print(arr[x]+", ");
else
System.out.println(arr[x]+"]");
}
}

}

6、折半查找(也称二分法查找)

package com.java.luo;

public class zhengbanchazhao {
public static void main(String[] args) {
int[] arr = {13,15,19,28,33,45,78,106};
System.out.println(halfSearch(arr,45));
}
private static int halfSearch(int[] arr, int key) {
int max,min,mid;
min = 0;
max = arr.length - 1;
mid = (max+min)/2;
while(arr[mid] != key) {
if(key>arr[mid]) {
min = mid + 1;
}else if(key<arr[mid]){
max = mid - 1;
}
if(max < min) {
return -1;
}
mid = (max+min)/2;
}
return mid;
}
}

7、快速排序(最难的一种,原理简单但是实现起来比较难)

package com.java.luo;

import java.util.Arrays;
public class QuickSort {
public static void main(String[] args) {
int arr[] = { 49, 38, 65, 97, 76, 13, 27, 49 };
quickSort(arr);
System.out.println(Arrays.toString(arr));
}
private static void quickSort(int[] arr) {
sort(arr, 0, arr.length - 1);
}
private static void sort(int[] a, int low, int hight) {
int i, j, index;
if (low > hight) {
return;
}
i = low;
j = hight;
index = a[i];// 用于表的第一个记录做基准
while (i < j) {// 从表的两段交替向中间扫描
while (i < j && a[j] >= index)
j--;
if (i < j)
a[i++] = a[j];//用比基准小的记录替换低位记录
while(i<j&&a[i]<index) {
i++;
}
if(i<j) {//用比基准大的记录替换高位记录
a[j--]=a[i];
}
a[i] = index;//将基准数值替换回a[i]
sort(a,low,i-1);//对底子表进行递归排序
sort(a,i+1,hight);//对高子表进行递归排序
}
}
}


总结:这些都是java基础必备的知识,以及面试、笔试的时候常见涉及的问题。一定要好好去研究他们实现的原理。尤其是最后一个快速排序可能代码实现的理解有点难,但是功夫不负有心人,要静得下心来去研究原理。最后推荐一片博客专门针对快速排序做了仔细的讲解。博客地址:http://www.cnblogs.com/hexiaochun/archive/2012/09/03/2668324.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值