:: Sorting by Exchanging
:: by Grzegorz Bancerek
::
:: Copyright (c) 2010-2021 Association of Mizar Users

theorem Th1: :: EXCHSORT:1
for a, b being Ordinal
for x being set holds
( x in (a +^ b) \ a iff ex c being Ordinal st
( x = a +^ c & c in b ) )
proof end;

defpred S1[ set , set , set , set ] means ( $3 <>$1 & $3 <>$2 & $4 <>$1 & $4 <>$2 );

defpred S2[ set , set , set , set ] means ( $3 in$1 & $4 =$1 );

defpred S3[ set , set , set , set ] means ( $3 in$1 & $4 =$2 );

defpred S4[ set , set , set , set ] means ( $3 =$1 & $4 in$2 );

defpred S5[ set , set , set , set ] means ( $3 =$1 & $4 =$2 );

defpred S6[ set , set , set , set ] means ( $3 =$1 & $2 in$4 );

defpred S7[ set , set , set , set ] means ( $1 in$3 & $4 =$2 );

defpred S8[ set , set , set , set ] means ( $3 =$2 & $2 in$4 );

theorem Th2: :: EXCHSORT:2
for a, b, c, d being Ordinal st a in b & c in d & not ( c <> a & c <> b & d <> a & d <> b ) & not ( c in a & d = a ) & not ( c in a & d = b ) & not ( c = a & d in b ) & not ( c = a & d = b ) & not ( c = a & b in d ) & not ( a in c & d = b ) holds
( c = b & b in d )
proof end;

theorem :: EXCHSORT:3
for x, y being set st x nin y holds
(y \/ {x}) \ y = {x} by ;

theorem Th4: :: EXCHSORT:4
for x being set holds (succ x) \ x = {x}
proof end;

theorem Th5: :: EXCHSORT:5
for f being Function
for r being Relation
for x being object holds
( x in f .: r iff ex y, z being object st
( [y,z] in r & [y,z] in dom f & f . (y,z) = x ) )
proof end;

theorem Th6: :: EXCHSORT:6
for a, b being Ordinal st a \ b <> {} holds
( inf (a \ b) = b & sup (a \ b) = a & union (a \ b) = union a )
proof end;

theorem Th7: :: EXCHSORT:7
for a, b being Ordinal st not a \ b is empty & a \ b is finite holds
ex n being Nat st a = b +^ n
proof end;

definition
let f be set ;
attr f is segmental means :Def1: :: EXCHSORT:def 1
ex a, b being Ordinal st proj1 f = a \ b;
end;

:: deftheorem Def1 defines segmental EXCHSORT:def 1 :
for f being set holds
( f is segmental iff ex a, b being Ordinal st proj1 f = a \ b );

theorem :: EXCHSORT:8
for f, g being Function st dom f = dom g & f is segmental holds
g is segmental ;

theorem Th9: :: EXCHSORT:9
for f being Function st f is segmental holds
for a, b, c being Ordinal st a c= b & b c= c & a in dom f & c in dom f holds
b in dom f
proof end;

registration
coherence
for b1 being Function st b1 is Sequence-like holds
b1 is segmental
proof end;
coherence
for b1 being Function st b1 is FinSequence-like holds
b1 is segmental
proof end;
end;

definition
let a be Ordinal;
let s be set ;
attr s is a -based means :: EXCHSORT:def 2
for b being Ordinal st b in proj1 s holds
( a in proj1 s & a c= b );
attr s is a -limited means :: EXCHSORT:def 3
a = sup ();
end;

:: deftheorem defines -based EXCHSORT:def 2 :
for a being Ordinal
for s being set holds
( s is a -based iff for b being Ordinal st b in proj1 s holds
( a in proj1 s & a c= b ) );

:: deftheorem defines -limited EXCHSORT:def 3 :
for a being Ordinal
for s being set holds
( s is a -limited iff a = sup () );

theorem :: EXCHSORT:10
for a being Ordinal
for f being Function holds
( ( f is a -based & f is segmental ) iff ex b being Ordinal st
( dom f = b \ a & a c= b ) )
proof end;

theorem :: EXCHSORT:11
for b being Ordinal
for f being Function holds
( ( f is b -limited & not f is empty & f is segmental ) iff ex a being Ordinal st
( dom f = b \ a & a in b ) )
proof end;

registration
coherence
for b1 being Function st b1 is Sequence-like holds
b1 is 0 -based
by ORDINAL3:8;
coherence
for b1 being Function st b1 is FinSequence-like holds
b1 is 1 -based
proof end;
end;

