Cute Algorithms!

  • View
    101

  • Download
    0

Embed Size (px)

DESCRIPTION

Cute Algorithms!. 稲葉 一浩 at #spro12. まずは準備運動です!. Slowest Sort. Thanks to: @ y_benjo @rng_58 @ hos_lyric @oxy @ xhl_kogitsune @args1234. アルゴリズムと言えば!. Quick Sort. ソート!. Bubble Sort. Merge Sort. Radix Sort. Insertion Sort. Heap Sort. Shell Sort. ソートと言えば!. Quick Sort. O(n log n) !. - PowerPoint PPT Presentation

Text of Cute Algorithms!

Cute Algorithm

Cute Algorithms! at #spro12SlowestSort

Thanks to: @y_benjo @rng_58 @hos_lyric @oxy @xhl_kogitsune @args1234Radix SortMerge SortBubble SortHeap SortInsertion SortQuick SortShell SortRadix SortMerge SortBubble SortHeap SortInsertion SortQuick SortShell Sort

O(n log n) !

!? Bubble Sort O(n) :

function bubble_sort(A) { for(i 0..n-2) for(j 1..n-1) if(A[j-1] > A[j]) swap(A[j-1], A[j])}123456Insertion Sort O(n)

(n2)

123456134625Quick Sort (n log n) : Quick Sort 123456123456 O(n2)n(n-1)/2 O(n log n)O(n log n)BubbleInsertQuickMergennn log nn log nn log n???n2n log nn log nn2Quick Sort + Insertion Sortfunction slow_sort(A) { // O(n log n) quick_sort(A[0 ... n/2]) // O(n2) insertion_sort(A[0...n])}

Cute!Quick Sort + Insertion SortN TimShellCombStoogeTim-Shell-Comb-Stooge-

FunctionalQueue: Purely Functional Data Structures, Chris OkasakiQueuepush(x): x pop(): 1 Queue123123Linked List class Queue { class Node { Node prev; T value; Node next; } Node head; Node tail;}nullnullLinkedListQueuenullnullnullnullnullnullpush 3popFunctional Queue: Purely Functional Data Structures

class Queue { class Node { const T value; const Node next; } const Node head; const Node tail; Queue push(T v);}nullnullclass Node { T value; Node next;}SingleLinkpushpush 3(1) Singly Linked List 19(2) popnullnullReverseNodenullpop(3) push nullnullnullnullnullpush 4push 5pushpoppoppoppush21Singly Linked List class Queue { class Node { T value; Node next; } Node for_pop; Node for_push;}nullnullfor_pop pop for_push reversefor_pop popfor_pop pop for_push reversereverse!reverse!(4) reversefor_pop reverse !push 1, 21null22null1nullpush 3, 43null4null2null1null2null14null3(5) reverseQueuereverse!push 3, 43null42null1null2null14null32null14null3ListQueue!nullclass Queue { class Node { const T value; const Node next; } const Queue for_pop; const Node for_push; }2null14null3null

Cute!Bootstrapping OS OS /Queue Queue

Bootstrappingin a maze: http://www.kmonos.net/wlog/105.html#_2232100114

Route shortest(Maze m);

Route shortest(Maze m);bool check(Maze m, Route r);Route safe_shortest(Maze m) { Route r = shortest(m); if(!check(m,r)) throw Error; return r;}shortest shortest true

Route shortest(Maze m);bool check(Maze m, Route r) { return r.len == shortest(m).len;}

Bootstrapping!shortest Route safe_shortest(Maze m);

bool check(Maze m, Route r) { // return r == safe_shortest(m); shortest!!} orBootstrapping! bool check(Maze m, Route r) { for( p,q r ) { m = mpq if( safe_shortest(m).len < r.len ) return false; } return true;}

Cute!

ExactReals: RealLib, Barnimir Lambov, 2007- http://wayback.archive.org/web/*/www.brics.dk/~barnie/RealLib/using namespace RealLib;int main() { Real sq3 = cos(Pi/3-Pi/2)*2; Real x = 1; for(int i=0; i