International Journal of Advanced Computational Engineering and Networking, ISSN (p): 2320-2106, Volume – 1, Issue – 1, Mar-2013 COMPARISON BASED IDENTIFICATION OF FOR A PROBLEM 1SATWINDER KAUR, 2HARPAL SINGH & 3PRABHDEEP SINGH P.I.T. Kapurthala,India Email: [email protected], [email protected],[email protected]

Abstract—Sorting is essential in today’ era of computing as for various operations and arises need for ordered data. Organized data is easy to access and operate upon. There are various sorting algorithms and each has its own merits. For a particular problem, is difficult to find an algorithm which has minimum (execution time) and minimum space complexity (memory used). So, time-space tradeoff plays an important role to reduce the computational time at the cost of increased memory use. A sorting algorithm cannot be used for every type of problem so depending upon the user inputs and requirements, optimal algorithm from the multiple sorting algorithms can be chosen as these are problem specific. This work analyzes the different sorting algorithms at the implementation level and provides a comparative study which will help in choosing the optimal sorting algorithm for a specific problem.

Keywords: Sorting, Algorithm Analysis, Complexity, Time-Space Tradeoff, Optimal Sorting Algorithm.

I. INTRODUCTION II.FUNDAMENTAL SORTING ALGORITHMS Sorting is one of the most important operations in A. : - Selection sort has the basic computer programming. Various sorting algorithms principle according to which it find out the smallest are available today, but not a single algorithm can be element in array with each pass and placed it in its stated best for multiple problems. Different type of proper place until the array being sorted completely problem has to be dealt with different algorithm [6]. which is optimal for it. A sorting algorithm may not 1. Algorithm: Selection sort have minimum time complexity and minimum space Description: - A is the array of n element to be sorted complexity for a problem, so we have the concept of .Min denote the smallest element.LOC is a location time-space tradeoff. Time-space tradeoff implies that variable we can minimize the time needed for the execution of STEP: 1 Iterate through Array of N element. an algorithm at the cost of memory space required for Repeat step 2 to 5 When K=1, 2, it. 3………………..N-1 STEP: 2 Set Min= A [K], LOC=K A. Calculating the Complexity of an Algorithm: We STEP: 3 Repeat for T=K+1, K+1…N need to find out the complexity of the algorithm to STEP: 4 IF Min>A [T] Update Min and LOC see how much efficient it is. We have defined the STEP: 5 Min=A [T], LOC=T complexities of each of the sorting algorithm using STEP: 6 Swaps (A [K], A [LOC]) Big Oh notation because it gives the longest running STEP: 7 EXIT. time for any input of size n and an upper bound on the running time for any input. Thus, it guarantees 2. Example:- that the algorithm will never take longer than this. A [25, 20, 10, 15, 2, 0, 3, 7]

B. Optimum Sorting Algorithm: From among the Pass A[ A[ A[ A[ A[ A[ A[ A[ different sorting algorithms, some are best for one 1] 2] 3] 4] 5] 6] 7] 8] type of problems and others are best for other type of K=1,LO 25 20 10 15 2 0 3 7 problems. It is difficult to say which sorting C=6 algorithm is optimal. However, we have compared K=2,LO 0 20 10 15 2 25 3 7 the different algorithms to describe the type of C=5 problem they are best for. This will help in choosing K=3,LO 0 2 10 15 20 25 3 7 C=7 the optimal sorting algorithm according to user K=4,LO 0 2 3 15 20 25 10 7 requirements. C=8 K=5,LO 0 2 3 7 20 25 10 15 C. Comparison of different sorting algorithms: C=7 Different sorting algorithms are compared by K=6,LO 0 2 3 7 10 25 20 15 analyzing the time complexity and space complexity C=8 using n input data. For better understanding and easy Sorted 0 2 3 7 10 15 20 25 comparison we have used the same example for So we see here that the selection sort has the basic sorting, using different algorithms. Here we discuss principle according to which it find out the smallest the advanced sorting techniques in detail with element in array with each pass and placed it in its algorithms and examples.. proper place until the array being sorted completely .It is very slow, and has its time complexity O (n2) for

