Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Lesso#6 updated
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
# Object Oriented Design # Insertion Sort # Example List: [54, 26, 93, 17, 77, 31] # sorted unsorted part # Example: [ 54 | {26}, 93, 17, 77, 31 ] # sorted unsorted # -------> [ 26, 54 | {93}, 17, 77, 31 ] # -------> [ 26, 54, 93 | {17}, 77, 31 ] # -------> [ 17, 26, 54, 93 | {77}, 31 ] # -------> [ 17, 26, 54, 77, 93 | {31} ] # -------> [ 17, 26, 31, 54, 77, 93 | ] # Idea: Split the list into 2 parts - sorted and unsorted part # Take the first element in "unsorted part" and # Analyze its location in the "sorted part" def InsertionSort(alist): length = len(alist) for position in range(1, length): # 1, 2, 3 currentValue = alist[position] # = {17} while position > 0 and alist[position-1] > currentValue: alist[position] = alist[position-1] position = position - 1 # 3->2 alist[position] = currentValue print ('Updating: ', alist) return alist alist = [5, -20, -1] print (InsertionSort(alist))
[
+
]
Show input
Absolute running time: 0.24 sec, cpu time: 0.22 sec, memory peak: 6 Mb, absolute service time: 0,25 sec
edit mode
|
history
|
discussion
Updating: [-20, 5, -1] Updating: [-20, -1, 5] [-20, -1, 5]