Algorithms-4th

Part1: Bags, Queues, and Stacks

1.1 ArrayBag

package com.lab1.test1;

import java.util.Iterator;

public class ArrayBag<Item> implements Iterable<Item> {
   
	private int n;
	@SuppressWarnings("unchecked")
	private Item[] a = (Item[]) new Object[2];

	@Override
	public Iterator<Item> iterator() {
   
		return new ListIterator();
	}

	private class ListIterator implements Iterator<Item> {
   
		int i = 0;

		@Override
		public boolean hasNext() {
   
			return i < n;
		}

		@Override
		public Item next() {
   
			Item item = a[i];
			i++;
			return item;
		}

	}

	@Override
	public String toString() {
   
		StringBuilder builder = new StringBuilder();
		for (Item item : this) {
   
			builder.append(item + " ");
		}
		return builder.toString();
	}

	private boolean isEmpty() {
   
		return n == 0;
	}

	private int size() {
   
		return n;
	}

	private void add(Item item) {
   
		if (n == a.length) {
   
			resize(a.length * 2);
		}
		a[n++] = item;
	}

	@SuppressWarnings("unchecked")
	private void resize(int capacity) {
   
		Item[] temp = (Item[]) new Object[capacity];
		for (int i = 0; i < n; i++) {
   
			temp[i] = a[i];
		}
		a = temp;
	}

	public static void main(String[] args) {
   
		ArrayBag<String> bag = new ArrayBag<>();
		System.out.println(bag);
		System.out.println(bag.size());
		System.out.println(bag.isEmpty());

		bag.add("bill");
		bag.add("jack");
		bag.add("lucy");
		System.out.println(bag);
		System.out.println(bag.size());
		System.out.println(bag.isEmpty());
	}

}

输出

0
true
bill jack lucy 
3
false

1.2 ArrayQueue

package com.lab1.test1;

import java.util.Iterator;

public class ArrayQueue<Item> implements Iterable<Item> {
   
	private int n;
	private int first;
	private int last;
	@SuppressWarnings("unchecked")
	private Item[] a = (Item[]) new Object[2];

	@Override
	public Iterator<Item> iterator() {
   
		return new ListIterator();
	}

	private class ListIterator implements Iterator<Item> {
   
		int i = 0;

		@Override
		public boolean hasNext() {
   
			return i < n;
		}

		@Override
		public Item next() {
   
			Item item = a[(first + i) % a.length];
			i++;
			return item;
		}

	}

	@Override
	public String toString() {
   
		StringBuilder builder = new StringBuilder();
		for (Item item : this) {
   
			builder.append(item + " ");
		}
		return builder.toString();
	}

	private boolean isEmpty() {
   
		return n == 0;
	}

	private int size() {
   
		return n;
	}

	private void enqueue(Item item) {
   
		if (n == a.length) {
   
			resize(a.length * 2);
		}
		a[last++] = item;
		n++;
		if (last == a.length) {
   
			last = 0;
		}
	}

	private Item dequeue() {
   
		Item item = a[first];
		a[first] = null;
		first++;
		if (first == a.length) {
   
			first = 0;
		}
		n--;
		if (n > 0 && n == a.length / 4) {
   
			resize(a.length / 2);
		}
		return item;
	}

	@SuppressWarnings("unchecked")
	private void resize(int capacity) {
   
		Item[] temp = (Item[]) new Object[capacity];
		for (int i = 0; i < n; i++) {
   
			temp[i] = a[(first + i) % a.length];
		}
		a = temp;
		first=0;
		last=n;
	}

	public static void main(String[] args) {
   
		ArrayQueue<String> queue = new ArrayQueue<>();
		System.out.println(queue);
		System.out.println(queue.size());
		System.out.println(queue.isEmpty());

		queue.enqueue("bill");
		queue.enqueue("jack");
		queue.enqueue("lucy");
		System.out.println(queue);
		System.out.println(queue.size());
		System.out.println(queue.isEmpty());

		queue.dequeue();
		queue.dequeue();
		System.out.println(queue);
		System.out.println(queue.size());
		System.out.println(queue.isEmpty());
	}

}

