Sort an array in Java

后端 未结 17 1151
傲寒
傲寒 2020-11-22 04:53

I\'m trying to make a program that consists of an array of 10 integers which all has a random value, so far so good.

However, now I need to sort them in order from l

17条回答
  •  萌比男神i
    2020-11-22 05:21

    We can also use binary search tree for getting sorted array by using in-order traversal method. The code also has implementation of basic binary search tree below.

    class Util {
        public static void printInorder(Node node) 
        { 
            if (node == null) {
                return;
            } 
    
            /* traverse left child */
            printInorder(node.left); 
    
            System.out.print(node.data + " "); 
    
            /* traverse right child */
            printInorder(node.right); 
         } 
    
        public static void sort(ArrayList al, Node node) {
            if (node == null) {
                return;
            } 
    
            /* sort left child */
            sort(al, node.left); 
    
            al.add(node.data);
    
            /* sort right child */
            sort(al, node.right); 
    
        }
    }
    
    class Node {
        Node left;
        Integer data;
        Node right;
    
        public Node(Integer data) {
            this.data = data;
        }
    
        public void insert(Integer element) {
            if(element.equals(data)) {
                return;
            }
    
            // if element is less than current then we know we will insert element to left-sub-tree
            if(element < data) {
                // if this node does not have a sub tree then this is the place we insert the element.
                if(this.left == null) {
                    this.left = new Node(element);  
                } else { // if it has left subtree then we should iterate again.
                    this.left.insert(element);
                }
            } else {
                if(this.right == null) {
                    this.right = new Node(element);
                } else {
                    this.right.insert(element);
                }
            }
        }
    }
    
    class Tree {
        Node root;
    
        public void insert(Integer element) {
            if(root == null) {
                root = new Node(element);
            } else {
                root.insert(element);
            }       
        }
    
        public void print() {
            Util.printInorder(root);
        }
    
        public ArrayList sort() {
            ArrayList al = new ArrayList();
            Util.sort(al, root);
            return al;
        }
    }
    
    public class Test {
    
        public static void main(String[] args) {
    
            int [] array = new int[10];
    
            array[0] = ((int)(Math.random()*100+1));
            array[1] = ((int)(Math.random()*100+1));
            array[2] = ((int)(Math.random()*100+1));
            array[3] = ((int)(Math.random()*100+1));
            array[4] = ((int)(Math.random()*100+1));
            array[5] = ((int)(Math.random()*100+1));
            array[6] = ((int)(Math.random()*100+1));
            array[7] = ((int)(Math.random()*100+1));
            array[8] = ((int)(Math.random()*100+1));
            array[9] = ((int)(Math.random()*100+1));
    
            Tree tree = new Tree();
    
            for (int i = 0; i < array.length; i++) {
                tree.insert(array[i]);
            }
    
            tree.print();
    
            ArrayList al = tree.sort();    
    
            System.out.println("sorted array : ");
            al.forEach(item -> System.out.print(item + " "));
    }
    

    }

提交回复
热议问题