Comparison Based Identification Of Sorting Algorithm For A Problem

67 International Journal of Advanced Computational Engineering and Networking, ISSN (p): 2320-2106, Volume – 1, Issue – 1, Mar-2013 both worst-case and best-case due to large number of 1. A [25]>A [20]…………………A [N-1] comparison it performed. In some cases it performs SWAP (PTR, PTR+1), Here in the 1st pass we need to better than as fewer swaps are required. make 8- 1=7 comparison to fix 25 for its proper place then we get following result: B. : Insertion sort is a simple sorting 20 10 15 2 0 3 7 algorithm that is relatively efficient for small lists and 25 it perform sorting by taking element from list one by 2. Now 25 is fixed to proper place from here we need one and insert them in their proper position into new make 7- 1=6 comparison for 2nd pass taking 20 as ordered list. PTR then we get following result by comparing 20 1. Algorithm: Insertion sort with each element. Description: - A is the Array of n element to be 10 15 2 0 3 7 20 sorted. Temp is the variable that keeps the record of 25 element. PTR is the Pointer. 3. From here 25 and 20 are fixed now we need to STEP: 1 Initialize the element make 6-1=5 comparison to for 3rd pass. As 10<15 Set A [0] = - ∞ No swapping so we take 15 as PTR initially and after STEP: 2 Repeat Step 3 to 5 when k=2, 3 ….N 3rd pass we got result as follow. STEP: 3 Set Temp= A[k] and PTR=K-1. 10 2 0 3 7 15 20 25 STEP: 4 Repeat While Temp < A [PTR]. 4. In the 4th pass 15, 20, 25 are positioned so we make a. Set A[PTR+1}=A[PTR] 5-1=4 comparison and we get following result. b. Set PTR=PTR-1 2 0 3 7 10 15 STEP: 5 Set A [PTR+1] = Temp. 20 25 STEP: 6 EXIT. 5. In the 5th pass we need make 4-1= 3 comparison, As 10, 15, 20, 25 are positioned, After this pass 2. Example:- we get following result. A :- 25 20 10 15 2 0 3 7 0 2 3 7 10 15 20 25 Data -∞ 25 20 10 15 2 0 3 7 Here we get sorted list. Bubble sort is approaches to 1st pass -∞ 20 25 10 15 2 0 3 7 the problem by making multiple passes, through the

2nd pass -∞ 10 20 25 15 2 0 3 7 algorithm.it performed sorting by comparing two consecutive values and swap them if they are out of 3rd pass -∞ 10 15 20 25 2 0 3 7 order. 4th pass -∞ 2 10 15 20 25 0 3 7 2 Bubble sort has time complexity O(n ) like selection 5th pass -∞ 0 2 10 15 20 25 3 7 and insertion sort but it is much slower than selection

6th pass -∞ 0 2 3 10 15 20 25 7 sort and insertion sort due to large number of comparison it performed [2]. 7th pass -∞ 0 2 3 7 10 15 20 2 5 D. Quick sort: - Quick sort is a divide and conquer Sorted -∞ 0 2 3 7 10 15 20 2 list 5 algorithm with two phases, a partition phase and a Insertion sort is a simple sorting algorithm that is sort phase. relatively efficient for small lists. It has time 1. Algorithm: Quick Sort complexity O (n2) like bubble sort and selection sort Description: - A is the Array of N element. P, R is but I will perform sorting faster than Bubble sort and the variable used for comparison Selection sort due to fewer comparison it QUICK_SORT (A, P, R) performed[1]. STEP: 1 P>R THEN STEP: 2 Q= PARTITION (A, P, R) C. Bubble Sort: - Bubble work by comparing each STEP: 3 (A, P, Q-1) item with next item to it and swap them if required.it STEP: 4 QUICKSORT (A, Q+1, R) is oldest and simplest sorting algorithm in use. STEP: 5 EXIT. 1. Algorithm:-Bubble sort Here is the sub-algorithm for partition Description: - A is the Array of N element.PTR PARTITION (A, P, R) denotes the pointer. STEP: 1 X=A [R] STEP: 1 Repeat step 2and 3 for K=1 to N-1 STEP: 2 I =P-1 STEP: 2 Set PTR=1 // initializing PTR STEP: 3 FOR J=P TO R-1 STEP: 3 Repeat When PTR<=N-K. STEP: 4 DO IF A [I] <=X a. IF A [PTR]>A [PTR+1] THEN STEP: 5 THEN I=I+1 SWAP (A [PTR], A [PTR+1]) STEP: 6 SWAP (A [I], A [J]) b. SET PTR=PTR+1 STEP: 7 SWAP (A [I+1], A [R]) STEP: 4 EXIT. STEP: 8 RETURN (I+1).