输出

0
true
bill jack lucy 
3
false
lucy 
1
false

1.3 ArrayStack

package com.lab1.test1;

import java.util.Iterator;

public class ArrayStack<Item> implements Iterable<Item> {
   
	private int n;
	@SuppressWarnings("unchecked")
	private Item[] a = (Item[]) new Object[2];

	@Override
	public Iterator<Item> iterator() {
   
		// TODO Auto-generated method stub
		return new ListIterator();
	}

	private class ListIterator implements Iterator<Item> {
   
		int i = 0;

		@Override
		public boolean hasNext() {
   
			return i < n;
		}

		@Override
		public Item next() {
   
			Item item = a[i];
			i++;
			return item;
		}

	}

	@Override
	public String toString() {
   
		StringBuilder builder = new StringBuilder();
		for (Item item : this) {
   
			builder.append(item + " ");
		}
		return builder.toString();
	}

	private boolean isEmpty() {
   
		return n == 0;
	}

	private int size() {
   
		return n;
	}

	private void push(Item item) {
   
		if (n == a.length) {
   
			resize(a.length * 2);
		}
		a[n++] = item;
	}

	private Item pop() {
   
		Item item = a[n - 1];
		a[n - 1] = null;
		n--;
		if (n > 0 && n == a.length / 4) {
   
			resize(a.length / 2);
		}
		return item;
	}

	@SuppressWarnings("unchecked")
	private void resize(int capacity) {
   
		Item[] temp = (Item[]) new Object[capacity];
		for (int i = 0; i < n; i++) {
   
			temp[i] = a[i];
		}
		a = temp;
	}

	public static void main(String[] args) {
   
		ArrayStack<String> stack = new ArrayStack<>();
		System.out.println(stack);
		System.out.println(stack.size());
		System.out.println(stack.isEmpty());

		stack.push("bill");
		stack.push("jack");
		stack.push("lucy");
		System.out.println(stack);
		System.out.println(stack.size());
		System.out.println(stack.isEmpty());

		stack.pop();
		stack.pop();
		System.out.println(stack);
		System.out.println(stack.size());
		System.out.println(stack.isEmpty());
	}

}

输出

0
true
bill jack lucy 
3
false
bill 
1
false

1.4 LinkedBag

package com.lab1.test1;

import java.util.Iterator;

public class LinkedBag<Item> implements Iterable<Item> {
   
	private int n;
	private Node first;

	private class Node {
   
		private Item item;
		private Node next;
	}

	@Override
	public Iterator<Item> iterator() {
   
		return new ListIterator();
	}

	private class ListIterator implements Iterator<Item> {
   
		Node current = first;

		@Override
		public boolean hasNext() {
   
			return current != null;
		}

		@Override
		public Item next() {
   
			Item item = current.item;
			current = current.next;
			return item;
		}

	}

	@Override
	public String toString() {
   
		StringBuilder builder = new StringBuilder();
		for (Item item : this) {
   
			builder.append(item + " ");
		}
		return builder.toString();
	}

	private boolean isEmpty() {
   
		return first == null;
	}

	private int size() {
   
		return n;
	}

	private void add(Item item) {
   
		Node oldfirst = first;
		first = new Node();
		first.item = item;
		first.next = oldfirst;
		n++;
	}

	public static void main(String[] args) {
   
		LinkedBag<String> bag = new LinkedBag<>();
		System.out.println(bag);
		System.out.println(bag.size());
		System.out.println(bag.isEmpty());
		
		bag.add("bill");
		bag.add("jack");
		bag.add("lucy");
		System.out.println(bag);
		System.out.println(bag.size());
		System.out.println(bag.isEmpty());
	}

}

输出

0
true
lucy jack bill 
3
false

