Vous êtes sur la page 1sur 19

A PROJECT REPORT ON

IMPLEMENTATION OF SORTING TECHNIQUES IN PROLOG

SUBMITTED BY: VIRAL RANA CE-86 & TUSHAR RATANPARA CE-87 D2 BATCH GUIDED BY: Mr. Dinesh Chatani

DHARMSINH DESAI UNIVERSITY NADIAD-387001

DHARMSINH DESAI UNIVERSITY NADIAD

This is to certify that the Project Work carried out in the subject of Artificial Intelligence and on the topic of IMPLEMENTATION OF SORTING TECHNIQUES is confide work of Mr. Viral Rana (CE-86) and Mr. Tushar Ratanpara(CE-87) of B.E. semester VIIth in the branch of Computer Engineering during the academic year 2009-2010. To the best of my knowledge and belief, the matter presented by them is original in nature and have not been copied.
Staff in Charge Date : Head of the Department Date :

ACKNOWLEDGEMENT
I would like to thank and acknowledge the members of individuals who have worked on this project. I would also like to express my heartfelt gratitude towards Mr. Dinesh Chhatani who have helped me to overcome the technical issues and provided me all the facilities without whose help the successful completion of this project would not have been possible. Last but not least, I would like to thank all my friends who helped and boosted me for the completion of the project.

PROJECT TEAM

Viral Rana Tushar Ratanpara

Index
1. Problem Definition 2. Explanation & Overview 3. Program Logic 3.1 Algorithms 3.2 Examples 5. 5. 6.

4. Coding 5. Sample Input/Output 6. References

13. 17. 19.

Program definition :

To implement sorting techniques BUBBLESORT , INSERTIONSORT , MERGESORT and QUICKSORT in Prolog.

Explanation & Overview :


Bubble Sort is that it is easy to understand and program. The basic idea underlying the bubble sort is to pass through the array sequentially several times. Each pass consists of comparing each element in the array with its successor (x[i] with x[i+1]) and interchanging the two elements if they are not in proper order. Here , N-1 passes are sufficient to sort an array of size N. Insertion Sort, in this method there is an array link of pointers, one for each of the original array elements. Initially link[i]=i+1 for 0 <= I < n-1 and link[n-1] = -1. Thus the array may bo thought of as a linear list pointed to by an external pointer first initialized to 0. To insert the kth element the limked list is traversed until the proper position for x[k] is found ,or until the end of the list is reached. At that point x[k] can be inserted into the list by merely adjusting the list pointers. Quick Sort, in this method let x be ab array, and n the number of elements in the number of elements in the array to be sorted. Choose an element a from a specific position within the array(e.g. a=x[0]). Suppose that the elements of x are partitioned so that a is placed into position j and the following conditions hold: 1.Each of the elements in position 0 through j-1 is less than or equal to a.

2.Each of the elements in position j+1 through n-1 is greater than or equal to a. If the foregoing process is repeated with subarraysx[0] through x[j-1] and x[j+1] through x[n-1] and any subarrays created by the process in successive iterations, the final result is a sorted array. Merge Sort, merging is the process of combining two or more sorted files into a third sorted file. Divide the array into n subarrays of size 1 and merge adjacent pairs of arrays. We then have approximately n/2 arrays of size 2. Repeat this process until there is only one array remaining of size n.

Program logic :
Algorithm BUBBLESORT(A,N) This algorithm sorts an array A with N elements where N=length(A). 1.Take Boolean variable Flag and initialize to True. 2.Repeat step 3 to 5 until flag is True. 3.Flag=False. 4.Repeat step 5 for i=0 to N-1. 5.If A[i] is greater then A[i+1] then Flag=True, Swap A[i] & A[i+1]. 6.Return.

Illustrating with an example, assume that the initial array be [3][5][4][9][2]