2. Example:- 2. Example:- A: - 25 20 10 15 2 0 3 A:- 25 20 10 15 2 0 7 3 7 Set pivot =7 partition the list into two

Comparison Based Identification Of Sorting Algorithm For A Problem

68 International Journal of Advanced Computational Engineering and Networking, ISSN (p): 2320-2106, Volume – 1, Issue – 1, Mar-2013 sub-list by placing all element which are greater than is slightly faster than heap sort for larger 7 on right list ,and smaller element in left side list. data set, but it require twice the memory of heap sort 2 0 3 25 20 10 15 because of second array it used. Due to its recursive 7 nature it does not suited for application run on machine with limited memory.it has time complexity Now set pivot=2 for left list and pivot=15 for right of O (n log n) [4]. list 0 2 3 7 10 15 20 25 III. ADVANCED SORTING ALGORITHMS A. : Library sort, also called the gapped Here we get sorted array. insertion sort is same as insertion sort but with gaps 0 2 3 7 10 15 20 25 in array to accelerate the subsequent insertions. Library Sort is a combination of , insertion Quick sort has complexity of O (n log n) [11]. Quick sort and . First of all, a bucket sort is sort is faster than common sorting algorithm, it is done by using some hash function and then a count is better than heap sort, merge sort etc. Quick sort could maintained for each bucket which counts the number be a best choice for sorting theoretically but of elements in the bucket. And finally the values are practically it is little too complex to implement on inserted using insertion sort. large data set. Although it is fast and efficient but if 1. Algorithm: library sort the list is already sorted it gives horrible result [5]. Description: A is the array of n elements to be sorted, E. Merge Sort: Merge Sort is a Divide and Conquer SIZE is the number of elements in array i.e. n, Bi Algorithm with two phase, Partition phase and Merge represents bucket formed using a hash function, C phase .Merge sort splits the list to be sorted into two [Bi] gives the count of elements in bucket Bi, POS is halves, and sort them recursively. the variable to find position in new Array B where 1. Algorithm:-Merge sort element is to be inserted. Description:- A is the Array of N element. N is the STEP: 1 Bucket sort the elements of array A using Number of element. a hash function such that each STEP: 1 IF N=1, THEN RETURN elements gets some value Bi. STEP: 2 Divide Array A [1………..N] into two STEP: 2 Maintain a count C [Bi] of elements in each halves bucket Bi for i =1,2,3,4….. 1. A[1……….N/2] STEP: 3 Initialize I=0 and take a new array B [SIZE] 2. A[((N/2)+1)……….N] STEP: 4 WHILE I<=SIZE-1 THEN calculate POS for element A STEP: 3 Sort two arrays A [1………..N/2] [I] from bucket Bi as And A [((N/2) + 1)…………N] POS=C [B1] + C [B2] + C [B3] +…+ C [Bn] recursively. +1 where n < i STEP: 4 Merge sorted arrays into single one STEP: 5 IF POS is EMPTY STEP: 5 EXIT. THEN insert A [I] at B [POS] ELSE POS=POS+1 and GOTO 2. Example:- STEP: 5 A:- 25 20 10 15 2 0 3 STEP: 6 INSERTION SORT (B) 7 STEP: 7 END. STEP: 1 Divide array into two halves. 25 20 10 15 2 0 3 7 2. Flowchart: library sort

