Report on Sorting

download Report on Sorting

of 19

Transcript of Report on Sorting

  • 8/6/2019 Report on Sorting

    1/19

    A PROJECT REPORT

    ON

    IMPLEMENTATION OF SORTINGTECHNIQUES IN PROLOG

    SUBMITTED BY:

    VIRAL RANA CE-86 &TUSHAR RATANPARA CE-87D2 BATCH

    GUIDED BY:Mr. Dinesh Chatani

    DHARMSINH DESAI UNIVERSITYNADIAD-387001

  • 8/6/2019 Report on Sorting

    2/19

    DHARMSINH DESAIUNIVERSITY

    NADIAD

    This is to certify that the Project Work carried out inthe subject of Artificial Intelligence and on the topic of IMPLEMENTATION OF SORTING TECHNIQUESis confide work of Mr. Viral Rana (CE-86) and Mr.Tushar Ratanpara(CE-87) of B.E. semester VII th in thebranch of Computer Engineering during the academicyear 2009-2010.

    To the best of my knowledge and belief, the matterpresented by them is original in nature and have notbeen copied.

    Staff in Charge Head of the DepartmentDate : Date :

  • 8/6/2019 Report on Sorting

    3/19

    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 towardsMr. Dinesh Chhatani who have helped me to overcome thetechnical issues and provided me all the facilities withoutwhose help the successful completion of this project wouldnot have been possible.

    Last but not least, I would like to thank all my friends whohelped and boosted me for the completion of the project.

    PROJECT TEAM

    Viral RanaTushar Ratanpara

  • 8/6/2019 Report on Sorting

    4/19

    Index

    1. Problem Definition 5.

    2. Explanation & Overview 5.

    3. Program Logic 6.3.1 Algorithms3.2 Examples

    4. Coding 13.

    5. Sample Input/Output 17.

    6. References 19 .

    Program definition :

  • 8/6/2019 Report on Sorting

    5/19

    To implement sorting techniques BUBBLESORT ,INSERTIONSORT , MERGESORT and QUICKSORT inProlog.

    Explanation & Overview :

    Bubble Sort is that it is easy to understand andprogram. The basic idea underlying the bubble sort is to passthrough the array sequentially several times. Each pass

    consists of comparing each element in the array with itssuccessor (x[i] with x[i+1]) and interchanging the two elementsif they are not in proper order. Here , N-1 passes are sufficientto 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

  • 8/6/2019 Report on Sorting

    6/19

    2.Each of the elements in position j+1 through n-1 isgreater than or equal to a.If the foregoing process is repeated with subarraysx[0] throughx[j-1] and x[j+1] through x[n-1] and any subarrays created bythe process in successive iterations, the final result is a sortedarray.

    Merge Sort, merging is the process of combining twoor more sorted files into a third sorted file. Divide the arrayinto n subarrays of size 1 and merge adjacent pairs of arrays.We then have approximately n/2 arrays of size 2. Repeat thisprocess 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 whereN=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]

  • 8/6/2019 Report on Sorting

    7/19

    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

  • 8/6/2019 Report on Sorting

    8/19

    [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 caseand in worst case it is O(n^2).

    Algorithm INSERTIONSORT(A,N)This algorithm sorts an array A with N elements.

    1. A[0] can be taken to be as a sorted element.2. Repeat steps 3 to 5 for k = 2,3,..,N:3. Set TEMP = A[K] and PTR = K-1.4. Repeat while TEMP < A[PTR]:

    (a.) Set A[PTR+1] = A[PTR]. [Moves elementforward.]

    (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 thevariable PTR, and there is an outer loop which uses K as anindex.

    Illustrating with an example, assume that the initial arraybe77,33,44,11,88,22,66,55

  • 8/6/2019 Report on Sorting

    9/19

    Here, the first element is 77.The first element can be taken as sorted since it is a singleelement( as per the algorithm )

    Pass A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7]K=1 77 33 44 11 88 22 66 55K=2 77 33 44 11 88 22 66 55K=3 33 77 44 11 88 22 66 55K=4 33 44 77 11 88 22 66 55K=5 11 33 44 77 88 22 66 55K=6 11 33 44 77 88 22 66 55

    K=7 11 22 33 44 77 88 66 55K=8 11 22 33 44 66 77 88 55Sorted: 11 22 33 44 55 66 77 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 S3such thatelements of S1 < a,elements of S2 = a andelements of S3 > a.4: Return (QUICKSORT(S1) U S2 U (S3)).

    Quicksort is also known as Partition Exchanged Sort. This isclear from the following explanation.

    Illustrating with an example, let the initial array be given as

  • 8/6/2019 Report on Sorting

    10/19

    25 57 48 37 12 92 86 33

    and the first element is placed in its proper position, theresulting 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 to25, and each element above that position (57,48,37,92,86 and33) is greater than or equal to 25. Since 25 is in its finalposition 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 fileof one element is already sorted. To sort the second subarraythe 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 besorted. Repeating the process on the subarray x[2] throughx[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)

  • 8/6/2019 Report on Sorting

    11/19

    12 25 33 37 48 57 (92 86)

    12 25 33 37 48 57 (86) 92

    12 25 33 37 48 57 86 92Note that the final array is sorted.

    The time complexity of QUICKSORT is O(n log 2 n) in bestcase and in worst case it is O(n^2).

    Algorithm MERGESORT(A)1.Take Length L=Length(A).2.If L

  • 8/6/2019 Report on Sorting

    12/19

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

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

  • 8/6/2019 Report on Sorting

    13/19

    Coding

    BUBBLESORT :

    domainslist=integer*

    predicatesbubblesort1(list,integer,list)bubblesort(list,list)

    clausesbubblesort1(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

  • 8/6/2019 Report on Sorting

    14/19

  • 8/6/2019 Report on Sorting

    15/19

    QUICKSORT :

    domainsint = integerlist = int*

    predicatesquicksort(list,list)split(int,list,list,list)combine(list,list,list)

    clausesquicksort([],[]).

    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).

  • 8/6/2019 Report on Sorting

    16/19

    MERGESORT:

    domainslist=integer*

    predicatesmergesort(list,list)divide(list,list,list)my_merge(list,list,list)

    clausesmergesort([], []).

    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([A | Ra], Rb, M).

  • 8/6/2019 Report on Sorting

    17/19

    SAMPLE INPUT - OUTPUT:

    For BubbleSort:

    For Insertionsort:

  • 8/6/2019 Report on Sorting

    18/19

    For Quicksort:

    For MergeSort:

  • 8/6/2019 Report on Sorting

    19/19

    References :

    1. Introduction To Turbo Prolog

    by Carl Townsend

    2. Data Structuresby Yedidyah Langsam,

    Moshe J. AugensteinAaron M. Tenenbaum