theorem Th12: :: EXCHSORT:12
for f being Function holds f is inf (dom f) -based by ;

theorem :: EXCHSORT:13
for f being Function holds f is sup (dom f) -limited ;

theorem :: EXCHSORT:14
for a, b being Ordinal
for f being Function st f is b -limited & a in dom f holds
a in b by ORDINAL2:19;

definition
let f be Function;
func base- f -> Ordinal means :Def4: :: EXCHSORT:def 4
f is it -based if ex a being Ordinal st a in dom f
otherwise it = 0 ;
existence
( ( ex a being Ordinal st a in dom f implies ex b1 being Ordinal st f is b1 -based ) & ( ( for a being Ordinal holds not a in dom f ) implies ex b1 being Ordinal st b1 = 0 ) )
proof end;
uniqueness
for b1, b2 being Ordinal holds
( ( ex a being Ordinal st a in dom f & f is b1 -based & f is b2 -based implies b1 = b2 ) & ( ( for a being Ordinal holds not a in dom f ) & b1 = 0 & b2 = 0 implies b1 = b2 ) )
proof end;
consistency
for b1 being Ordinal holds verum
;
func limit- f -> Ordinal means :Def5: :: EXCHSORT:def 5
f is it -limited if ex a being Ordinal st a in dom f
otherwise it = 0 ;
existence
( ( ex a being Ordinal st a in dom f implies ex b1 being Ordinal st f is b1 -limited ) & ( ( for a being Ordinal holds not a in dom f ) implies ex b1 being Ordinal st b1 = 0 ) )
proof end;
uniqueness
for b1, b2 being Ordinal holds
( ( ex a being Ordinal st a in dom f & f is b1 -limited & f is b2 -limited implies b1 = b2 ) & ( ( for a being Ordinal holds not a in dom f ) & b1 = 0 & b2 = 0 implies b1 = b2 ) )
;
consistency
for b1 being Ordinal holds verum
;
end;

:: deftheorem Def4 defines base- EXCHSORT:def 4 :
for f being Function
for b2 being Ordinal holds
( ( ex a being Ordinal st a in dom f implies ( b2 = base- f iff f is b2 -based ) ) & ( ( for a being Ordinal holds not a in dom f ) implies ( b2 = base- f iff b2 = 0 ) ) );

:: deftheorem Def5 defines limit- EXCHSORT:def 5 :
for f being Function
for b2 being Ordinal holds
( ( ex a being Ordinal st a in dom f implies ( b2 = limit- f iff f is b2 -limited ) ) & ( ( for a being Ordinal holds not a in dom f ) implies ( b2 = limit- f iff b2 = 0 ) ) );

definition
let f be Function;
func len- f -> Ordinal equals :: EXCHSORT:def 6
() -^ ();
coherence
() -^ () is Ordinal
;
end;

:: deftheorem defines len- EXCHSORT:def 6 :
for f being Function holds len- f = () -^ ();

theorem Th15: :: EXCHSORT:15
proof end;

theorem Th16: :: EXCHSORT:16
for f being Function holds limit- f = sup (dom f)
proof end;

theorem :: EXCHSORT:17
for f being Function holds f is limit- f -limited by Th16;

theorem :: EXCHSORT:18
for a being Ordinal
for A being empty set holds A is a -based ;

registration
let a be Ordinal;
let X, Y be set ;
existence
ex b1 being Sequence st
( b1 is Y -defined & b1 is X -valued & b1 is a -based & b1 is segmental & b1 is finite & b1 is empty )
proof end;
end;

definition end;

registration
let A be array;
coherence
proof end;
end;

theorem :: EXCHSORT:19
for f being array holds
( f is 0 -limited iff f is empty )
proof end;

registration
coherence
for b1 being array st b1 is 0 -based holds
b1 is Sequence-like
proof end;
end;

definition
let X be set ;
mode array of X is X -valued array;
end;

definition
let X be 1-sorted ;
mode array of X is array of the carrier of X;
end;

definition
let a be Ordinal;
let X be set ;
mode array of a,X is a -defined array of X;
end;

theorem Th20: :: EXCHSORT:20
for f being Function holds base- f = inf (dom f)
proof end;

theorem :: EXCHSORT:21
for f being Function holds f is base- f -based
proof end;

theorem :: EXCHSORT:22
for A being array holds dom A = () \ ()
proof end;

theorem Th23: :: EXCHSORT:23
for a, b being Ordinal
for A being array st dom A = a \ b & not A is empty holds
( base- A = b & limit- A = a )
proof end;

theorem Th24: :: EXCHSORT:24
for f being Sequence holds
( base- f = 0 & limit- f = dom f & len- f = dom f )
proof end;

