Bubble sort and cocktail sort (code)

Source: Internet
Author: User

Yesterday reviewed the bubble sort and cocktail sort, wrote it in an object-oriented way, and optimized the code to record ~

First, bubble sort
#Bubble SortclassBubblesort (object):def __init__(self, data_list): Self.data_list=data_list self.length=Len (data_list)#simple rough sort of way    defB_sort (self): data_list=list (self.data_list) forIinchRange (self.length):Print('************** section%s round cycle **************'% (i + 1))) Index_len=Self.length forIndexinchRange (index_len-1):                Print(Index)ifData_list[index] > Data_list[index + 1]: Data_list[index], Data_list[index+ 1] = Data_list[index + 1], Data_list[index]returndata_list#beautiful and lovely bubble sort after optimization    defNew_b_sort (self): data_list=list (self.data_list) forIinchRange (self.length):Print('************** section%s round cycle **************'% (i + 1))) Index_len=self.length Mark=False forIndexinchRange (Index_len-1-i):Print(index) Mark=TrueifData_list[index] > Data_list[index + 1]: Data_list[index], Data_list[index+ 1] = Data_list[index + 1], Data_list[index] Mark=FalseifMark: Break        returndata_listdata_list= [2, 3, 1, 7, 4, 3]bubblesort=Bubblesort (data_list) res1=Bubblesort.b_sort ()Print(res1) Res2=Bubblesort.new_b_sort ()Print(Res2)

Second, cocktail order
#Cocktail OrderingclassCocktailsort (object):def __init__(self, data_list): Self.data_list=data_list self.length=Len (data_list)#simple rough sort of way    defC_sort (self): data_list=list (self.data_list) forIinchRange (self.length):Print('************** section%s round cycle **************'% (i + 1))) Index_len=Self.lengthPrint('Left---> right')             forIndexinchRange (index_len-1):                Print(Index)ifData_list[index] < Data_list[index + 1]: Data_list[index], Data_list[index+ 1] = Data_list[index + 1], Data_list[index]Print('Right---> left') Index_len=Self.length forIndexinchRange (index_len-1):                Print(Index)ifData_list[index] > Data_list[index + 1]: Data_list[index], Data_list[index+ 1] = Data_list[index + 1], Data_list[index]returndata_list#optimized for playful, beautiful and sensible cocktail sort    defNew_c_sort (self): data_list=list (self.data_list) forIinchRange (self.length):Print('************** section%s round cycle **************'% (i + 1))) Index_len=self.length Mark=FalsePrint('Left---> right')             forIndexinchRange (index_len-1-i): Mark=TruePrint(Index)ifData_list[index] < Data_list[index + 1]: Data_list[index], Data_list[index+ 1] = Data_list[index + 1], Data_list[index] Mark=FalsePrint('Right---> left') Index_len=Self.length forIndexinchRange (I, index_len-1): Mark=TruePrint(Index)ifData_list[index] > Data_list[index + 1]: Data_list[index], Data_list[index+ 1] = Data_list[index + 1], Data_list[index] Mark=FalseifMark: Break        returndata_listdata_list= [2, 3, 1, 7, 4, 3]cocktailsort=Cocktailsort (data_list) res1=cocktailsort.c_sort () res2=Cocktailsort.new_c_sort ()Print(RES1)Print(Res2)

Bubble sort and cocktail sort (code)

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.