1.5 LinkedQueue

package com.lab1.test1;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class LinkedQueue<Item> implements Iterable<Item> {
   
	private int n;
	private Node first, last;

	private class Node {
   
		private Item item;
		private Node next;
	}

	@Override
	public Iterator<Item> iterator() {
   
		return new ListIterator();
	}

	private class ListIterator implements Iterator<Item> {
   
		Node current = first;

		@Override
		public boolean hasNext() {
   
			return current != null;
		}

		@Override
		public Item next() {
   
			Item item = current.item;
			current = current.next;
			return item;
		}

	}

	@Override
	public String toString() {
   
		StringBuilder builder = new StringBuilder();
		for (Item item : this) {
   
			builder.append(item + " ");
		}
		return builder.toString();
	}

	private boolean isEmpty() {
   
		return first == null;
	}

	private int size() {
   
		return n;
	}

	private void push(Item item) {
   
		Node oldlast = last;
		last = new Node();
		last.item = item;
		if (isEmpty()) {
   
			first = last;
		} else {
   
			oldlast.next = last;
		}
		n++;
	}

	private Item pop() {
   
		if (isEmpty()) {
   
			throw new NoSuchElementException("empty stack exception");
		}
		Item item = first.item;
		first = first.next;
		if (isEmpty()) {
   
			last = null;
		}
		n--;
		return item;
	}

	public static void main(String[] args) {
   
		LinkedQueue<String> stack = new LinkedQueue<>();
		System.out.println(stack);
		System.out.println(stack.size());
		System.out.println(stack.isEmpty());

		stack.push("bill");
		stack.push("jack");
		stack.push("lucy");
		System.out.println(stack);
		System.out.println(stack.size());
		System.out.println(stack.isEmpty());

		stack.pop();
		stack.pop();
		System.out.println(stack);
		System.out.println(stack.size());
		System.out.println(stack.isEmpty());
	}

}

输出

0
true
bill jack lucy 
3
false
lucy 
1
false

1.6 LinkedStack

package com.lab1.test1;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class LinkedStack<Item> implements Iterable<Item> {
   
	private int n;
	private Node first;

	private class Node {
   
		private Item item;
		private Node next;
	}

	@Override
	public Iterator<Item> iterator() {
   
		return new ListIterator();
	}

	private class ListIterator implements Iterator<Item> {
   
		Node current = first;

		@Override
		public boolean hasNext() {
   
			return current != null;
		}

		@Override
		public Item next() {
   
			Item item = current.item;
			current = current.next;
			return item;
		}

	}

	@Override
	public String toString() {
   
		StringBuilder builder = new StringBuilder();
		for (Item item : this) {
   
			builder.append(item + " ");
		}
		return builder.toString();
	}

	private boolean isEmpty() {
   
		return first == null;
	}

	private int size() {
   
		return n;
	}

	private void push(Item item) {
   
		Node oldfirst = first;
		first = new Node();
		first.item = item;
		first.next = oldfirst;
		n++;
	}

	private Item pop() {
   
		if (isEmpty()) {
   
			throw new NoSuchElementException("empty stack exception");
		}
		Item item = first.item;
		first = first.next;
		n--;
		return item;
	}

	public static void main(String[] args) {
   
		LinkedStack<String> stack = new LinkedStack<>();
		System.out.println(stack);
		System.out.println(stack.size());
		System.out.println(stack.isEmpty());

		stack.push("bill");
		stack.push("jack");
		stack.push("lucy");
		System.out.println(stack);
		System.out.println(stack.size());
		System.out.println(stack.isEmpty());

		stack.pop();
		stack.pop();
		System.out.println(stack);
		System.out.println(stack.size());
		System.out.println(stack.isEmpty());
	}

}

输出

0
true
lucy jack bill 
3
false
bill 
1
false

Part2: Sorting

2.1 Bubble

package com.lab1.test2;

public class Bubble {
   