Elements in blue indicate comparisons. Elements in red indicate swaps. The green dash indicates return to position 0. [3][5][4][9][2] -- original [3][5][4][9][2] -- compare 3 to 5 [3][5][4][9][2] -- compare 5 to 4 [3][4][5][9][2] -- swap 4 and 5 [3][4][5][9][2] -- compare 5 to 9 [3][4][5][9][2] -- compare 9 to 2 [3][4][5][2][9] -- swap 2 and 9 [3][4][5][2][9] -- compare 3 to 4 [3][4][5][2][9] -- compare 4 to 5 [3][4][5][2][9] -- compare 5 to 2 [3][4][2][5][9] -- swap 2 and 5 [3][4][2][5][9] -- compare 5 to 9 [3][4][2][5][9] -- compare 3 to 4 [3][4][2][5][9] -- compare 4 to 2 [3][2][4][5][9] -- swap 2 and 4 [3][2][4][5][9] -- compare 4 to 5 [3][2][4][5][9] -- compare 5 to 9 [3][2][4][5][9] -- compare 3 to 2 [2][3][4][5][9] -- swap 2 and 3 [2][3][4][5][9] -- compare 3 to 4 [2][3][4][5][9] -- compare 4 to 5 [2][3][4][5][9] -- compare 5 to 9 [2][3][4][5][9] -- compare 2 to 3 [2][3][4][5][9] -- compare 3 to 4

[2][3][4][5][9] -- compare 4 to 5 [2][3][4][5][9] -- compare 5 to 9 [2][3][4][5][9] -- no changes (swaps) were made in the last run, so we are done! The time complexity of BUBBLESORT is O(n) in best case and in worst case it is O(n^2).

Algorithm INSERTIONSORT(A,N) This algorithm sorts an array A with N elements. 1. 2. 3. 4. A[0] can be taken to be as a sorted element. Repeat steps 3 to 5 for k = 2,3,..,N: Set TEMP = A[K] and PTR = K-1. Repeat while TEMP < A[PTR]: (a.) Set A[PTR+1] = A[PTR]. [Moves element forward.] (b.) Set PTR := PTR 1. [End of loop.] 5. Set A[PTR+1] = TEMP. [Inserts element in proper place.] [End of step 2 loop.] 6. Return Here, there is an inner loop which is controlled by the variable PTR, and there is an outer loop which uses K as an index. Illustrating with an example, assume that the initial array be 77,33,44,11,88,22,66,55

Here, the first element is 77. The first element can be taken as sorted since it is a single element( as per the algorithm ) Pass K=1 K=2 K=3 K=4 K=5 K=6 K=7 K=8 Sorted: A[0] 77 77 33 33 11 11 11 11 11 A[1] 33 33 77 44 33 33 22 22 22 A[2] 44 44 44 77 44 44 33 33 33 A[3] 11 11 11 11 77 77 44 44 44 A[4] 88 88 88 88 88 88 77 66 55 A[5] 22 22 22 22 22 22 88 77 66 A[6] 66 66 66 66 66 66 66 88 77 A[7] 55 55 55 55 55 55 55 55 88

Algorithm QUICKSORT(S) 1: Do step 2 until S becomes empty. 2: If |S| = 1 then return S else choose a S. 3: Split the sequence S into three subsequences S1,S2 and S3 such that elements of S1 < a, elements of S2 = a and elements of S3 > a. 4: Return (QUICKSORT(S1) U S2 U (S3)). Quicksort is also known as Partition Exchanged Sort. This is clear from the following explanation. Illustrating with an example, let the initial array be given as

25 57 48 37 12 92 86 33 and the first element is placed in its proper position, the resulting array is 12 25 57 48 37 92 86 33 At this point, 25 is in the proper position in the array (x[1]), each element below that position (12) is less than or equal to 25, and each element above that position (57,48,37,92,86 and 33) is greater than or equal to 25. Since 25 is in its final position the original problem has been decomposed into the problem of sorting the two subarrays. (12) and (57 48 37 92 86 33) Nothing need be done to sort the first of these subarrays; a file of one element is already sorted. To sort the second subarray the process is repeated and the subarray is further subdivided. The entire array may now be viewed as 12 25 (57 48 37 92 86 33) where parentheses enclose the subarrays that are yet to be sorted. Repeating the process on the subarray x[2] through x[7] yields 12 25 (48 37 33) 57 (92 86) and further repetitions yield 12 25 (33 37) 48 57 (92 86) 12 25 (33) 37 48 57 (92 86)

