Crazy

Сортирање со меурчиња кај листа

Nov 19th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.26 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. class DLLNode<E> {
  6.     protected E element;
  7.     protected DLLNode<E> predecessor, successor;
  8.    
  9.     public DLLNode(E element, DLLNode<E> predecessor, DLLNode<E> successor) {
  10.         this.element = element;
  11.         this.predecessor = predecessor;
  12.         this.successor = successor;
  13.     }
  14. }
  15.  
  16. class DLL<E> {
  17.     private DLLNode<E> first, last;
  18.    
  19.     public DLL() {
  20.         first = last = null;
  21.     }
  22.    
  23.     public void insertFirst(E o) {
  24.         DLLNode<E> insert = new DLLNode<E>(o, null, first);
  25.         if(first == null)
  26.             last = insert;
  27.         else first.predecessor = insert;
  28.         first = insert;        
  29.     }
  30.    
  31.     public void insertLast(E o) {
  32.         if(first == null)
  33.             insertFirst(o);
  34.         else {
  35.             DLLNode<E> insert = new DLLNode<E>(o, last, null);
  36.             last.successor = insert;
  37.             last = insert;
  38.         }
  39.     }
  40.    
  41.     public DLLNode<E> getFirst() {
  42.         return first;
  43.     }
  44. }
  45.  
  46. public class BubbleSortLL {
  47.     static void bubbleSort(DLL<Integer> lista) {
  48.         DLLNode<Integer> current = lista.getFirst();
  49.        
  50.         while(current != null) {
  51.             DLLNode<Integer> tmp = current;
  52.             while(tmp != null) {
  53.                 if(current.element > tmp.element) {
  54.                     int temp = current.element;
  55.                     current.element = tmp.element;
  56.                     tmp.element = temp;
  57.                 }
  58.                 tmp = tmp.successor;
  59.             }
  60.             current = current.successor;
  61.         }
  62.        
  63.         current = lista.getFirst();
  64.         while(current != null) {
  65.             System.out.print(current.element + " ");
  66.             current = current.successor;
  67.         }
  68.     }
  69.    
  70.     public static void main(String[] args) throws IOException {
  71.         DLL<Integer> lista = new DLL<Integer>();
  72.        
  73.         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  74.         String s = stdin.readLine();
  75.         int N = Integer.parseInt(s);
  76.         s = stdin.readLine();
  77.         String[] pomniza = s.split(" ");
  78.         for (int i = 0; i < N; i++) {
  79.             lista.insertLast(Integer.parseInt(pomniza[i]));
  80.         }
  81.        
  82.         bubbleSort(lista);
  83.     }
  84. }
Add Comment
Please, Sign In to add comment