实训总结20171015

快速排序:

public class QuickSort {

public static int middle(int[] array, int left, int right) {

int temp = array[left];

while (left != right) {

while (right > left && array[right] > temp)

right--;

array[left] = array[right];

while (right > left && array[left] < temp)

left++;

array[right] = array[left];

}

array[right] = temp;

return right;

}

public static int[] sort(int[] a, int left, int right) {

if (left < right) {

int i = middle(a, left, right);

sort(a, left, i);

sort(a, i + 1, right);

}

return a;

}

public static void main(String[] args) {

int[] a = {11, 81, 27, 53, 42, 45, 6, 77, 178, 9, 70};

sort(a, 0, a.length - 1);

for (int i : a) {

System.out.print(i+" ");

}

}

}

冒泡排序:public classMaopao {

public static int[] sort(int[] a){

for(inti=0;i

inttemp = a[i];

for(intj=i+1;j

if(a[j]

a[i]=a[j];

a[j]=temp;

temp=a[i];

}

}

}

returna;

}

public static voidmain(String[] args) {

int[] a={11,15,8,18,22,25,45};

sort(a);

for(inti:a){

System.out.println(i+" ");

}

}

}

斐波那契数列:public classFibonacci {

public static  intf(intn){

if(n<=0)return0;

if(n<=1)return1;

if(n<=2)return1;

returnf(n-1)+f(n-2);

}

public static voidmain(String[] args) {

System.out.println(f(3));

}

}

队列排序:

public class Queue {

private Object[] objects;

private int size;

private int head;

private int end;

public Queue(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)

//            return null;

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;

}

}

堆排序:

public class Stack {

private Object[] objects;

private int head;

private int size;

public Stack(int size) {

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("this stack is full");

objects[head++] = object;

size++;

}

public Object pop() throws Exception {

if (size == 0)

throw new Exception("this stack is empty");

size--;

return objects[--head];

}

}

你可能感兴趣的:(实训总结20171015)