Python implementation of the classical sorting algorithm

Source: Internet
Author: User

First, choose the sort Select_sort

#Finds the smallest value in an arraydeffindsmallest (arr):#Stores the smallest valuesmallest =Arr[0]#Stores The index of the smallest valueSmallest_index =0 forIinchRange (1, Len (arr)):ifArr[i] <smallest:smallest=Arr[i] Smallest_index=IreturnSmallest_index#Sort ArraydefSelectionsort (arr): NewArr= []   forIinchRange (len (arr)):#Finds the smallest element in the array and adds it to the new arraysmallest =findsmallest (arr) newarr.append (Arr.pop (smallest))returnNEWARRPrintSelectionsort ([5, 3, 6, 2, 10])

First, quick sort Quick_sort

defquicksort (array):ifLen (array) < 2:    #base case, arrays with 0 or 1 element is already "sorted"    returnArrayElse:    #recursive casePivot =Array[0]#Sub-array of the elements less than the pivotless = [i forIinchArray[1:]ifI <=Pivot]#Sub-array of all the elements greater than the pivotGreater = [I forIinchArray[1:]ifi >Pivot]returnQuicksort (less) + [pivot] +quicksort (Greater)PrintQuicksort ([10, 5, 2, 3])

Python implementation of the classical sorting algorithm

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.