duplicityyy

[АПС] - Сортирање со меурчиња кај листа

Jun 25th, 2020 (edited)
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.68 KB | None | 0 0
  1. Сортирање со меурчиња кај листа Problem 3 (1 / 1)
  2. Дадена е една двојно поврзана листa и со N јазли кои во себе содржат по еден природен број. Треба да се сортира листата со помош на сортирањето со меурчиња (bubble sort). Во првиот ред од влезот е даден бројот на јазли во листата, а потоа во вториот ред се дадени јазлите од кои е составена. На излез треба да се испечатат јазлите на сортираната листа.
  3.  
  4. Име на класата: BubbleSortDLL
  5. Забелешка: При реализација на задачата МОРА да се користи дадената структура, а не да користат помошни структури како низи и сл.
  6.  
  7. Sample input
  8. 8
  9. 6 10 13 5 8 17 2 5
  10.  
  11. Sample output
  12. 2 5 5 6 8 10 13 17
  13.  
  14. --------------------------------------------------------------
  15.  
  16. import java.io.BufferedReader;
  17. import java.io.IOException;
  18. import java.io.InputStreamReader;
  19.  
  20.  
  21. class DLLNode<E> {
  22.     protected E element;
  23.     protected DLLNode<E> pred, succ;
  24.  
  25.     public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
  26.         this.element = elem;
  27.         this.pred = pred;
  28.         this.succ = succ;
  29.     }
  30.  
  31.     @Override
  32.     public String toString() {
  33.         return element.toString();
  34.     }
  35. }
  36.  
  37. class DLL<E> {
  38.     private DLLNode<E> first, last;
  39.  
  40.     public DLL() {
  41.         // Construct an empty SLL
  42.         this.first = null;
  43.         this.last = null;
  44.     }
  45.  
  46.     public void deleteList() {
  47.         first = null;
  48.         last = null;
  49.     }
  50.  
  51.     public int length() {
  52.         int ret;
  53.         if (first != null) {
  54.             DLLNode<E> tmp = first;
  55.             ret = 1;
  56.             while (tmp.succ != null) {
  57.                 tmp = tmp.succ;
  58.                 ret++;
  59.             }
  60.             return ret;
  61.         } else
  62.             return 0;
  63.  
  64.     }
  65.  
  66.     public DLLNode<E> find(E o) {
  67.         if (first != null) {
  68.             DLLNode<E> tmp = first;
  69.             while (tmp.element != o && tmp.succ != null)
  70.                 tmp = tmp.succ;
  71.             if (tmp.element == o) {
  72.                 return tmp;
  73.             } else {
  74.                 System.out.println("Elementot ne postoi vo listata");
  75.             }
  76.         } else {
  77.             System.out.println("Listata e prazna");
  78.         }
  79.         return first;
  80.     }
  81.  
  82.     public void insertFirst(E o) {
  83.         DLLNode<E> ins = new DLLNode<E>(o, null, first);
  84.         if (first == null)
  85.             last = ins;
  86.         else
  87.             first.pred = ins;
  88.         first = ins;
  89.     }
  90.  
  91.     public void insertLast(E o) {
  92.         if (first == null)
  93.             insertFirst(o);
  94.         else {
  95.             DLLNode<E> ins = new DLLNode<E>(o, last, null);
  96.             last.succ = ins;
  97.             last = ins;
  98.         }
  99.     }
  100.  
  101.     public void insertAfter(E o, DLLNode<E> after) {
  102.         if (after == last) {
  103.             insertLast(o);
  104.             return;
  105.         }
  106.         DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
  107.         after.succ.pred = ins;
  108.         after.succ = ins;
  109.     }
  110.  
  111.     public void insertBefore(E o, DLLNode<E> before) {
  112.         if (before == first) {
  113.             insertFirst(o);
  114.             return;
  115.         }
  116.         DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
  117.         before.pred.succ = ins;
  118.         before.pred = ins;
  119.     }
  120.  
  121.     public E deleteFirst() {
  122.         if (first != null) {
  123.             DLLNode<E> tmp = first;
  124.             first = first.succ;
  125.             if (first != null)
  126.                 first.pred = null;
  127.             if (first == null)
  128.                 last = null;
  129.             return tmp.element;
  130.         } else
  131.             return null;
  132.     }
  133.  
  134.     public E deleteLast() {
  135.         if (first != null) {
  136.             if (first.succ == null)
  137.                 return deleteFirst();
  138.             else {
  139.                 DLLNode<E> tmp = last;
  140.                 last = last.pred;
  141.                 last.succ = null;
  142.                 return tmp.element;
  143.             }
  144.         }
  145.         // else throw Exception
  146.         return null;
  147.     }
  148.  
  149.     public E delete(DLLNode<E> node) {
  150.         if (node == first) {
  151.             deleteFirst();
  152.             return node.element;
  153.         }
  154.         if (node == last) {
  155.             deleteLast();
  156.             return node.element;
  157.         }
  158.         node.pred.succ = node.succ;
  159.         node.succ.pred = node.pred;
  160.         return node.element;
  161.  
  162.     }
  163.  
  164.     @Override
  165.     public String toString() {
  166.         String ret = new String();
  167.         if (first != null) {
  168.             DLLNode<E> tmp = first;
  169.             ret += tmp + " ";
  170.             while (tmp.succ != null) {
  171.                 tmp = tmp.succ;
  172.                 ret += tmp + " ";
  173.             }
  174.         } else
  175.             ret = "Prazna lista!!!";
  176.         return ret;
  177.     }
  178.  
  179.     public String toStringR() {
  180.         String ret = new String();
  181.         if (last != null) {
  182.             DLLNode<E> tmp = last;
  183.             ret += tmp + "<->";
  184.             while (tmp.pred != null) {
  185.                 tmp = tmp.pred;
  186.                 ret += tmp + "<->";
  187.             }
  188.         } else
  189.             ret = "Prazna lista!!!";
  190.         return ret;
  191.     }
  192.  
  193.     public DLLNode<E> getFirst() {
  194.         return first;
  195.     }
  196.  
  197.     public DLLNode<E> getLast() {
  198.  
  199.         return last;
  200.     }
  201. }
  202.  
  203. public class BubbleSortLL{
  204.  
  205.     public static void bubbleSort(DLL<Integer> lista) {
  206.         DLLNode<Integer> node = lista.getFirst();
  207.         int temp = 0;
  208.  
  209.         boolean sorted = false;
  210.         while (!sorted) {
  211.             sorted = true;
  212.             while (node.succ != null) {
  213.                 if (node.element > node.succ.element) {
  214.                     temp = node.element;
  215.                     node.element = node.succ.element;
  216.                     node.succ.element = temp;
  217.                     sorted = false;
  218.                 }
  219.                 node = node.succ;
  220.             }
  221.             node = lista.getFirst();
  222.         }
  223.         System.out.println(lista);
  224.     }
  225.  
  226.     public static void main(String[] args) throws IOException {
  227.         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  228.         String s = stdin.readLine();
  229.         int N = Integer.parseInt(s);
  230.         s = stdin.readLine();
  231.         String[] pomniza = s.split(" ");
  232.         DLL<Integer> lista = new DLL<Integer>();
  233.         for (int i = 0; i < N; i++) {
  234.             lista.insertLast(Integer.parseInt(pomniza[i]));
  235.         }
  236.  
  237.         bubbleSort(lista);
  238.     }
  239. }
Add Comment
Please, Sign In to add comment