/** * 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 Start.next, 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 = start.next; 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 = cur.next;if (cur.value <= Start.value) {if (start != last) {//to prevent duplicate references that occur when the first element is less than an axis last.next = next; Node startnext = start.next;start. Next = cur;cur.next = startnext;cur = next;} Else{last = cur;cur = cur.next;}} else {last = cur;cur = cur.next;}} 2. Move the axis to the appropriate position and swap the values of the nodes smaller than the axes node newindex = start.next;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 = newindex.next;} 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 (Last.next , end);}
This article is from the "Wauoen" blog, make sure to keep this source http://7183397.blog.51cto.com/7173397/1967055
Algorithm learning--single-link list fast row