25 20 10 15 2 0 3 7 Partitioning Partitioning 25 20 10 15 2 0 3 7

20 25 10 15 0 2 3 7 Merging Merging 10 15 20 25 0 2 3 7

3. Example: 0 2 3 7 10 15 20 25 A: 25 20 10 15 2 0 3 7

Comparison Based Identification Of Sorting Algorithm For A Problem

69 International Journal of Advanced Computational Engineering and Networking, ISSN (p): 2320-2106, Volume – 1, Issue – 1, Mar-2013

B1 : 0 2 3 C[B1 ]= 3 B2 : 7 THEN I=1 C[B2 ]= 1 B3 : 10 C[B3 ]= [incrementing J] 1 STEP: 3 END. B4 : 15 20 25 C[B4 ]= 3 SIZE=8 1. I=0, A[0]=25, I<=7(True) 2. Flowchart: gnome sort a. 25->B4 b. POS= C[B1 ]+ C[B2 ]+ C[B3 ] =3+1+1 = 5 c. B[5]=25 d. I=0+1 = 1 2. I=1, A[1]=20, I<=7(True) a. 20->B4 b. POS= C[B1 ]+ C[B2 ]+ C[B3 ] =3+1+1 = 5+1 (Because B [5] is not empty) =6 c. B[6]=20 d. I=1+1 = 2 and so on till we get the following 3. I=8, I<=7(False) 4. Insertion Sort on B [2 0 3 7 10 25 20 15] 5. 0 2 3 7 10 25 20 3. Example: 15 A: 25 20 10 15 2 0 3 7 6. 0 2 3 7 10 20 25 A [0, 1, . . . . .7 ] SIZE=8 15 1. I=1 and J=2, I<=SIZE-1(1<=7,True) 7. 0 2 3 7 10 15 20 a. A [0] <= A [1] i.e. 25 25<=20(False) So, here we can see that library sort reduces the time b. SWAP(25,20) to sort using insertion sort by providing an almost c. A: 20 25 10 15 sorted list. The gapped insertion sort or the library 2 0 3 sort accelerates the subsequent insertions in the array. 7 Thus, it is much faster than the basic sorting d. I=1-1 = 0 techniques. Library sort is much efficient for e. IF(I=0)(True) completely unordered list. And also for larger input THEN I=1 set library sort is better than other basic sorting 2. I=1 and J=2, I<=SIZE-1(1<=7,True) methods. a. A [0] <= A [1] i.e. 20<=25(True) b. I=J=2 B. Gnome Sort: Gnome Sort is similar to insertion J=J+1=2+1=3 sort except that moving an element to its proper place c. A: 20 25 10 15 is accomplished by a series of swaps as in bubble 2 0 3 sort. This is similar to the way we sort flower pots 7 placed in a line. Starting from the second we compare 3. I=2 and J=3, I<=SIZE-1(2<=7,True) each pot with its previous pot, if it is larger than step a. A [1] <= A [2] i.e. 25<=10(False) forward to next pot otherwise swap the pots and b. SWAP(25,10) move one step backward. c. A: 20 10 25 15 1. Algorithm: gnome sort 2 0 3 Description: A is the array of n elements to be sorted, 7 SIZE is the number of elements in array i.e. n, and d. I=2-1 = 1 TEMP is a temporary variable used for swapping. e. IF(I=0)(False) STEP: 1 Initialize I=1 and J=2 And so on till we get the following STEP: 2 WHILE (I! = SIZE-1) 4. I=3 and J=8, I<=SIZE-1(3<=7,True) IF (A [I-1] <= A [I]) a. A [2] <= A [3] i.e. 3<=7(True) THEN I=J and J++; b. I=J=8 [increment I and J] J=J+1=8+1=9 ELSE TEMP=A [I-1] and A [I-1] c. A: 0 2 3 7 = A [I] and A [I] = TEMP and I- - ; 10 15 20 IF (I=0) 25 5. I=8 and J=8, I<=SIZE-1(8<=7, False).

