Python-based Sorting Algorithm and python-based Sorting Algorithm
This example describes the selection and sorting algorithm implemented by Python. We will share this with you for your reference. The details are as follows:
Selection sort is a simple and intuitive sorting algorithm. It works by selecting the smallest (or largest) element from the data elements to be sorted each time, and storing it at the starting position of the sequence until all the data elements to be sorted are arranged.
Select the Sorting index value that records the maximum number of records each time. Similar to the Bubble sorting, it also needs to be compared n-1 times, the difference is that the Bubble Sorting is exchanged each time, and the selection sorting is only exchanged after the final comparison.
Sample Code:
#! /Usr/bin/env python # coding: utf-8def selectSort (nums): for I in range (len (nums): max_index = 0 for j in range (len (nums) -I): if nums [max_index] <nums [j]: max_index = j nums [max_index], nums [len (nums)-i-1] = nums [len (nums) -i-1], nums [max_index] return numsnums = [6, 2, 54435,3141] print "helper house Test Result:" print selectSort (nums)
Running result:
PS: For more information about sorting algorithms, see online tools on this site:
Online animation demo insert/select/bubble/merge/hill/Quick Sort Algorithm process Tool
Http://tools.jb51.net/aideddesign/paixu_ys