Home Backend Development Python Tutorial python implements insertion sort algorithm

python implements insertion sort algorithm

Dec 29, 2016 pm 03:58 PM
insertion sort

#!/usr/bin/python 

def insert_sort(array): 
for i in range(1, len(array)): 
key = array[i] 
j = i - 1 
while j >= 0 and key < array[j]: 
array[j + 1] = array[j] 
j-=1 

array[j + 1] = key 

if __name__ == "__main__": 
array = [2, 4, 32, 64, 34, 78, 23, 2345, 2345, 12, 1, 3] 

insert_sort(array) 
for a in array: 
print a
Copy after login


For more articles related to python implementation of insertion sort algorithm, please pay attention to the PHP Chinese website!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

C program for recursive insertion sort C program for recursive insertion sort Sep 20, 2023 pm 02:37 PM

Insertion sort is a sorting algorithm that is based on in-place comparison. This algorithm works by placing an element at a position in a sorted subarray, i.e. the subarray before the element is the sorted subarray. Algorithm Step1-Loop from 1 to n-1 and execute-Step2.1-Select the element at position i, array[i]. Step2.2-Insert the element into the sorted sub-array array[0] at its position arr[i]. Let's use an example to understand the algorithm array = [34,7,12,90,51] for i=1, arr[1]=7, put it into the position in the subarray arr[0]-arr[1]. [7,34,12,90,51] For i=2, arr

How to use insertion sort algorithm in C++ How to use insertion sort algorithm in C++ Sep 19, 2023 am 10:03 AM

Array sorting using the insertion sort algorithm in C++ Insertion sort is a simple but effective sorting algorithm. It inserts the elements to be sorted into the sorted list one by one, and finally gets an ordered list. This article will introduce how to use the C++ programming language to implement the insertion sort algorithm and give specific code examples. Algorithm idea: The basic idea of ​​insertion sort is to divide the array into sorted intervals and unsorted intervals. Each time an element is selected from the unsorted range and inserted into the appropriate position of the sorted range until the unsorted range is empty

Detailed explanation of the insertion sort algorithm implemented in Java Detailed explanation of the insertion sort algorithm implemented in Java Feb 19, 2024 pm 12:56 PM

Detailed explanation of the implementation method of Java insertion sort algorithm Insertion sort is a simple and intuitive sorting algorithm. Its principle is to divide the sequence to be sorted into sorted and unsorted parts. Each time an element is taken out from the unsorted part and inserted into Suitable location sorted. The implementation method of the insertion sort algorithm is relatively simple. The specific implementation method will be introduced in detail below and corresponding code examples will be given. The algorithm idea assumes that an integer array arr is to be sorted in ascending order. Initially, arr[0] is regarded as the sorted part, and the remaining elements are regarded as unsorted.

How to write insertion sort algorithm in Python? How to write insertion sort algorithm in Python? Sep 19, 2023 pm 04:07 PM

How to write insertion sort algorithm in Python? Insertion sort is a simple and intuitive sorting algorithm. Its idea is to divide the array to be sorted into an ordered part and an unordered part. Each time, an element is selected from the unordered part and inserted into the correct position of the ordered part. The implementation of the insertion sort algorithm is usually implemented by comparing and exchanging elements multiple times, with a time complexity of O(n^2). Let's take a look at how to write the insertion sort algorithm in Python, as well as specific code examples. definition_so

How to implement insertion sort algorithm using java How to implement insertion sort algorithm using java Sep 19, 2023 am 08:28 AM

How to implement insertion sort algorithm using Java Insertion sort is a simple but effective sorting algorithm based on the idea of ​​comparing and exchanging elements. In this article, we will learn how to write an implementation of the insertion sort algorithm in Java and provide concrete code examples. The basic idea of ​​insertion sort is to divide the array into sorted and unsorted parts. First, we treat the first element as the sorted part, and then insert the elements of the unsorted part into the correct position of the sorted part in sequence. In order to find the correct insertion position we need to

Precautions and performance optimization tips for implementing insertion sort algorithm in Java Precautions and performance optimization tips for implementing insertion sort algorithm in Java Feb 20, 2024 pm 12:27 PM

Precautions and optimization tips for writing insertion sort algorithm in Java Insertion sort is a simple but effective sorting algorithm suitable for small-scale arrays or nearly ordered arrays. Although the time complexity of insertion sort is O(n^2), due to its comparison-based nature, insertion sort can be faster than other advanced sorting algorithms in some cases. The following are considerations and optimization tips for writing insertion sort algorithms in Java. Pay Attention to Bounds Handling When writing your insertion sort algorithm, make sure you properly handle the bounds of your array. insertion sort

What is the principle and implementation of the insertion sort algorithm in PHP? What is the principle and implementation of the insertion sort algorithm in PHP? Sep 20, 2023 pm 12:49 PM

The principle and implementation of the insertion sort algorithm in PHP Insertion sort is a common sorting algorithm. Its core idea is to insert an element into an already ordered sequence according to its size. In PHP, we can implement the insertion sort algorithm through simple numerical exchange. This article will introduce the principle and implementation of insertion sort in detail, and provide specific code examples. Principle: Select an element from an unsorted sequence and insert it into the appropriate position of the sorted sequence. By comparing the element to be inserted with the elements in the sorted sequence, determine whether the element should

In-depth understanding of the insertion sort algorithm and its implementation principles in Java In-depth understanding of the insertion sort algorithm and its implementation principles in Java Feb 21, 2024 pm 09:03 PM

Deeply understand the insertion sort algorithm and its implementation principles in Java Insertion sort is a simple but commonly used sorting algorithm, and its implementation principle is also relatively simple. This article will delve into the insertion sort algorithm and its implementation principles in Java, and attach specific code examples. 1. The idea of ​​insertion sort algorithm The idea of ​​insertion sort is to insert an element to be sorted into an appropriate position in an already ordered partial sequence, thereby dividing the sequence into sorted and unsorted parts. During the sorting process, by constantly comparing and moving the positions of elements, we finally get

See all articles