	public static void main(String[] args) {
   
		Comparable[] a = {
    5, 3, 1, 2, 4 };
		sort(a);
		show(a);
	}

	private static void show(Comparable[] a) {
   
		for (int i = 0; i < a.length; i++) {
   
			System.out.print(a[i] + " ");
		}
	}

	private static void sort(Comparable[] a) {
   
		for (int i = 0; i < a.length; i++) {
   
			for (int j = 1; j < a.length - i; j++) {
   
				if (less(a, j, j - 1)) {
   
					exch(a, j - 1, j);
				}
			}
		}
	}

	private static void exch(Comparable[] a, int i, int j) {
   
		Comparable temp = a[i];
		a[i] = a[j];
		a[j] = temp;
	}

	private static boolean less(Comparable[] a, int i, int j) {
   
		return a[i].compareTo(a[j]) < 0;
	}
}

输出

1 2 3 4 5 

2.2 Selection

package com.lab1.test2;

public class Selection {
   
	public static void main(String[] args) {
   
		Comparable[] a = {
    5, 3, 1, 2, 4 };
		sort(a);
		show(a);
	}

	private static void show(Comparable[] a) {
   
		for (int i = 0; i < a.length; i++) {
   
			System.out.print(a[i] + " ");
		}
	}

	private static void sort(Comparable[] a) {
   
		for (int i = 0; i < a.length; i++) {
   
			int min = i;
			for (int j = i; j < a.length; j++) {
   
				if (less(a, j, min)) {
   
					min = j;
				}
			}
			exch(a, i, min);
		}
	}

	private static void exch(Comparable[] a, int i, int j) {
   
		Comparable temp = a[i];
		a[i] = a[j];
		a[j] = temp;
	}

	private static boolean less(Comparable[] a, int i, int j) {
   
		return a[i].compareTo(a[j]) < 0;
	}
}

输出

1 2 3 4 5 

2.3 Insertion

package com.lab1.test2;

public class Insertion {
   
	public static void main(String[] args) {
   
		Comparable[] a = {
    5, 3, 1, 2, 4 };
		sort(a);
		show(a);
	}

	private static void show(Comparable[] a) {
   
		for (int i = 0; i < a.length; i++) {
   
			System.out.print(a[i] + " ");
		}
	}

	private static void sort(Comparable[] a) {
   
		for (int i = 0; i < a.length; i++) {
   
			for (int j = i; j > 0 && less(a, j, j - 1); j--) {
   
				exch(a, j - 1, j);
			
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
This fourth edition of Robert Sedgewick and Kevin Wayne’s Algorithms is the leading textbook on algorithms today and is widely used in colleges and universities worldwide. This book surveys the most important computer algorithms currently in use and provides a full treatment of data structures and algorithms for sorting, searching, graph processing, and string processing--including fifty algorithms every programmer should know. In this edition, new Java implementations are written in an accessible modular programming style, where all of the code is exposed to the reader and ready to use. The algorithms in this book represent a body of knowledge developed over the last 50 years that has become indispensable, not just for professional programmers and computer science students but for any student with interests in science, mathematics, and engineering, not to mention students who use computation in the liberal arts. The companion web site, algs4.cs.princeton.edu, contains An online synopsis Full Java implementations Test data Exercises and answers Dynamic visualizations Lecture slides Programming assignments with checklists Links to related material The MOOC related to this book is accessible via the "Online Course" link at algs4.cs.princeton.edu. The course offers more than 100 video lecture segments that are integrated with the text, extensive online assessments, and the large-scale discussion forums that have proven so valuable. Offered each fall and spring, this course regularly attracts tens of thousands of registrants. Robert Sedgewick and Kevin Wayne are developing a modern approach to disseminating knowledge that fully embraces technology, enabling people all around the world to discover new ways of learning and teaching. By integrating their textbook, online content, and MOOC, all at the state of the art, they have built a unique resource that greatly expands the breadth and depth of the educational experience.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值