registration
let a, b be Ordinal;
let X be set ;
existence
ex b1 being array of a,X st
( b1 is b -based & b1 is natural-valued & b1 is INT -valued & b1 is real-valued & b1 is complex-valued & b1 is finite )
proof end;
end;

registration
let a be Ordinal;
let x be set ;
cluster {[a,x]} -> segmental ;
coherence
{[a,x]} is segmental
proof end;
end;

registration
let a be Ordinal;
let x be Nat;
cluster {[a,x]} -> natural-valued for array;
coherence
for b1 being array st b1 = {[a,x]} holds
b1 is natural-valued
proof end;
end;

registration
let a be Ordinal;
let x be Real;
cluster {[a,x]} -> real-valued for array;
coherence
for b1 being array st b1 = {[a,x]} holds
b1 is real-valued
proof end;
end;

registration
let a be Ordinal;
let X be non empty set ;
let x be Element of X;
cluster {[a,x]} -> X -valued for array;
coherence
for b1 being array st b1 = {[a,x]} holds
b1 is X -valued
proof end;
end;

registration
let a be Ordinal;
let x be set ;
cluster {[a,x]} -> a -based succ a -limited for array;
coherence
for b1 being array st b1 = {[a,x]} holds
( b1 is a -based & b1 is succ a -limited )
proof end;
end;

registration
let b be Ordinal;
existence
ex b1 being array st
( not b1 is empty & b1 is b -based & b1 is natural-valued & b1 is INT -valued & b1 is real-valued & b1 is complex-valued & b1 is finite )
proof end;
let X be non empty set ;
existence
ex b1 being array st
( not b1 is empty & b1 is b -based & b1 is finite & b1 is X -valued )
proof end;
end;

notation
let s be Sequence;
synonym s last for last s;
end;

definition
let A be array;
func last A -> set equals :: EXCHSORT:def 7
A . (union (dom A));
coherence
A . (union (dom A)) is set
;
end;

:: deftheorem defines last EXCHSORT:def 7 :
for A being array holds last A = A . (union (dom A));

registration
let A be Sequence;
identify A last with last A;
compatibility ;
end;

definition
let f be Function;
attr f is descending means :: EXCHSORT:def 8
for a, b being Ordinal st a in dom f & b in dom f & a in b holds
f . b c< f . a;
end;

:: deftheorem defines descending EXCHSORT:def 8 :
for f being Function holds
( f is descending iff for a, b being Ordinal st a in dom f & b in dom f & a in b holds
f . b c< f . a );

theorem :: EXCHSORT:25
for f being finite array st ( for a being Ordinal st a in dom f & succ a in dom f holds
f . (succ a) c< f . a ) holds
f is descending
proof end;

theorem Th26: :: EXCHSORT:26
for f being array st len- f = omega & ( for a being Ordinal st a in dom f & succ a in dom f holds
f . (succ a) c< f . a ) holds
f is descending
proof end;

theorem Th27: :: EXCHSORT:27
for f being Sequence st f is descending & f . 0 is finite holds
f is finite
proof end;

theorem :: EXCHSORT:28
for f being Sequence st f is descending & f . 0 is finite & ( for a being Ordinal st f . a <> {} holds
succ a in dom f ) holds
last f = {}
proof end;

scheme :: EXCHSORT:sch 1
A{ F1() -> Sequence, F2( set ) -> set } :
F1() is finite
provided
A1: F2((F1() . 0)) is finite and
A2: for a being Ordinal st succ a in dom F1() & F2((F1() . a)) is finite holds
F2((F1() . (succ a))) c< F2((F1() . a))
proof end;

registration
let X be set ;
let f be X -defined Function;
let a, b be object ;
cluster Swap (f,a,b) -> X -defined ;
coherence
Swap (f,a,b) is X -defined
proof end;
end;

registration
let X be set ;
let f be X -valued Function;
let x, y be object ;
cluster Swap (f,x,y) -> X -valued ;
coherence
Swap (f,x,y) is X -valued
proof end;
end;

theorem Th29: :: EXCHSORT:29
for x, y being set
for f being Function st x in dom f & y in dom f holds
(Swap (f,x,y)) . x = f . y
proof end;

theorem Th30: :: EXCHSORT:30
for x, y, X being set
for f being array of X st x in dom f & y in dom f holds
(Swap (f,x,y)) /. x = f /. y
proof end;

theorem Th31: :: EXCHSORT:31
for x, y being set
for f being Function st x in dom f & y in dom f holds
(Swap (f,x,y)) . y = f . x
proof end;

