Advertisement
isabelgh

Bubblesort

Sep 21st, 2015
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.11 KB | None | 0 0
  1.  
  2. public class Node {
  3.    
  4.     //content of each node
  5.     public int data;   
  6.     //pointer to the next Node in the list
  7.     public Node next;
  8.    
  9.     public Node (int data){
  10.         this.data = data;
  11.     }
  12.    
  13.     //prints out the data in the Node
  14.     public void display(){
  15.        
  16.         System.out.print(data);
  17.     }
  18.    
  19.     public String toString(){
  20.         return data + "";
  21.     }
  22.  
  23. }
  24.  
  25.  
  26.  
  27.  
  28. public class LinkedList {
  29.    
  30.     //a pointer to the latest value in the list
  31.     public Node firstNode;
  32.    
  33.     //the number of nodes in the list
  34.     public int numberOfNodes;
  35.    
  36.     //starts at a null value
  37.     LinkedList(){
  38.        
  39.         firstNode = null;
  40.     }
  41.    
  42.     //check if the list is empty
  43.     public boolean isEmpty(){
  44.        
  45.         return(firstNode == null);
  46.     }
  47.    
  48.     //add a new node
  49.     public void add(int data){     
  50.        
  51.         Node n = new Node(data);        //new node
  52.        
  53.         n.next = firstNode;                 //n.next gets the address to the previous node
  54.        
  55.         firstNode = n;                      //firstNode point at the new node n
  56.     }
  57.    
  58.     //cycle through the nodes
  59.     public void display(){
  60.        
  61.         Node n = firstNode;
  62.        
  63.         while(n != null){
  64.            
  65.             n.display();
  66.             //System.out.println("Next node: " + n.next);
  67.             n = n.next;
  68.            
  69.             numberOfNodes++;
  70.         }
  71.     }
  72.    
  73.     public void bubblesort(LinkedList list, int number){
  74.        
  75.         boolean swapped = true;
  76.        
  77.         while(number > 0 && swapped){
  78.            
  79.             swapped = false;
  80.             Node n1 = list.firstNode;
  81.             Node n2 = n1.next;
  82.            
  83.             for (int i = 0; i < number - 1; i++) {
  84.                
  85.                 if(n1.data > n2.data){
  86.                                        
  87.                     swapped = true;
  88.                     swap(list, n1, n2);
  89.                 }
  90.                 n1 = n1.next;
  91.                 n2 = n1.next;
  92.             }
  93.         }
  94.        
  95.     }
  96.     static void swap(LinkedList List, Node n1, Node n2)
  97.     {
  98.         int n = n1.data;
  99.         n1.data = n2.data;
  100.         n2.data = n;
  101.          
  102.     }
  103.  
  104. }
  105.  
  106.  
  107.  
  108.  
  109. public class Driver {
  110.  
  111.     public static void main(String[] args) {
  112.         // TODO Auto-generated method stub
  113.        
  114.         LinkedList list = new LinkedList();
  115.         list.add(2);
  116.         list.add(8);
  117.         list.add(3);
  118.         list.add(9);
  119.         list.add(5);
  120.         list.add(6);
  121.         list.display();
  122.         System.out.println();
  123.         list.bubblesort(list, list.numberOfNodes);
  124.         list.display();
  125.         //System.out.println(list.numberOfNodes);
  126.  
  127.     }
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement