Comparison Based Identification of Sorting Algorithm for a Problem 1Satwinder Kaur, 2Harpal Singh & 3Prabhdeep Singh P.I.T
Total Page:16
File Type:pdf, Size:1020Kb
International Journal of Advanced Computational Engineering and Networking, ISSN (p): 2320-2106, Volume – 1, Issue – 1, Mar-2013 COMPARISON BASED IDENTIFICATION OF SORTING ALGORITHM 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 time complexity (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: - 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 bubble sort 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: 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 QUICKSORT (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.