theorem Th32: :: EXCHSORT:32
for x, y, X being set
for f being array of X st x in dom f & y in dom f holds
(Swap (f,x,y)) /. y = f /. x
proof end;

theorem Th33: :: EXCHSORT:33
for f being Function
for x, y, z being object st z <> x & z <> y holds
(Swap (f,x,y)) . z = f . z
proof end;

theorem Th34: :: EXCHSORT:34
for x, y, z, X being set
for f being array of X st z in dom f & z <> x & z <> y holds
(Swap (f,x,y)) /. z = f /. z
proof end;

theorem :: EXCHSORT:35
for x, y being set
for f being Function st x in dom f & y in dom f holds
Swap (f,x,y) = Swap (f,y,x)
proof end;

registration
let X be non empty set ;
existence
ex b1 being X -valued non empty Function st b1 is onto
proof end;
end;

registration
let X be non empty set ;
let f be X -valued non empty onto Function;
let x, y be Element of dom f;
cluster Swap (f,x,y) -> onto ;
coherence
Swap (f,x,y) is onto
proof end;
end;

registration
let A be array;
let x, y be set ;
cluster Swap (A,x,y) -> segmental ;
coherence
Swap (A,x,y) is segmental
proof end;
end;

theorem :: EXCHSORT:36
for x, y being set
for A being array st x in dom A & y in dom A holds
Swap ((Swap (A,x,y)),x,y) = A
proof end;

registration
let A be real-valued array;
let x, y be set ;
cluster Swap (A,x,y) -> real-valued for array;
coherence
for b1 being array st b1 = Swap (A,x,y) holds
b1 is real-valued
proof end;
end;

definition
let A be array;
mode permutation of A -> array means :Def9: :: EXCHSORT:def 9
ex f being Permutation of (dom A) st it = A * f;
existence
ex b1 being array ex f being Permutation of (dom A) st b1 = A * f
proof end;
end;

:: deftheorem Def9 defines permutation EXCHSORT:def 9 :
for A, b2 being array holds
( b2 is permutation of A iff ex f being Permutation of (dom A) st b2 = A * f );

theorem Th37: :: EXCHSORT:37
for A being array
for B being permutation of A holds
( dom B = dom A & rng B = rng A )
proof end;

theorem Th38: :: EXCHSORT:38
for A being array holds A is permutation of A
proof end;

theorem Th39: :: EXCHSORT:39
for A, B being array st A is permutation of B holds
B is permutation of A
proof end;

theorem Th40: :: EXCHSORT:40
for A, B, C being array st A is permutation of B & B is permutation of C holds
A is permutation of C
proof end;

theorem Th41: :: EXCHSORT:41
for x, y, X being set holds Swap ((id X),x,y) is one-to-one
proof end;

registration
let X be non empty set ;
let x, y be Element of X;
cluster Swap ((id X),x,y) -> X -defined X -valued one-to-one ;
coherence
( Swap ((id X),x,y) is one-to-one & Swap ((id X),x,y) is X -valued & Swap ((id X),x,y) is X -defined )
by Th41;
end;

registration
let X be non empty set ;
let x, y be Element of X;
cluster Swap ((id X),x,y) -> total onto ;
coherence
( Swap ((id X),x,y) is onto & Swap ((id X),x,y) is total )
proof end;
end;

definition
let X, Y be non empty set ;
let f be Function of X,Y;
let x, y be Element of X;
:: original: Swap
redefine func Swap (f,x,y) -> Function of X,Y;
coherence
Swap (f,x,y) is Function of X,Y
proof end;
end;

theorem Th42: :: EXCHSORT:42
for x, y, X being set
for f being Function
for A being array st x in X & y in X & f = Swap ((id X),x,y) & X = dom A holds
Swap (A,x,y) = A * f
proof end;

theorem Th43: :: EXCHSORT:43
for x, y being set
for A being array st x in dom A & y in dom A holds
( Swap (A,x,y) is permutation of A & A is permutation of Swap (A,x,y) )
proof end;

theorem :: EXCHSORT:44
for x, y being set
for A, B being array st x in dom A & y in dom A & A is permutation of B holds
( Swap (A,x,y) is permutation of B & A is permutation of Swap (B,x,y) )
proof end;

definition
let O be RelStr ;
let A be array of O;
attr A is ascending means :: EXCHSORT:def 10
for a, b being Ordinal st a in dom A & b in dom A & a in b holds
A /. a <= A /. b;
func inversions A -> set equals :: EXCHSORT:def 11
{ [a,b] where a, b is Element of dom A : ( a in b & not A /. a <= A /. b ) } ;
coherence
{ [a,b] where a, b is Element of dom A : ( a in b & not A /. a <= A /. b ) } is set
;
end;