Comparison Based Identification Of Sorting Algorithm For A Problem

70 International Journal of Advanced Computational Engineering and Networking, ISSN (p): 2320-2106, Volume – 1, Issue – 1, Mar-2013 Here, we see that gnome sort is also like insertion sort a. IF(A[0]> S1.TOP) i.e. IF(25> -10) except that moving an element to its proper place is True accomplished by a series of swaps as in bubble sort. b. IF (A[0]> S2.TOP) i.e. IF(25>100) The gnome algorithm is not much efficient but quite False simple sort. Gnome algorithm is in no way better than c. PUSH A[0] i.e. 25 at S2.TOP+1 insertion sort and hence is hardly preferred. However, d. Therefore, S1: -100 this algorithm is used for small data input to avoid S2: 100 25 nested loops. e. I=I++ = 0+1 = 1

C. Stack Sort: In Stack Sort two stacks are used, one 2. I=1, I<=SIZE-1(1<=7)True of which arranges the elements in ascending order a. IF(A[1]> S1.TOP) i.e. IF(20> -10) and the second stack arranges the elements in True descending order. At the end both the stacks are b. IF (A[1]> S2.TOP) i.e. IF(20>25) combined to form the sorted list [3]. False 1. Algorithm: stack sort c. PUSH A[1] i.e. 20 at S2.TOP+1 Description: A is the array of n elements to be sorted, d. Therefore, S1: -100 SIZE is the number of elements in array i.e. n, S1 and S2: 100 25 20 S2 are two stacks., MIN is some integer less than any e. I=I++ = 1+1 = 2 element of array A, MAX is some integer greater than And so on till we get the following any element of array A, PUSH is insertion operation into TOP of stack, POP is deletion operation from 3. I=7, I<=SIZE-1(7<=7)True TOP of stack a. IF(A[7]> S1.TOP) i.e. IF(7> 2) STEP: 1 Initialize S1.TOP=MIN and True S2.TOP=MAX b. IF(A[7]> S2.TOP) i.e. IF(7> 3) STEP: 2 WHILE I<= SIZE-1 True IF (A [I]>S1.TOP) c. MOVE 3 from S2 to S1 IF (A i. S1: -10 0 0 [I]>S2.TOP) 2 3 MOVE elements ii. S2: 100 25 20 from S2 to S1 until A [I] < 15 10 S2.TOP iii. A[7] S1.TOP 10 7 And PUSH A [I] at f. I=I++ = 7+1 = 8 S1.TOP+1 and I++ STEP: 3 MOVE all elements from S2 to S1. 4. I=8, I<=SIZE-1(8<=7)False STEP: 4 END. a. MOVE all elements from S2 to S1 i. S1: -10 0 0 2. Flowchart: stack sort 2 3 7 10 15 20 25 ii. S2: 100

Stack Sort technique requires the use of two stacks, thus its memory requirement is more. Stack Sort consumes too much time for reverse ordered input list.

D. Deq Sort: Deq stands for double ended queues. In this, the element is first inserted at the tail of the deq. For the second element, two comparisons are made. First comparison is with the tail element, if the new element is greater than the tail element than the new 3. Example: element is appended to the queue, otherwise a second A: 25 20 10 15 2 0 3 7 comparison is made with the head element of the S1.TOP= -100 S2.TOP= 100 SIZE=8 queue and if found smaller new element is prepended 1. I=0, I<=SIZE-1(0<=7)True

Comparison Based Identification Of Sorting Algorithm For A Problem

