Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
HW Descending Order Final
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
#python 3.5.2 print ("Hello, world!") #-------------Bubble Sort------------------- def bubbleSort(alist): length = len(alist) for passnumber in range(0, length): # number of passes = N - 1 print ('Passnumber: ', passnumber) for i in range((length-1) ,0,-1 ): # loop for bubbles print ('Possible values for i:',i) if alist[i] > alist[i-1]: temp = alist[i] # temp = 89 alist[i] = alist[i-1] # 89 with 5 alist[i-1] = temp # 89 => 5 print (i,'-->',alist) print () print ('------------------------------------------------') return alist #------------SelectionSort-------------------- def selectionSort(alist): length = len(alist) for nLastPosition in range(length-1, 0, -1): # nLastPosition -> 5 -> 4 -> 3 -> 2 -> 1 # this loop goes from last position to first position # 1: identifying the maximum number [come back to this] minIndex = 0 for numIndex in range(0, nLastPosition+1): if alist[numIndex] < alist[minIndex]: minIndex = numIndex print ("Min number: ", alist[minIndex]) temp = alist[nLastPosition] alist[nLastPosition] = alist[minIndex] alist[minIndex] = temp print ("After swap: ", alist) print () return alist #---------InsertionSort------------------------ 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 #---------------------------------------------- sampleList = [1, 5, 89, 8, 23, 1] sortedList = bubbleSort(sampleList) print (sortedList)
[
+
]
Show input
edit mode
|
history
|
discussion