:: deftheorem defines ascending EXCHSORT:def 10 :
for O being RelStr
for A being array of O holds
( A is ascending iff for a, b being Ordinal st a in dom A & b in dom A & a in b holds
A /. a <= A /. b );

:: deftheorem defines inversions EXCHSORT:def 11 :
for O being RelStr
for A being array of O holds inversions A = { [a,b] where a, b is Element of dom A : ( a in b & not A /. a <= A /. b ) } ;

registration
let O be RelStr ;
coherence
for b1 being empty array of O holds b1 is ascending
;
let A be empty array of O;
coherence
proof end;
end;

theorem Th45: :: EXCHSORT:45
for O being non empty connected Poset
for x, y being Element of O holds
( x > y iff not x <= y )
proof end;

definition
let O be non empty connected Poset;
let R be array of O;
redefine func inversions R equals :: EXCHSORT:def 12
{ [a,b] where a, b is Element of dom R : ( a in b & R /. a > R /. b ) } ;
compatibility
for b1 being set holds
( b1 = inversions R iff b1 = { [a,b] where a, b is Element of dom R : ( a in b & R /. a > R /. b ) } )
proof end;
end;

:: deftheorem defines inversions EXCHSORT:def 12 :
for O being non empty connected Poset
for R being array of O holds inversions R = { [a,b] where a, b is Element of dom R : ( a in b & R /. a > R /. b ) } ;

theorem Th46: :: EXCHSORT:46
for O being non empty connected Poset
for R being array of O
for x being object
for y being set holds
( [x,y] in inversions R iff ( x in dom R & y in dom R & x in y & R /. x > R /. y ) )
proof end;

theorem Th47: :: EXCHSORT:47
for O being non empty connected Poset
for R being array of O holds inversions R c= [:(dom R),(dom R):]
proof end;

registration
let O be non empty connected Poset;
let R be finite array of O;
coherence
proof end;
end;

theorem Th48: :: EXCHSORT:48
for O being non empty connected Poset
for R being array of O holds
( R is ascending iff inversions R = {} )
proof end;

theorem Th49: :: EXCHSORT:49
for x, y being set
for O being non empty connected Poset
for R being array of O st [x,y] in inversions R holds
[y,x] nin inversions R
proof end;

theorem Th50: :: EXCHSORT:50
for x, y, z being set
for O being non empty connected Poset
for R being array of O st [x,y] in inversions R & [y,z] in inversions R holds
[x,z] in inversions R
proof end;

registration
let O be non empty connected Poset;
let R be array of O;
coherence
proof end;
end;

registration
let O be non empty connected Poset;
let R be array of O;
coherence
for b1 being Relation st b1 = inversions R holds
( b1 is asymmetric & b1 is transitive )
proof end;
end;

definition
let O be non empty connected Poset;
let a, b be Element of O;
:: original: <
redefine pred a < b;
asymmetry
for a, b being Element of O st R72(O,b1,b2) holds
not R72(O,b2,b1)
by ORDERS_2:5;
end;

theorem Th51: :: EXCHSORT:51
for x, y being set
for O being non empty connected Poset
for R being array of O st [x,y] in inversions R holds
[x,y] nin inversions (Swap (R,x,y))
proof end;

theorem Th52: :: EXCHSORT:52
for x, y, z, t being set
for O being non empty connected Poset
for R being array of O st x in dom R & y in dom R & z <> x & z <> y & t <> x & t <> y holds
( [z,t] in inversions R iff [z,t] in inversions (Swap (R,x,y)) )
proof end;

theorem Th53: :: EXCHSORT:53
for x, y, z being set
for O being non empty connected Poset
for R being array of O st [x,y] in inversions R holds
( ( [z,y] in inversions R & z in x ) iff [z,x] in inversions (Swap (R,x,y)) )
proof end;

theorem Th54: :: EXCHSORT:54
for x, y, z being set
for O being non empty connected Poset
for R being array of O st [x,y] in inversions R holds
( [z,x] in inversions R iff ( z in x & [z,y] in inversions (Swap (R,x,y)) ) )
proof end;

theorem Th55: :: EXCHSORT:55
for x, y, z being set
for O being non empty connected Poset
for R being array of O st [x,y] in inversions R & z in y & [x,z] in inversions (Swap (R,x,y)) holds
[x,z] in inversions R
proof end;