71 International Journal of Advanced Computational Engineering and Networking, ISSN (p): 2320-2106, Volume – 1, Issue – 1, Mar-2013 to the deq. And the process repeats. Whenever the A: 25 20 10 15 2 0 3 7 element is found to be greater than the head element a SIZE=8 new sublist is created and comparisons are made on 1. I=1, J=1, S1.TAIL= 25 the new list. a. IF I < SIZE (1<8) True 1. Algorithm: deq_sort b. IF A[I] >S1.TAIL (20>25) False Description: A is the array of n elements to be sorted, c. IF S1.HEAD = EMPTY (True) SIZE is the number of elements in array i.e. n, B is d. S1.HEAD=20, I++ (i.e. I=2) the sorted array e. Therefore now S1 = 20 25 STEP: 1 Initialize I=1, J=1 and S.TAIL=A 2. I=2, J=1, S1.TAIL= 25 [0] a. IF I < SIZE (2<8) True STEP: 2 WHILE I < SIZE b. IF A[I] >S1.TAIL (10>25) False IF (A [I]>SJ.TAIL) c. IF S1.HEAD = EMPTY (False) THEN SJ.TAIL=A [I], I++ d. IF A[2] < S1.HEAD (10<20) (True) ELSE e. Prepend A[2] to S1.HEAD ,I++ (I=3) IF (SJ.HEAD=EMPTY) f. Therefore now S1 =10 20 25 THEN SJ.HEAD = A [I], I++ And so on till we get the following ELSE 3. I=7, J=3, S3.TAIL= 3 IF (A [I] < SJ.HEAD) a. IF I < SIZE (7<8) (True) THEN PREPEND A [I] to b. IF A[I] >S3.TAIL (7>3) (True) SJ.HEAD, I++ c. Append A[7] i.e. 7 to S3 , I++(I=8) ELSE d. Therefore now S1 =10 20 J++ and Create a 25 new Sub list SJ S2= 0 2 and 15 SJ.TAIL=A[I], S3= 3 7 I++ 4. I=8, J=3, S3.TAIL= 7 STEP: 3 IF I > SIZE a. IF I < SIZE (8<8) (False) THEN K= 0 5. S1 =10 20 25 WHILE (K < SIZE) S2= 0 2 15 THEN Select the S3= 3 7 minimum among the B [0] = 0 HEAD of all SJ, ADD it to 6. S1 =10 20 25 B [K] S2= 2 15 STEP: 4 END. S3= 3 7 B [1] = 2 2. Flowchart: deq_sort 7. S1 =10 20 25 S2= 15 S3= 3 7 B [2] = 3 And so on till we get the following 8. S1 =25 S2= S3= B [7] = 25 9. Therefore B = 0 2 3 7 10 15 20 25 DEQ or the double ended queue sorting technique is very efficient technique. Ordered insertion is easier than stack sort. There is no need to pop the elements instead a new sub list can be created. DEQ Sort is better for reverse ordered input list.

E. Heap Sort: - Heap sort is a improved version of straight selection sort .it work by building the heap out of data items and then removing the largest node from heap and placed it at the end of Array then again it reconstruct the heap, remove the largest node, placed it at end of Array, the process is continued 3. Example: until we get sorted list [9],[10].

Comparison Based Identification Of Sorting Algorithm For A Problem

72 International Journal of Advanced Computational Engineering and Networking, ISSN (p): 2320-2106, Volume – 1, Issue – 1, Mar-2013 1. Algorithm: Heap Sort Description: - A is the Array of n element to be 15 sorted. Size is the number of element in array. L is 10 7 used for Left element in heap.R is used for right element. 0 STEP: 1 Build Heap (A) 3 2 STEP: 2 for I =length (A) down to 2 STEP: 3 exchange A [1] and A[i] 6. Delete top node and placed at end of array STEP: 4 heapsize=heapsize-1 0 7 10 3 2 15 20 25 STEP: 5 Heapify(A,1) 7. Build Heap Build heap. 10 STEP: 1 heapsize=length (A) 0 STEP: 2 for I=floor (legth/2) down to 1 7 STEP: 3 heapify(A,I) Heapify(A,I) 3 2 STEP: 1 L=left (I), R=right (I) Sorted array: STEP: 2 If (L<=heapsize) and A [L]>A [I] 10 7 0 3 2 15 20 25 STEP: 3 Largest =Left, Else STEP: 4 Largest=I 8. Delete top node again STEP: 5 If(R<=heapsize) and A[R]>A[Largest] 2 7 0 3 10 15 20 25 STEP: 6 Largest=R STEP: 7 If (Largest! =I) 9. Build Heap again 7 STEP: 8 Exchange A [I] =A [Largest] 3 0 STEP: 9 Heapify(A, Largest) 2. Example:- 2 A: - 25 20 10 15 2 0 3 7 .Sorted array: 1. Building the heap from given array 7 3 0 2 10 15 20 25

