Outline Objectives of this Lecture The Specification of Sorting Insertion Sort HeapSort System Modelling and Design Modelling: Sorting Algorithms Revision: 1.3, May 9, 2008 Ken Robinson School of Computer Science & Engineering The University of New South Wales, Sydney Australia May 18, 2010 � Ken Robinson 2005-2010 c mailto::k.robinson@unsw.edu.au
Outline Objectives of this Lecture The Specification of Sorting Insertion Sort HeapSort Outline I The Specification of Sorting Abstract Specification of Sorting Insertion Sort The Insertion Sort Plan InsertionSortR1: More Refinement InsertionSortR2: Discovering m and n InsertionSortR3: The Concrete Algorithm HeapSort
Outline Objectives of this Lecture The Specification of Sorting Insertion Sort HeapSort Objectives of this Lecture • to model a number of sorting algorithms to illustrate the use of modelling to gain understanding of a proposed design; • the objective is understanding, not sorting algorithms in themselves; • to illustrate the refinement process
Outline Objectives of this Lecture The Specification of Sorting Insertion Sort HeapSort Objectives of this Lecture • to model a number of sorting algorithms to illustrate the use of modelling to gain understanding of a proposed design; • the objective is understanding, not sorting algorithms in themselves; • to illustrate the refinement process
Outline Objectives of this Lecture The Specification of Sorting Insertion Sort HeapSort Objectives of this Lecture • to model a number of sorting algorithms to illustrate the use of modelling to gain understanding of a proposed design; • the objective is understanding, not sorting algorithms in themselves; • to illustrate the refinement process
Outline Objectives of this Lecture The Specification of Sorting Insertion Sort HeapSort Objectives of this Lecture • to model a number of sorting algorithms to illustrate the use of modelling to gain understanding of a proposed design; • the objective is understanding, not sorting algorithms in themselves; • to illustrate the refinement process
Outline Objectives of this Lecture The Specification of Sorting Insertion Sort HeapSort The Specification of Sorting In this development we will describe sorting an injective sequence of numbers. Making the sequence injective avoids having to deal with multiple instances of the same value in the sequence. This is done to make the process just a little simpler. The following context contains the definitions required for specifying sequences and a predicate function isSorted ( s )( m )( n ) for determining whether the sequence s is sorted (monotonically increasing) in the domain subrange m .. n .
Outline Objectives of this Lecture The Specification of Sorting Insertion Sort HeapSort The Specification of Sorting In this development we will describe sorting an injective sequence of numbers. Making the sequence injective avoids having to deal with multiple instances of the same value in the sequence. This is done to make the process just a little simpler. The following context contains the definitions required for specifying sequences and a predicate function isSorted ( s )( m )( n ) for determining whether the sequence s is sorted (monotonically increasing) in the domain subrange m .. n .
Outline Objectives of this Lecture The Specification of Sorting Insertion Sort HeapSort CONTEXT Sorting ctx
Outline Objectives of this Lecture The Specification of Sorting Insertion Sort HeapSort CONSTANTS length The length of a sequence ISEQ The set of injective sequences DOM 0 Domain of possibly empty sequences DOM 1 Domain of non-empty sequences PERM The set of sequence permutations isSORTED Predicate for determining sortedness UNSORTED An arbitrary sequence
Outline Objectives of this Lecture The Specification of Sorting Insertion Sort HeapSort AXIOMS axm 1: length ∈ N 1 axm 2: DOM 0 = 0 .. length axm 3: DOM 1 = 1 .. length ISEQ = DOM 1 N axm 4: All injective sequences of natural numbers with domain DOM1 axm 8: PERM = DOM 1 ։ DOM 1 axm 9: isSORTED ∈ ISEQ → ( DOM 1 → ( DOM 0 → BOOL )) axm 10: ∀ s , m , n · s ∈ ISEQ ∧ m ∈ DOM 1 ∧ n ∈ DOM 0 ⇒ isSORTED ( s )( m )( n ) = bool ( ∀ i , j · i ∈ m .. n ∧ j ∈ m .. n ∧ i < j ⇒ s ( i ) < s ( j )) axm 11: UNSORTED ∈ ISEQ
Outline Objectives of this Lecture The Specification of Sorting Insertion Sort HeapSort THEOREMS thm 1: ∀ m , n · m ∈ DOM 1 ∧ n ∈ DOM 1 ⇒ { i · i ∈ m .. n | i + 1 �→ i } ∈ m + 1 .. n + 1 ։ m .. n ∀ m , n · m ∈ DOM 1 ∧ n ∈ DOM 1 thm 2: ⇒ dom ( { i · i ∈ m .. n | i + 1 �→ i } ) = m + 1 .. n + 1 thm 3: ∀ m , n · m ∈ DOM 1 ∧ n ∈ DOM 1 ⇒ ran ( { i · i ∈ m .. n | i + 1 �→ i } ) = m .. n thm 4: dom ( isSORTED ) = ISEQ
Outline Objectives of this Lecture The Specification of Sorting Insertion Sort HeapSort thm 5: ∀ s · s ∈ ISEQ ⇒ dom ( isSORTED ( s )) = dom ( s ) thm 6: ∀ s , m · s ∈ ISEQ ∧ m ∈ dom ( s ) ⇒ dom ( isSORTED ( s )( m )) = DOM 0 thm 7: ∀ s , m , n · s ∈ ISEQ ∧ m ∈ DOM 1 ∧ n ∈ DOM 0 ∧ m > n ⇒ isSORTED ( s )( m )( n ) = TRUE thm 8: ∀ s , m · s ∈ ISEQ ∧ m ∈ DOM 1 ⇒ isSORTED ( s )( m )( m ) = TRUE
Outline Objectives of this Lecture The Specification of Sorting Insertion Sort HeapSort thm 9: ∀ s , m , n · s ∈ ISEQ ∧ m ∈ DOM 1 ∧ n ∈ DOM 1 ∧ n + 1 ∈ DOM 1 ∧ isSORTED ( s )( m )( n ) = TRUE ∧ s ( n ) < s ( n + 1 ) ⇒ isSORTED ( s )( m )( n + 1 ) = TRUE
Outline Objectives of this Lecture The Specification of Sorting Insertion Sort HeapSort thm 10: ∀ s , l , m , n · s ∈ ISEQ ∧ l ∈ DOM 1 ∧ m ∈ DOM 1 ∧ n ∈ DOM 0 ∧ l ≤ m ∧ m ≤ n ∧ isSORTED ( s )( l )( m ) = TRUE ∧ isSORTED ( s )( m )( n ) = TRUE ⇒ isSORTED ( s )( l )( n ) = TRUE
Outline Objectives of this Lecture The Specification of Sorting Insertion Sort HeapSort thm 11: ∀ s · s ∈ ISEQ ⇒ s − 1 ; s = id ( ran ( s )) thm 12: ∀ s , t · s ∈ ISEQ ∧ t ∈ ISEQ ∧ ran ( s ) = ran ( t ) ⇒ s ; t − 1 ; t = s thm 13: ∀ s · s ∈ ISEQ ⇒ s ; id ( ran ( s )) = s
Outline Objectives of this Lecture The Specification of Sorting Insertion Sort HeapSort thm 14: ∀ p , s · p ∈ PERM ∧ s ∈ ISEQ ⇒ p ; s ∈ ISEQ thm 15: ∀ m , p , s · 1 ≤ m ∧ m ≤ length ∧ p ∈ 1 .. m ։ 1 .. m ∧ s ∈ ISEQ ⇒ dom ( p ; s ) = 1 .. m
Outline Objectives of this Lecture The Specification of Sorting Insertion Sort HeapSort thm 16: ∀ s · s ∈ ISEQ ⇒ s ; s − 1 = id ( DOM 1 ) thm 17: ∀ s · s ∈ ISEQ ⇒ s − 1 ; s = id ( ran ( s )) END
Outline Objectives of this Lecture The Specification of Sorting Insertion Sort HeapSort Abstract Specification of Sorting In the following Sort machine, the Sort event creates a sorted sequence by proposing a permutation p that will transform the unsorted sequence u into a sorted sequence s by relational composition: p ; u = s Composing both sides of the equality on the right by u − 1 gives p ; u ; u − 1 = s ; u − 1 giving the required permutaion as p = s ; u − 1 Of course, that’s all very well after the fact, and it is the job of a sorting algorithm to effectively compute that permutation.
Outline Objectives of this Lecture The Specification of Sorting Insertion Sort HeapSort Abstract Specification of Sorting In the following Sort machine, the Sort event creates a sorted sequence by proposing a permutation p that will transform the unsorted sequence u into a sorted sequence s by relational composition: p ; u = s Composing both sides of the equality on the right by u − 1 gives p ; u ; u − 1 = s ; u − 1 giving the required permutaion as p = s ; u − 1 Of course, that’s all very well after the fact, and it is the job of a sorting algorithm to effectively compute that permutation.
Outline Objectives of this Lecture The Specification of Sorting Insertion Sort HeapSort Abstract Specification of Sorting In the following Sort machine, the Sort event creates a sorted sequence by proposing a permutation p that will transform the unsorted sequence u into a sorted sequence s by relational composition: p ; u = s Composing both sides of the equality on the right by u − 1 gives p ; u ; u − 1 = s ; u − 1 giving the required permutaion as p = s ; u − 1 Of course, that’s all very well after the fact, and it is the job of a sorting algorithm to effectively compute that permutation.
Outline Objectives of this Lecture The Specification of Sorting Insertion Sort HeapSort Abstract Specification of Sorting In the following Sort machine, the Sort event creates a sorted sequence by proposing a permutation p that will transform the unsorted sequence u into a sorted sequence s by relational composition: p ; u = s Composing both sides of the equality on the right by u − 1 gives p ; u ; u − 1 = s ; u − 1 giving the required permutaion as p = s ; u − 1 Of course, that’s all very well after the fact, and it is the job of a sorting algorithm to effectively compute that permutation.
Recommend
More recommend