theorem Th56: :: EXCHSORT:56
for x, y, z being set
for O being non empty connected Poset
for R being array of O st [x,y] in inversions R & x in z & [z,y] in inversions (Swap (R,x,y)) holds
[z,y] in inversions R
proof end;

theorem Th57: :: EXCHSORT:57
for x, y, z being set
for O being non empty connected Poset
for R being array of O st [x,y] in inversions R & y in z & [x,z] in inversions (Swap (R,x,y)) holds
[y,z] in inversions R
proof end;

theorem Th58: :: EXCHSORT:58
for x, y, z being set
for O being non empty connected Poset
for R being array of O st [x,y] in inversions R holds
( ( y in z & [x,z] in inversions R ) iff [y,z] in inversions (Swap (R,x,y)) )
proof end;

definition
let O be non empty connected Poset;
let R be array of O;
let x, y be set ;
func (R,x,y) incl -> Function equals :: EXCHSORT:def 13
[:(Swap ((id (dom R)),x,y)),(Swap ((id (dom R)),x,y)):] +* (id ([:{x},((succ y) \ x):] \/ [:((succ y) \ x),{y}:]));
coherence
[:(Swap ((id (dom R)),x,y)),(Swap ((id (dom R)),x,y)):] +* (id ([:{x},((succ y) \ x):] \/ [:((succ y) \ x),{y}:])) is Function
;
end;

:: deftheorem defines incl EXCHSORT:def 13 :
for O being non empty connected Poset
for R being array of O
for x, y being set holds (R,x,y) incl = [:(Swap ((id (dom R)),x,y)),(Swap ((id (dom R)),x,y)):] +* (id ([:{x},((succ y) \ x):] \/ [:((succ y) \ x),{y}:]));

theorem Th59: :: EXCHSORT:59
for a, b, c being Ordinal holds
( c in (succ b) \ a iff ( a c= c & c c= b ) )
proof end;

theorem Th60: :: EXCHSORT:60
for O being non empty connected Poset
for T being non empty array of O
for p, q being Element of dom T holds (succ q) \ p c= dom T
proof end;

theorem Th61: :: EXCHSORT:61
for O being non empty connected Poset
for T being non empty array of O
for p, q being Element of dom T holds
( dom ((T,p,q) incl) = [:(dom T),(dom T):] & rng ((T,p,q) incl) c= [:(dom T),(dom T):] )
proof end;

theorem Th62: :: EXCHSORT:62
for O being non empty connected Poset
for T being non empty array of O
for p, q, r being Element of dom T st p c= r & r c= q holds
( ((T,p,q) incl) . (p,r) = [p,r] & ((T,p,q) incl) . (r,q) = [r,q] )
proof end;

theorem Th63: :: EXCHSORT:63
for f being Function
for O being non empty connected Poset
for T being non empty array of O
for p, q, r, s being Element of dom T st r <> p & s <> q & f = Swap ((id (dom T)),p,q) holds
((T,p,q) incl) . (r,s) = [(f . r),(f . s)]
proof end;

theorem Th64: :: EXCHSORT:64
for f being Function
for O being non empty connected Poset
for T being non empty array of O
for p, q, r being Element of dom T st r in p & f = Swap ((id (dom T)),p,q) holds
( ((T,p,q) incl) . (r,q) = [(f . r),(f . q)] & ((T,p,q) incl) . (r,p) = [(f . r),(f . p)] )
proof end;

theorem Th65: :: EXCHSORT:65
for f being Function
for O being non empty connected Poset
for T being non empty array of O
for p, q, r being Element of dom T st q in r & f = Swap ((id (dom T)),p,q) holds
( ((T,p,q) incl) . (p,r) = [(f . p),(f . r)] & ((T,p,q) incl) . (q,r) = [(f . q),(f . r)] )
proof end;

theorem Th66: :: EXCHSORT:66
for O being non empty connected Poset
for T being non empty array of O
for p, q being Element of dom T st p in q holds
((T,p,q) incl) . (p,q) = [p,q]
proof end;

theorem Th67: :: EXCHSORT:67
for O being non empty connected Poset
for T being non empty array of O
for p, q, r, s being Element of dom T st p in q & r <> p & r <> q & s <> p & s <> q holds
((T,p,q) incl) . (r,s) = [r,s]
proof end;

theorem Th68: :: EXCHSORT:68
for O being non empty connected Poset
for T being non empty array of O
for p, q, r being Element of dom T st r in p & p in q holds
( ((T,p,q) incl) . (r,p) = [r,q] & ((T,p,q) incl) . (r,q) = [r,p] )
proof end;

