update
This commit is contained in:
parent
1f1ad49427
commit
6f050a029e
2496 changed files with 37609 additions and 3031 deletions
|
|
@ -0,0 +1,52 @@
|
|||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class PermutationSort
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int[] a={3,2,1,8,9,4,6};
|
||||
System.out.println("Unsorted: " + Arrays.toString(a));
|
||||
a=pSort(a);
|
||||
System.out.println("Sorted: " + Arrays.toString(a));
|
||||
}
|
||||
public static int[] pSort(int[] a)
|
||||
{
|
||||
List<int[]> list=new ArrayList<int[]>();
|
||||
permute(a,a.length,list);
|
||||
for(int[] x : list)
|
||||
if(isSorted(x))
|
||||
return x;
|
||||
return a;
|
||||
}
|
||||
private static void permute(int[] a, int n, List<int[]> list)
|
||||
{
|
||||
if (n == 1)
|
||||
{
|
||||
int[] b=new int[a.length];
|
||||
System.arraycopy(a, 0, b, 0, a.length);
|
||||
list.add(b);
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
swap(a, i, n-1);
|
||||
permute(a, n-1, list);
|
||||
swap(a, i, n-1);
|
||||
}
|
||||
}
|
||||
private static boolean isSorted(int[] a)
|
||||
{
|
||||
for(int i=1;i<a.length;i++)
|
||||
if(a[i-1]>a[i])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
private static void swap(int[] arr,int i, int j)
|
||||
{
|
||||
int temp=arr[i];
|
||||
arr[i]=arr[j];
|
||||
arr[j]=temp;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,84 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref symbols nobinary
|
||||
|
||||
import java.util.List
|
||||
import java.util.ArrayList
|
||||
|
||||
numeric digits 20
|
||||
|
||||
class RSortingPermutationsort public
|
||||
|
||||
properties private static
|
||||
iterations
|
||||
maxIterations
|
||||
|
||||
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
method permutationSort(vlist = List) public static returns List
|
||||
perm = RPermutationIterator(vlist)
|
||||
iterations = 0
|
||||
maxIterations = RPermutationIterator.factorial(vlist.size())
|
||||
loop while perm.hasNext()
|
||||
iterations = iterations + 1
|
||||
pl = List perm.next()
|
||||
if isSorted(pl) then leave
|
||||
else pl = null
|
||||
end
|
||||
return pl
|
||||
|
||||
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
method isSorted(ss = List) private static returns boolean
|
||||
status = isTrue
|
||||
loop ix = 1 while ix < ss.size()
|
||||
vleft = Rexx ss.get(ix - 1)
|
||||
vright = Rexx ss.get(ix)
|
||||
if vleft.datatype('N') & vright.datatype('N')
|
||||
then vtest = vleft > vright -- For numeric types we must use regular comparison.
|
||||
else vtest = vleft >> vright -- For non-numeric/mixed types we must do strict comparison.
|
||||
if vtest then do
|
||||
status = isFalse
|
||||
leave ix
|
||||
end
|
||||
end ix
|
||||
return status
|
||||
|
||||
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
method runSample(arg) private static
|
||||
placesList = -
|
||||
"UK London, US New York, US Boston, US Washington" -
|
||||
"UK Washington, US Birmingham, UK Birmingham, UK Boston"
|
||||
anotherList = 'Alpha, Beta, Gamma, Beta'
|
||||
reversed = '7, 6, 5, 4, 3, 2, 1'
|
||||
unsorted = '734, 3, 1, 24, 324, -1024, -666, -1, 0, 324, 99999999'
|
||||
lists = [makeList(placesList), makeList(anotherList), makeList(reversed), makeList(unsorted)]
|
||||
loop il = 0 while il < lists.length
|
||||
vlist = lists[il]
|
||||
say vlist
|
||||
runtime = System.nanoTime()
|
||||
rlist = permutationSort(vlist)
|
||||
runtime = System.nanoTime() - runtime
|
||||
if rlist \= null then say rlist
|
||||
else say 'sort failed'
|
||||
say 'This permutation sort of' vlist.size() 'elements took' iterations 'passes (of' maxIterations') to complete. \-'
|
||||
say 'Elapsed time:' (runtime / 10 ** 9)'s.'
|
||||
say
|
||||
end il
|
||||
return
|
||||
|
||||
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
method makeList(in) public static returns List
|
||||
lst = ArrayList()
|
||||
loop while in > ''
|
||||
parse in val ',' in
|
||||
lst.add(val.strip())
|
||||
end
|
||||
return lst
|
||||
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
method main(args = String[]) public static
|
||||
runSample(Rexx(args))
|
||||
return
|
||||
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
method isTrue() public static returns boolean
|
||||
return (1 == 1)
|
||||
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
method isFalse() public static returns boolean
|
||||
return (1 == 0)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
#lang racket
|
||||
(define (sort l)
|
||||
(for/first ([p (in-permutations l)] #:when (apply <= p)) p))
|
||||
(sort '(6 1 5 2 4 3)) ; => '(1 2 3 4 5 6)
|
||||
Loading…
Add table
Add a link
Reference in a new issue