Algorithm learning--single-link list fast row

Source: Internet
Author: User

/** *  Start-end The nodes between the axes with p as an axis (including start &&  not including end); *  ideas:  * 1. Start the head node as an axis node, and from, if the node is less than the value of the axis start, insert the node behind the axis node;  * 2. Insert the Axis node in the appropriate position, that is, the last node less than the axis, and the node is swapped with the axis node value , the linked list is divided into two parts, less than the axis node and the greater than the axis node;  * 3. Recursively iterates through 2 of the two-part nodes.  *  *  @param  p *  @param  start *  @param  end */ Private static void quicksortwithlinkedlist (node start, node end)  {if  (start == null | |  start == end)  {return;} node last = start; node cur =; Node next ;// 1. The default start as the axis, if index is smaller than start, moves to the nextwhile  of start (cur != null  && cur != end)  {next =;if  (cur.value <=  Start.value)  {if  (start != last)  {//to prevent duplicate references that occur when the first element is less than an axis = next; Node startnext =;start. Next = cur; = startnext;cur = next;} Else{last = cur;cur =;}}  else {last = cur;cur =;}}  2. Move the axis to the appropriate position and swap the values of the nodes smaller than the axes node newindex =;last = start;//  Find the position of the axis insert last, that is, to find a node smaller than the axis; Newindex != end is to prevent end from being added to the computed range while  (newindex != null  && newIndex != end && newIndex.value <=  Start.value)  {last = newindex;newindex =;}   Swap the axis with the value of the node at the insertion position Int temp = last.value;last.value = start.value;start.value  = temp;// 3. Proceed to the next iteration quicksortwithlinkedlist (Start, last); Quicksortwithlinkedlist ( ,  end);}

This article is from the "Wauoen" blog, make sure to keep this source

Algorithm learning--single-link list fast row

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.