Classic algorithm Daily Walkthrough--23rd Cocktail Order

Source: Internet
Author: User

The original: A daily walkthrough of the classic Algorithmic question--the 23rd cocktail order

This article we continue to talk about the cocktail sort, in order to know why the name of the cocktail, deliberately read the encyclopedia, see the box, can only reluctantly say so.

If the literary point, can be said to be stirring sort, easy to understand the point, it is called "two-way bubble sort", I would like to be a code farming words, can't not know bubble sort,

Bubbling is a one-way, small-to-large or large-to-small exchange sort, while cocktail ordering is bidirectional, from one end to the next from small to large, from the other end to the big

To a small sort.

As you can see, for the first forward comparison, we found the maximum value of 9.

For the first reverse comparison, we found the minimum value of 1.

For the second forward comparison, we found the second largest value of 8.

The second reverse comparison, we found a sub-value of 2

。。。

Finally, it's done.

Let's look at the code below:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSYSTEM.XML.XSL;6 7 namespaceConsoleApplication18 {9     class ProgramTen     { One         Static voidMain (string[] args) A         { -list<int> list =Newlist<int> () {8,1,4,2,9,5,3 }; -  theConsole.WriteLine ("\ n Sort before = = {0}\n",string. Join (",", list)); -  -List =Cocktailsort (list); -  +Console.WriteLine ("\ n Sort after = {0}\n",string. Join (",", list)); -  + Console.read (); A         } at  -         /// <summary> -         ///Cocktail Ordering -         /// </summary> -         /// <param name= "list" ></param> -         /// <returns></returns> in         Staticlist<int> Cocktailsort (list<int>list) -         { to             //because it is a bidirectional comparison, the number of comparisons is 1/2 times that of the original array.  +              for(inti =1; I <= list. Count/2; i++) -             { the                 //sort from front to back (ascending) *                  for(intm = i-1; M <= list. Count-i; m++) $                 {Panax Notoginseng                     //if the front is greater than the back, the interchange -                     if(M +1< list. Count && List[m] > list[m +1]) the                     { +                         vartemp =List[m]; A  theList[m] = list[m +1]; +  -List[m +1] =temp; $                     } $                 } -  -Console.WriteLine ("forward Sort = = {0}",string. Join (",", list)); the  -                 //sort from back to front (descending)Wuyi                  for(intn = list. Count-i-1; n >= i; n--) the                 { -                     //if the front is greater than the back, the interchange Wu                     if(N >0&& List[n-1] >List[n]) -                     { About                         vartemp =List[n]; $  -List[n] = list[n-1]; -  -List[n-1] =temp; A                     } +                 } the  -Console.WriteLine ("reverse Sort = = {0}",string. Join (",", list)); $             } the  the             returnlist; the         } the     } -}

From the results, we will find that when the array is in order, we will continue to row, knowing that the completion of the LENGTH/2 times, this is not optimized before the bubble sort,

At this point we can add a flag bit issorted to determine whether there is no exchange, if not, early exit loop ...

1         /// <summary>2         ///Cocktail Ordering3         /// </summary>4         /// <param name= "list" ></param>5         /// <returns></returns>6         Staticlist<int> Cocktailsort (list<int>list)7         {8             //Judging if it's sorted.9           var issorted = false;Ten  One             //because it is a bidirectional comparison, the number of comparisons is 1/2 times that of the original array.  A              for(inti =1; I <= list. Count/2; i++) -             { -                 //sort from front to back (ascending) the                  for(intm = i-1; M <= list. Count-i; m++) -                 { -                     //if the front is greater than the back, the interchange -                     if(M +1< list. Count && List[m] > list[m +1]) +                     { -                         vartemp =List[m]; +  AList[m] = list[m +1]; at  -List[m +1] =temp; -  -                       issorted = true; -                     } -                 } in  -Console.WriteLine ("forward Sort = = {0}",string. Join (",", list)); to  +                 //sort from back to front (descending) -                  for(intn = list. Count-i-1; n >= i; n--) the                 { *                     //if the front is greater than the back, the interchange $                     if(N >0&& List[n-1] >List[n])Panax Notoginseng                     { -                         vartemp =List[n]; the  +List[n] = list[n-1]; A  theList[n-1] =temp; +  -                        issorted = true; $                     } $                 } -  -                 //when there is no longer a sort, quit early the                 if (!issorted) -                      Break;Wuyi  theConsole.WriteLine ("reverse Sort = = {0}",string. Join (",", list)); -             } Wu  -             returnlist; About}

Classic algorithm Daily Walkthrough--23rd Cocktail Order

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.