1 / 10

Sorting lists: 1. Slow Sort

Sorting lists: 1. Slow Sort. inorder ( []). inorder ([_]). inorder ([A,B|T]) :- A < B, inorder ([B|T]). % slow_sort (L, S): Sorts list L, returning S. % Keep permuting L until it becomes sorted. slowsort (L, S) :- permutation(L, S), inorder (S). permutation([ ], [ ]).

blue
Télécharger la présentation

Sorting lists: 1. Slow Sort

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sorting lists: 1. Slow Sort inorder( []). inorder([_]). inorder([A,B|T]) :- A < B, inorder([B|T]). COSC 2P93 : More examples % slow_sort(L, S): Sorts list L, returning S. % Keep permuting L until it becomes sorted. slowsort(L, S) :- permutation(L, S), inorder(S). permutation([ ], [ ]). permutation(L, [H|T]) :- append(V, [H|U], L), append(V, U, W), permutation(W, T).

  2. 2. Bubble sort COSC 2P93 : More examples bubblesort(L, S) :- append(X, [A,B|Y], L), B < A, append(X, [B,A|Y], L2), bubblesort(L2, S). bubblesort(L, L).

  3. 3. Insertion sort COSC 2P93 : More examples insort([ ], [ ]). insort([X|L], M) :- insort(L, N), insortx(X, N, M). insortx(X, [ ], [X]). insortx(X, [A|L], [A|M]) :- A < X, insortx(X, L, M). insortx(X, [A|L], [X,A|L]) :- A >= X.

  4. 4. Quick sort COSC 2P93 : More examples qsort([], []). qsort([H|T], S) :- split(H, T, A, B), qsort(A, AS), qsort(B, BS), append(AS, [H|BS], S).

  5. Quick sort (cont) COSC 2P93 : More examples % split(H, L, Less, Greater): % All items in L that are less in value than H are put in Less (in order). % The rest are put in Greater (in order). split(_, [], [], []). split(H, [A|X], [A|Y], Z) :- A < H, split(H, X, Y, Z). split(H, [A|X], Y, [A|Z]) :- A >= H, split(H, X, Y, Z).

  6. Lotto 6/49 simulator COSC 2P93 : More examples % Lotto 6/49 generator: generate 6 random numbers (no duplicates!) ?- use_module(library(random)). %  include random number library (Sicstus) lotto649(Numbers) :- make_intlist(1, 49, All), % see previous overheads! select_random(6, All, Numbers). % select_random(N, A, L): selects N numbers from A, putting them in L select_random(N, _, [ ]) :- N =< 0. select_random(N, All, [Number | Rest]) :- N > 0, size(All, Size), % see “count” from assignment generate_random_int(1, Size, R), remove_nth(R, All, Number, Leftover), M is N - 1, select_random(M, Leftover, Rest).

  7. Lotto 6/49 (cont) COSC 2P93 : More examples % generate random value between Low and High, incl. % random/1 returns random float between [0.0, 1.0). (ie. never 1.0) generate_random_int(Low, High, R) :- Range is High - Low + 1, random(X), % Sicstusbuiltin R is integer(X * Range) + Low. % remove_nth(R, All, Item, Rest) removes the Rth entry Item from All, % leaving Rest. R is between 1 and the size of All. remove_nth(1, [Item|Rest], Item, Rest). remove_nth(N, [X|All], Item, [X|Rest]) :- M is N - 1, remove_nth(M, All, Item, Rest).

  8. Another Lotto 6/49 program COSC 2P93 : More examples This version accepts an integer seed to scramble random numbers (could do this in previous program as well!) ?- use_module(library(random)). lotto649(Seed, Numbers) :- make_intlist(1, 49, All), setrand(Seed), random_permutation(All, [A,B,C,D,E,F| _ ]), % Sicstus built-in sort([A,B,C,D,E,F], Numbers).

  9. Integer puzzle solver COSC 2P93 : More examples • problem: given a list of integers between 1 and N, generate lists of unique integers from this list that add up to a given integer value % int_puzzle(H, N, L) solves the problem in L for integers ranging from 1 to H, adding to value N int_puzzle(High, Number, List) :- make_intlist(1, High, All), find_soln(Number, All, List). find_soln(0, _, [ ]). find_soln(Tot, All, [Num|List]) :- Tot > 0, remove(Num, All, Rest), NewTot is Tot-Num, find_soln(NewTot, Rest, List).

  10. int_puzzle(cont) COSC 2P93 : More examples % remove(N, L, R) removes item N from list L, resulting in R. % Will fail if N is not found in L. remove(N, [N|L], L). remove(N, [A|R], [A|L]) :- remove(N, R, L). • One problem with int_puzzle: returns permutations of solutions • Later, will modify to keep track of unique solutions, and filter duplicated ones

More Related