Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- class DLLNode<E> {
- protected E element;
- protected DLLNode<E> predecessor, successor;
- public DLLNode(E element, DLLNode<E> predecessor, DLLNode<E> successor) {
- this.element = element;
- this.predecessor = predecessor;
- this.successor = successor;
- }
- }
- class DLL<E> {
- private DLLNode<E> first, last;
- public DLL() {
- first = last = null;
- }
- public void insertFirst(E o) {
- DLLNode<E> insert = new DLLNode<E>(o, null, first);
- if(first == null)
- last = insert;
- else first.predecessor = insert;
- first = insert;
- }
- public void insertLast(E o) {
- if(first == null)
- insertFirst(o);
- else {
- DLLNode<E> insert = new DLLNode<E>(o, last, null);
- last.successor = insert;
- last = insert;
- }
- }
- public DLLNode<E> getFirst() {
- return first;
- }
- }
- public class BubbleSortLL {
- static void bubbleSort(DLL<Integer> lista) {
- DLLNode<Integer> current = lista.getFirst();
- while(current != null) {
- DLLNode<Integer> tmp = current;
- while(tmp != null) {
- if(current.element > tmp.element) {
- int temp = current.element;
- current.element = tmp.element;
- tmp.element = temp;
- }
- tmp = tmp.successor;
- }
- current = current.successor;
- }
- current = lista.getFirst();
- while(current != null) {
- System.out.print(current.element + " ");
- current = current.successor;
- }
- }
- public static void main(String[] args) throws IOException {
- DLL<Integer> lista = new DLL<Integer>();
- BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
- String s = stdin.readLine();
- int N = Integer.parseInt(s);
- s = stdin.readLine();
- String[] pomniza = s.split(" ");
- for (int i = 0; i < N; i++) {
- lista.insertLast(Integer.parseInt(pomniza[i]));
- }
- bubbleSort(lista);
- }
- }
Add Comment
Please, Sign In to add comment