12 25 33 37 48 57 (92 86) 12 25 33 37 48 57 (86) 92 12 25 33 37 48 57 86 92 Note that the final array is sorted. The time complexity of QUICKSORT is O(n log2 n) in best case and in worst case it is O(n^2).

Algorithm MERGESORT(A) 1.Take Length L=Length(A). 2.If L<=1 then Return A. 3.Now take variable list Left , Right , Result. 4.Take variable Middle=L/2. 5.Repeat Step 6 for each x in A upto Middle. 6.Add x to Left. 7.Repeat Step 7 for each x in A after Middle. 8.Add X to Right. 9.Left=MERGESORT(Left). 10.Right=MERGESORT(Right). 11.If Left.Last_Item is greater then Right.First_Item Then Result=MERGESORT(Left,Right). Else Result=append(Left,Right). 12.Return Result.

Illustrating with an example, assume that the initial array be38,27,43,3,9,82,10.

The time complexity of MERGESORT is (n log n) in best as well as worst case.

Coding
BUBBLESORT : domains list=integer* predicates bubblesort1(list,integer,list) bubblesort(list,list) clauses bubblesort1(L1,1,L1):-!. bubblesort1(L1,C,L2):C>0, bubblesort(L1,X), CC=C-1, bubblesort1(X,CC,L2). bubblesort([A],[A]):-!. bubblesort([H1|[H2|L1]],[H1|L]):H1<H2, bubblesort([H2|L1],L),!. bubblesort([H1|[H2|L1]],[H2|L]):bubblesort([H1|L1],L).

INSERTIONSORT : domains int = integer list = int* predicates insertsort(list,list) insert(int,list,list) clauses insertsort([],[]). insertsort([X|Tail],Sorted) :insertsort(Tail, SortedTail), insert(X, SortedTail, Sorted). insert(X, [Y|Sorted], [Y|Sorted1]) :X > Y, !, insert (X, Sorted, Sorted1). insert(X, Sorted, [X|Sorted]). The time complexity for INSERTIONSORT is O( n2 ). ( worst case and average case both show almost same time complexities )

QUICKSORT : domains int = integer list = int* predicates quicksort(list,list) split(int,list,list,list) combine(list,list,list) clauses quicksort([],[]). quicksort([X|Tail], Sorted) :split(X, Tail, Small, Big), quicksort(Small, SortedSmall), quicksort(Big, SortedBig), combine(SortedSmall, [X|SortedBig], Sorted). split(X, [], [], []). split(X, [Y|Tail], [Y|Small], Big) :X > Y, !, split(X, Tail, Small, Big). split(X, [Y|Tail], Small, [Y|Big]) :split(X, Tail, Small, Big). combine([], L, L). combine([X|L1], L2, [X|L3]) :- combine(L1, L2, L3).

MERGESORT: domains list=integer* predicates mergesort(list,list) divide(list,list,list) my_merge(list,list,list) clauses mergesort([], []). mergesort([A], [A]). mergesort([A, B | Rest], S) :divide([A, B | Rest], L1, L2), mergesort(L1, S1), mergesort(L2, S2), my_merge(S1, S2, S). divide([], [], []). divide([A], [A], []). divide([A, B | R], [A | Ra], [B | Rb]) :divide(R, Ra, Rb). my_merge(A, [], A). my_merge([], B, B). my_merge([A | Ra], [B | Rb], [A | M]) :A <= B, my_merge(Ra, [B | Rb], M). my_merge([A | Ra], [B | Rb], [B | M]) :A > B, my_merge([A | Ra], Rb, M).

SAMPLE INPUT - OUTPUT: For BubbleSort:

For Insertionsort:

For Quicksort:

For MergeSort:

References :
1. Introduction To Turbo Prolog by Carl Townsend 2. Data Structures by Yedidyah Langsam, Moshe J. Augenstein Aaron M. Tenenbaum

Vous aimerez peut-être aussi