Python-based Sorting Algorithm and python-based Sorting Algorithm

Source: Internet
Author: User

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

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.