Common list Sorting (Java edition)

Source: Internet
Author: User

The last blog explains the nine internal sorting algorithm, some of the algorithm also provides code implementation, but those code implementation is based on the array to sort, this blog on the list of links to implement several common sorting algorithm, readers.

Quick-Sort Linked list implementation

algorithm idea : For a linked list, the value of the head node as a key, and then traverse the node, you can get a list of less than key and a linked list of greater than or equal to key, so the recursive can be two linked list to be fast. Here's the idea of a quick sort, which is to put an element that is less than key on one side and put an element greater than or equal to the other side.

Code implementation:

1     //Quick Sort2      Public Static voidQuickSort (ListNode begin, ListNode end) {3         if(Begin = =NULL|| begin = =end)4             return;5         6ListNode index =paration (begin, end);7 QuickSort (begin, index);8 QuickSort (Index.next, end);9     }Ten      One     /** A * Partitioning function, dividing the head node value into a datum element . -      * @parambegin -      * @paramEnd the      * @return -      */ -      Public StaticListNode paration (ListNode begin, ListNode end) { -         if(Begin = =NULL|| begin = =end) +             returnbegin; -          +         intval = begin.val;//Datum elements AListNode index = begin, cur =Begin.next; at          -          while(cur! =end) { -             if(Cur.val < Val) {//Exchange -index =Index.next; -                 intTMP =Cur.val; -Cur.val =Index.val; inIndex.val =tmp; -             } toCur =Cur.next; +         } -          the          *Begin.val =Index.val; $Index.val =Val;Panax Notoginseng          -         returnindex; the}

Linked list implementation with merge sort

Algorithmic thinking: single-linked lists can only sequentially access each element compared to arrays, so the key to using a two-way merge sort is to find the middle node of the linked list in half: You can use the fast and slow pointer to traverse a single linked list at the same time. When a pointer with a step of 2 points to the last node of the list or the next node of the last node, a pointer with a step of 1 points to the middle node of the linked list. Then there is the merging of two ordered single-linked lists. The time complexity is O (N*LOGN) and the space complexity is O (1).

Code implementation:

1     //Merge Sort2      Public StaticListNode mergesort (ListNode head) {3         if(Head = =NULL|| Head.next = =NULL)//An empty list or only a single node.4             returnhead;5ListNode slow = head, fast =Head.next;6         7          while(Fast! =NULL&& Fast.next! =NULL){//use the fast and slow pointer to find the middle node8slow =Slow.next;9             TenFast =Fast.next; One             if(Fast.next! =NULL) AFast =Fast.next;  -         } -          theListNode PTR1 =Slow.next; -Slow.next =NULL; -          -ListNode TMP1 =MergeSort (head); +ListNode TMP2 =mergesort (PTR1); -         returnmerge (TMP1, TMP2); +     } A      at      -      Public Staticlistnode Merge (ListNode Start1, ListNode start2) { -ListNode Header =NewListNode (-1); -ListNode pre =header; -          -ListNode ptr1 = start1, ptr2 =Start2; in          while(Ptr1! =NULL&& PTR2! =NULL){ -             if(Ptr1.val <=ptr2.val) { toPre.next =ptr1; +Pre =ptr1; -PTR1 =Ptr1.next; the}Else{ *Pre.next =ptr2; $Pre =ptr2;Panax NotoginsengPTR2 =Ptr2.next; -             } the         } +          while(Ptr1! =NULL){ APre.next =ptr1; thePre =ptr1; +PTR1 =Ptr1.next; -         } $          $          while(PTR2! =NULL){ -Pre.next =ptr2; -Pre =ptr2; thePTR2 =Ptr2.next; -         }Wuyi          the          -         returnHeader.next; Wu          -}

The list implementation of bubble sort

algorithm idea: compare adjacent nodes sequentially, and exchange two nodes in reverse order

Code implementation:

1     //Bubble Sort2      Public StaticListNode Bubblesort (ListNode head) {3         if(Head = =NULL|| Head.next = =NULL)//The linked list is empty or has only a single node.4             returnhead;5         6ListNode cur =NULL, tail =NULL;7         8Cur =head;9         Ten          while(Cur.next! =tail) { One              while(Cur.next! =tail) { A                 if(Cur.val >cur.next.val) { -                     intTMP =Cur.val; -Cur.val =Cur.next.val; theCur.next.val =tmp; -                 } -Cur =Cur.next; -             } +              -tail = cur;//The end node of the next traversal is the current node (carefully pondering the lane inside) +cur = head;//traverse the starting node to reset to the head node. A         } at          -         returnhead; -          -          -}

Write these kinds of things first, think more ...

Common list Sorting (Java edition)

Related Article

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: info-contact@alibabacloud.com 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.