theorem Th69: :: EXCHSORT:69
for O being non empty connected Poset
for T being non empty array of O
for p, q, s being Element of dom T st p in s & s in q holds
( ((T,p,q) incl) . (p,s) = [p,s] & ((T,p,q) incl) . (s,q) = [s,q] )
proof end;

theorem Th70: :: EXCHSORT:70
for O being non empty connected Poset
for T being non empty array of O
for p, q, s being Element of dom T st p in q & q in s holds
( ((T,p,q) incl) . (p,s) = [q,s] & ((T,p,q) incl) . (q,s) = [p,s] )
proof end;

Lm1: for f being Function
for O being non empty connected Poset
for T being non empty array of O
for p, q being Element of dom T st p in q & f = (T,p,q) incl holds
for x1, x2, y1, y2 being Element of dom T st x1 in y1 & x2 in y2 & f . (x1,y1) = f . (x2,y2) holds
( x1 = x2 & y1 = y2 )

proof end;

theorem Th71: :: EXCHSORT:71
for O being non empty connected Poset
for T being non empty array of O
for p, q being Element of dom T st p in q holds
((T,p,q) incl) | (inversions (Swap (T,p,q))) is one-to-one
proof end;

registration
let O be non empty connected Poset;
let R be array of O;
let x, y, z be set ;
cluster ((R,x,y) incl) .: z -> Relation-like ;
coherence
((R,x,y) incl) .: z is Relation-like
proof end;
end;

theorem Th72: :: EXCHSORT:72
for x, y being set
for O being non empty connected Poset
for R being array of O st [x,y] in inversions R holds
((R,x,y) incl) .: (inversions (Swap (R,x,y))) c< inversions R
proof end;

registration
let R be finite Function;
let x, y be set ;
cluster Swap (R,x,y) -> finite ;
coherence
Swap (R,x,y) is finite
proof end;
end;

theorem Th73: :: EXCHSORT:73
for x, y being set
for O being non empty connected Poset
for R being array of O st [x,y] in inversions R & inversions R is finite holds
card (inversions (Swap (R,x,y))) in card ()
proof end;

theorem :: EXCHSORT:74
for x, y being set
for O being non empty connected Poset
for R being finite array of O st [x,y] in inversions R holds
card (inversions (Swap (R,x,y))) < card ()
proof end;

definition
let O be non empty connected Poset;
let R be array of O;
mode arr_computation of R -> non empty array means :Def14: :: EXCHSORT:def 14
( it . (base- it) = R & ( for a being Ordinal st a in dom it holds
it . a is array of O ) & ( for a being Ordinal st a in dom it & succ a in dom it holds
ex R being array of O ex x, y being set st
( [x,y] in inversions R & it . a = R & it . (succ a) = Swap (R,x,y) ) ) );
existence
ex b1 being non empty array st
( b1 . (base- b1) = R & ( for a being Ordinal st a in dom b1 holds
b1 . a is array of O ) & ( for a being Ordinal st a in dom b1 & succ a in dom b1 holds
ex R being array of O ex x, y being set st
( [x,y] in inversions R & b1 . a = R & b1 . (succ a) = Swap (R,x,y) ) ) )
proof end;
end;

:: deftheorem Def14 defines arr_computation EXCHSORT:def 14 :
for O being non empty connected Poset
for R being array of O
for b3 being non empty array holds
( b3 is arr_computation of R iff ( b3 . (base- b3) = R & ( for a being Ordinal st a in dom b3 holds
b3 . a is array of O ) & ( for a being Ordinal st a in dom b3 & succ a in dom b3 holds
ex R being array of O ex x, y being set st
( [x,y] in inversions R & b3 . a = R & b3 . (succ a) = Swap (R,x,y) ) ) ) );

theorem Th75: :: EXCHSORT:75
for a being Ordinal
for O being non empty connected Poset
for R being array of O holds {[a,R]} is arr_computation of R
proof end;

registration
let O be non empty connected Poset;
let R be array of O;
let a be Ordinal;
existence
ex b1 being arr_computation of R st
( b1 is a -based & b1 is finite )
proof end;
end;

registration
let O be non empty connected Poset;
let R be array of O;
let C be arr_computation of R;
let x be set ;
coherence
( C . x is segmental & C . x is Function-like & C . x is Relation-like )
proof end;
end;

registration
let O be non empty connected Poset;
let R be array of O;
let C be arr_computation of R;
let x be set ;
cluster C . x -> the carrier of O -valued ;
coherence
C . x is the carrier of O -valued
proof end;
end;

registration
let O be non empty connected Poset;
let R be array of O;
let C be arr_computation of R;
coherence
( last C is segmental & last C is Relation-like & last C is Function-like )
;
end;