25 10 10. Delete top node again 20 2 3 0 7 10 15 20 25

11. Build Heap Again… 15 0 3 2 3 7 HEAP 3 3 Sorting on Heap:-

25 20 10 15 2 0 3 7 Sorted array 2. Delete root and replace with last element. 3 2 0 7 10 15 20 25

7 20 10 15 2 0 3 25 12. Delete top node again 3. Build heap 20 0 2 3 7 10 15 20 25

10 0 15

0 3 2 7 2 Hence we left with this sorted array.

Heap sort is the slowest of the O(n log n) algorithms 4. Delete top element again but it does not require recursion or multiple array to 3 15 10 7 2 0 20 25 work , which make it perfect choice for sorting of 5. Build Heap Again extremely large data set over merge sort and quick sort algorithms. But heap sort is slower than merge Sorted array: sort and quick sort [11],[8]. 15 7 10 3 2 0 20 25

Comparison Based Identification Of Sorting Algorithm For A Problem

73 International Journal of Advanced Computational Engineering and Networking, ISSN (p): 2320-2106, Volume – 1, Issue – 1, Mar-2013 F. Shell Sort: Shell sort is named after its inventor D Array 2.5 2.1 1.1 1.5 0.2 0.3 6.1 .L shell. It performed sorting by comparing element Now mapping will be done as shown in table. which are at certain distance and insert them at their 0 1 2 3 4 5 6 proper place using insertion sort. The value of distance is half the number of input and is halved 0. 0. 1. 1. 2. 2. 6. 0.2 0.3 1.1 1.5 2.1 2.5 6.1 after 2 3 1 5 1 5 1 each pass [7]. This is the sorted array. 1. Algorithm: Shell Sort Proxmap uses technique similar to hashing to assign Description: - A is the Array of N element.Temp is an item to its correctly sorted positioned in a the variable to store the value temporarily. Size container. It sort with help of an address computation, Denote the size of Array. D denote gap between to A key is shifted in the first pass to the proximity of element. the final destination. It has worst case complexity O STEP: 1 D=Size (n2) and best –case complexity O(n). It is faster than STEP: 2 while (d>1) the comparison based algorithms. We can avoid STEP: 3 D = (D+1)/2 worst-case by using good map key. STEP: 4 for (i = 0,iA [i] THEN III.COMPARATIVE STUDY STEP: 5 Temp=A [i+D] Advanced sorting algorithms: STEP: 6 A [i+D] =A [i] S.No Sort Average Worst Advantage Drawbacks STEP: 7 A [i] =Temp . Name Case Case s Complexit Complexit STEP: 8 EXIT. y y

2. Example:-- 1. Proxma O(n) O(n2) Faster than Use extra p compariso memory A:- 25 20 10 15 2 0 3 n based for storing 7 algorithm key Now we will sort this array using shell sort Value A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] 2. Merge O(n log n) O(n log n) Well suited At least of D for large twice the 25 20 10 15 2 0 3 7 data set. memory requiremen D=3 15 2 0 3 7 10 25 20 ts than other sorts. D=2 0 2 7 3 15 10 25 20 D=1 0 2 3 7 10 15 20 25 3. Shell O(n log n) O(n log n) Efficient Complex for Algorithm. sorted 0 2 3 7 10 15 20 25 medium sized list. Shell sort is the most efficient of the O(n2)Class of 2 sorting algorithm. It is suitable for sorting list having 4. Library O(n log n) O(n ) Stable Requires compariso extra space less than 5,000 items .Although it is the efficient n and can for the algorithm but it is relatively slower than merge sort, be run as gaps. online heap sort , quick sort. It is very complex relative other algorithm. algorithm of O(n2) class.

