Data Structure Experimental Report - Sorting Algorithm Design and Analysis (Sorting Singly Linked List)

1. Experimental purpose and requirements

By learning a variety of sorting algorithms, you can experience different algorithm designs for the same operation; by comparing the requirements of each sorting algorithm for data storage structure, you can realize that algorithm design does not depend on data storage structure, but algorithm implementation depends on data storage structure; by analyzing the efficiency of sorting algorithms, study how to further improve the performance of the algorithm.

It is required to master the idea of ​​each sorting algorithm, algorithm description, algorithm design and algorithm implementation means, master the analysis methods of sorting algorithm time complexity and space complexity, and have the ability to design sorting algorithms.

2. Experimental topics

Experiment 1: Sorting singly linked list by constructing singly linked list

public SortedSinglyList(SinglyList<T> list)

Experiment 2: Return the merged sorted list

Public SortedSinglyList<T> mergeWith(SortedSinglyList<T> list)

3. Experimental methods and steps (requirements analysis, algorithm design ideas, flowcharts, etc.)

(1) Sorting singly linked list by constructing singly linked list

Traverse the singly linked list, define a node to record the minimum value, if the node is smaller than the minimum value, replace it, otherwise move to the next node, and finally return to the linked list.

(2) Return the merged sorted list

Create a new, empty sorted singly-linked list, and start comparing the size of the elements in the two singly-linked lists. If the element of this is smaller than the first one in the list, this will be traversed until the element of this is greater than the list, and then the list will be traversed. The traversal of the list is the same as the previous steps.

  • Experimental original record (source program, data structure, etc.)

public class SortedSinglyList<T extends Comparable<? super T>> extends SinglyList<T>

{    

    public SortedSinglyList()                             

    {

        super();                                          

    }

    

    public SortedSinglyList(T[] values)                    

    {

        super();                                          

        for (int i=0;  i<values.length;  i++)             

            this.insert(values[i]);                       

    }

    public SortedSinglyList(SortedSinglyList<T> list)      

    {

        super(list);                                      

    }   

    public void set(int i, T x)

    {

        throw new UnsupportedOperationException("set(int i, T x)");

    }

    public Node<T> insert(int i, T x)

    {

        throw new UnsupportedOperationException("insert(int i, T x)");

    }

    

    public Node<T> insert(T x)

    {

        Node<T> front=this.head, p=front.next;             

        while (p!=null && x.compareTo(p.data)>0)           

        {

            front = p;

            p = p.next;

        }

        front.next = new Node<T>(x, p);               

        return front.next;                

    }

    

    public Node<T> search(T key)

    {

        for (Node<T> p=this.head.next;  p!=null && key.compareTo(p.data)>=0;  p=p.next)

        {

            if (key.compareTo(p.data)==0)              

                return p;

        }

        return null;

    }

    

    public Node<T> insertDifferent(T x)

    {

        Node<T> front=this.head, p=front.next;            

        while (p!=null && x.compareTo(p.data)>0)           

        {

            front = p;

            p = p.next;

        }

        if ( p ! = null & & x . compareTo ( p . data ) == 0 )

            return p;                                      

        return front.next = new Node<T>(x, p);           

    }

    public T remove(T key)

    {

        Node<T> front=this.head, p=front.next;             

        while (p!=null && key.compareTo(p.data)>0)      

        {

            front = p;

            p = p.next;

        }

        if ( p ! = null & & key . compareTo ( p . data ) == 0 )          

        {

            front.next = p.next;                          

            return p.data;

        }

        return null;

    }

   

    public void concat(SinglyList<T> list)                

    {

        throw new UnsupportedOperationException("concat(SinglyList<T> list)");

    }

    public void addAll(SinglyList<T> list)

    {

        for (Node<T> p=list.head.next;  p!=null;  p=p.next)

            this.insert(p.data);                  

    }    

    

    public SortedSinglyList<T> union(SinglyList<T> list)

    {

        SortedSinglyList<T> result = new SortedSinglyList<T>(this);   

        result.addAll(list);

        return result;

    }

    public SortedSinglyList(SinglyList<T> list)

    {

        super(list);                                       

        for (Node<T> first=this.head.next;  first.next!=null;  first=first.next)

        {                                                

            Node<T> min=first;                             

            for (Node<T> p=min.next;  p!=null;  p=p.next)  

                if (p.data.compareTo(min.data)<0)          

                    min = p;                               

            if (min!=first)                                

            {

                T temp = min.data;

                min.data = first.data;

                first.data = temp;

            }

        }

    }

    public void merge(SortedSinglyList<T> list)            

    {

        Node<T> front=this.head, p=front.next;            

        Node<T> q=list.head.next;                         

        while ( p ! = null & & q ! = null )                         

            if ((p.data).compareTo(q.data)<0)             

            {

                front = p;

                p = p.next;

            }

            else

            {                                              

                front.next = q;

                q = q.next;

                front = front.next;

                front.next = p;

            }

        if (q!=null)                                       

            front.next=q;

        list.head.next=null;                            

    }

    

    public SortedSinglyList<T> mergeWith(SortedSinglyList<T> list)  

    {

        Node<T> p=this.head.next, q=list.head.next;      

        SortedSinglyList<T> result = new SortedSinglyList<T>();

        Node<T> rear = result.head;                     

        while (p!=null || q!=null)                       

            if (p!=null && (q!=null && (p.data).compareTo(q.data)<=0 || q==null))

            {                                             

                rear.next = new Node<T>(p.data,null);

                rear = rear.next;

                p = p.next;

            }

            else if (q!=null && (p!=null && (p.data).compareTo(q.data)>0 || p==null))

            {                                              

                rear.next = new Node<T>(q.data,null);

                rear = rear.next;

                q = q.next;

            }

        return result;

    }

    

5. Experimental results and analysis (calculation process and results, data curves, charts, etc.)

public static void main(String args[])

    {

     Integer []values={1,2,3,5,7,9};

     Integer []values1={6,8,10,11};

     Integer []values2={1,3,4,2,8,5,9,6,7};

     SinglyList<Integer>list3=new SinglyList<Integer>(values2);

     SortedSinglyList<Integer>list=new SortedSinglyList<Integer>(values);

     SortedSinglyList<Integer>list1=new SortedSinglyList<Integer>(values1);

     SortedSinglyList<Integer>list2=new SortedSinglyList<Integer>(list3);

     System.out.println(list+" and "+list1+" after connection: "+"\n"+list.mergeWith(list1));

     System.out.println(list3+" is converted into a sorted singly linked list to "+"\n"+list2); }

}

Experimental results:

  1. Construct a sorted singly linked list from a singly linked list:

 

  1. Return the merged sorted singly linked list:

Guess you like

Origin blog.csdn.net/weixin_45823684/article/details/122469628