registration
let O be non empty connected Poset;
let R be array of O;
let C be arr_computation of R;
cluster last C -> the carrier of O -valued ;
coherence
last C is the carrier of O -valued
;
end;

definition
let O be non empty connected Poset;
let R be array of O;
let C be arr_computation of R;
attr C is complete means :: EXCHSORT:def 15
last C is ascending ;
end;

:: deftheorem defines complete EXCHSORT:def 15 :
for O being non empty connected Poset
for R being array of O
for C being arr_computation of R holds
( C is complete iff last C is ascending );

theorem Th76: :: EXCHSORT:76
for O being non empty connected Poset
for R being array of O
for C being 0 -based arr_computation of R st R is finite array of O holds
C is finite
proof end;

theorem :: EXCHSORT:77
for O being non empty connected Poset
for R being array of O
for C being 0 -based arr_computation of R st R is finite array of O & ( for a being Ordinal st inversions (C . a) <> {} holds
succ a in dom C ) holds
C is complete
proof end;

theorem Th78: :: EXCHSORT:78
for O being non empty connected Poset
for R being array of O
for C being finite arr_computation of R holds
( last C is permutation of R & ( for a being Ordinal st a in dom C holds
C . a is permutation of R ) )
proof end;

theorem Th79: :: EXCHSORT:79
for X being set
for A being finite 0 -based array of X st A <> {} holds
last A in X
proof end;

theorem Th80: :: EXCHSORT:80
for x being set holds last <%x%> = x
proof end;

theorem Th81: :: EXCHSORT:81
for x being set
for A being finite 0 -based array holds last (A ^ <%x%>) = x
proof end;

registration
let X be set ;
cluster -> X -valued for Element of X ^omega ;
coherence
for b1 being Element of X ^omega holds b1 is X -valued
by AFINSQ_1:42;
end;

scheme :: EXCHSORT:sch 2
A{ F1( set ) -> set , F2() -> non empty set , P1[ set , set ], F3() -> set } :
ex f being non empty finite 0 -based array ex k being Element of F2() st
( k = last f & F1(k) = {} & f . 0 = F3() & ( for a being Ordinal st succ a in dom f holds
ex x, y being Element of F2() st
( x = f . a & y = f . (succ a) & P1[x,y] ) ) )
provided
A1: F3() in F2() and
A2: F1(F3()) is finite and
A3: for x being Element of F2() st F1(x) <> {} holds
ex y being Element of F2() st
( P1[x,y] & F1(y) c< F1(x) )
proof end;

theorem Th82: :: EXCHSORT:82
for A being array
for B being permutation of A holds B in Funcs ((dom A),(rng A))
proof end;

registration
let A be real-valued array;
cluster -> real-valued for permutation of A;
coherence
for b1 being permutation of A holds b1 is real-valued
proof end;
end;

registration
let a be Ordinal;
let X be non empty set ;
cluster -> Sequence-like for Element of Funcs (a,X);
coherence
for b1 being Element of Funcs (a,X) holds b1 is Sequence-like
by FUNCT_2:92;
end;

registration
let X be set ;
let Y be real-membered non empty set ;
cluster -> real-valued for Element of Funcs (X,Y);
coherence
for b1 being Element of Funcs (X,Y) holds b1 is real-valued
;
end;

registration
let X be set ;
let A be array of X;
cluster -> X -valued for permutation of A;
coherence
for b1 being permutation of A holds b1 is X -valued
proof end;
end;

registration
let X, Z be set ;
let Y be Subset of Z;
cluster -> Z -valued for Element of Funcs (X,Y);
coherence
for b1 being Element of Funcs (X,Y) holds b1 is Z -valued
proof end;
end;

theorem :: EXCHSORT:83
for X, Y being set
for r being b1 -defined b2 -valued Relation holds r is Relation of X,Y
proof end;

theorem Th84: :: EXCHSORT:84
for a being finite Ordinal
for x being set holds
( not x in a or x = 0 or ex b being Ordinal st x = succ b )
proof end;

theorem Th85: :: EXCHSORT:85
for O being non empty connected Poset
for A being non empty finite 0 -based array of O ex C being 0 -based arr_computation of A st C is complete
proof end;

theorem Th86: :: EXCHSORT:86
for O being non empty connected Poset
for A being non empty finite 0 -based array of O ex B being permutation of A st B is ascending
proof end;

registration
let O be non empty connected Poset;
let A be finite 0 -based array of O;
existence
ex b1 being permutation of A st b1 is ascending
proof end;
end;