5. Heap O (n lon O(nlogn) Well suited Less G. Proxmap Sort:- Proxmap is a sorting algorithm n) for large efficient. data set that work by partitioning the array of data items into and does sub-array . not use massive 1. Algorithm: Proxmap Sort recursion. 2 2 Description:- A is the Array of N element. 6. Gnome O(n ) O(n ) No nested Not much loops, tiny efficient. Step: 1 Define mapping, map a key to sub array of code size destination array A2 and stable. Step: 2 Bucket sorts all elements which have same 7. Stack O(n lon) O(n2) Best Requireme performan nt of two key. ce among stacks Step: 3 then, interchange the key the sorting hence more by memory. Step: 4 fine Grain sorting will be Done insertion algorithms in all three 2. Example:- cases. 8. Deqsort O(n lon) O(n lon n) Better for Managing A:- 2.5 2.1 1.1 1.5 0.2 reverse multiple 0.3 7.1 ordered deqs is not lists. easy. Key 0 1 2 3 4 5 6

Comparison Based Identification Of Sorting Algorithm For A Problem

74 International Journal of Advanced Computational Engineering and Networking, ISSN (p): 2320-2106, Volume – 1, Issue – 1, Mar-2013 CONCLUSION [3] R.A Ammar , 1989, “Stack Based Algorithms”, The Journal Of Systems and Software 9, Elsevier Science Publishing Co., In this paper, different advanced sorting algorithms pp- 225-239. have been studied. Each algorithm has been first [4] R. Bremananth, V. Radhika, S. Thenmonzhi, 2009, represented in the form of pseudo code which “Visualization Of Searching And Sorting Algorithms”, International Journal of Computer And Information describes the step by step execution and then it is also Technology, Vol. 3, Issue 3. graphically represented through flowchart and explained with the help of examples. [5] S. Khamitkar, P. Balachandra, S. Lokhande and N. Deshmukh, 2009, “The Folklore Of Sorting Algorithms”, International Journal Of Computer Science Issues, Vol. 4, Further, the advantages and disadvantages of sorting No. 2, pp- 1694-0814. algorithms are discussed and time complexity of the [6] P. Adhikari, 2007, “Review on Sorting Algorithms: A different algorithms are compared and tried to find Comparative Study On Two Sorting Algorithms”. out which sorting algorithm is good in which situation, since never a single algorithm can be stated [7] M. Ciura, 2001, “Best Increments For The Average Case Of Shell Sort”, Department of Computer Science, Silesian best for every kind of problem statement. The best Institute Of Technology , Akademicka 16, Poland, pp-106- sorting algorithm is the one which suits the user 117. problem statement and provides the best solution with [8] V.Sharma,S.SINGH andDr. K.S .Kahlon, minimum time and minimum space complexity 2008,’’Performance study of Improved Heap sort Algorithm (memory can be traded off against time). and Other Sorting Algorithms On Different Platforms”.IJCSNS International Journal Of Computer Science and Network Security, Vol.8 No.4. REFERENCES [9] J. Alnihoud and R.Mansi,2010 “ An Enhancement Of Major [1] A.D Mishra and D. Garg, 2008, ”Selection Of Best Sorting Sorting Algorithms”.The International Arab Journal of Algorithm”, International Journal Of Intelligent Information Information Technology,Vol.7 No.1. Processing, Vol. 2, Issue 2, pp-363-368. [10] J.Harkins, Tarek El-Ghazawi, Esam El-Araby and M.Haung, [2] D.T.V Rao and B. Ramesh, 2012, “Experimental Based 2005 “Performance and Analysis of Sorting Algorithms on Selection Of Best Sorting Algorithm”, International Journal the SRC6 Reconfigurable Computer”. Of Modern Engineering Research, Vol. 2, Issue 4, pp-2908- 2912.



Comparison Based Identification Of Sorting Algorithm For A Problem

75