analysis on searching and sorting algorithms · a number of searching algorithms have been...
Post on 16-Oct-2020
11 Views
Preview:
TRANSCRIPT
ANALYSIS ON SEARCHING AND SORTING ALGORITHMS
Anjana Kamana, Melissa Mary Smith, Nikhitha Beecharaju,
Mr. Rajasekhar Sastry, Dr. B V Ramana Murthy and Mr. C Kishor Kumar Reddy.
Stanley College of Engineering and Technology for Women, Hyderabad
anjanakamana@gmail.com,@melissahessing19@gmail.com,nikhitha8222@gmail.com,
rajniras@gmail.com,drbvrm@stanley.edu.in,ckishorkumar@stanley.edu.in
ABSTRACT
In this survey paper, the authors present various types of sorting and searching algorithms. Sorting and
Searching are two basic operations in computer science. Sorting techniques are analyzed based on time
complexity and space complexity. Searching Algorithms are designed to check for element or retrieve an
element from any data structure where it is stored. Searching algorithms are based on two categories
sequential search and interval search. Binary search works on sorted set of elements only. And is also
found out that it is suitable for midsized data. Linear search sequentially checks each element of the list
until it finds an element that matches the required value. Hash search is best for large data items and
exponential search can be used for infinite set of elements. This paper provides a analysis on searching
and sorting algorithms.
Keywords: Searching, Sorting, Algorithm, Time and Space complexity
1. INTRODUCTION
An algorithm is a process or set of rules to be followed in calculations or other problem-solving
operations, especially by computer. Sorting means arranging of data in given order such that increment or
decrement. Searching means finding out the location or finding out an element of a given item in a
collection of item. Many data structures are used to store information but Arrays, linked lists and tree are
fundamental data structures used to sorting and searching operation. Searching element is any type of a
numerical data, alphabet, and character data. A number of searching algorithms have been developed like
that sequential search, binary search and hashing etc. Every searching algorithm depends on the specific
problem, property of data and algorithm complexity. If the data is kept in some sorted order then
searching becomes easy. Implementation of sorting algorithm can be simply classified into internal and
external sorting. Internal sorting, where values are compared and rearranged within the system, may be
efficient in case of small data sets. However, when the records to be sorted are too large to be processed
by the system internally, external sorting algorithms are implemented. This method, also known as file
sorting, increases the parallelism of the process, since data is sorted in other nodes and finally merged at
the local node.
1.1. TIME COMPLEXITY
JASC: Journal of Applied Science and Computations
Volume VI, Issue I, January/2019
ISSN NO: 1076-5131
Page No:139
Time complexity makes it easy to estimate the run time of a program. Performing an accurate
calculation in a program’s operation time is a very labor-intensive process (it depends on the compiler and
the type of computer or speed of the processor).Therefore, it will not make an accurate measurement, just
a measurement of a certain order of magnitude. It can be viewed as the maximum number of primitive
operations that a program may execute. Regular operations are single additions, multiplications,
assignments etc. It leave some operations uncounted and concentrate on those that are performed the
largest number of times. Such operations are called as dominant. The number of dominant operations
depends upon the specific input data. An algorithm's performance time varies with different inputs of the
same size, one commonly uses the worst-case time complexity of an algorithm, denoted as T(n), which is
defined as the maximum amount of time taken on an input of size (n). Less common and the usually
specified explicitly is the measure of average case complexity. Time complexities are classified according
to the nature of the function T(n). For instance an algorithm with T(n) = O (n) is called a linear time
algorithm, and an algorithm with log T(n) =Ω(nα) for some constant α ≥1 is said to be an exponential
time algorithm.
1.2. SPACE COMPLEXITY
Space complexity is the amount of memory which is used by the algorithm (including the input
values to the algorithm) to execute and produce the result.To calculate the space complexity, it should
know the memory required to store different data type value. Sometimes Auxiliary Space is confused with
Space Complexity. But Auxiliary Space is the extra space or the temporary space used by the algorithm
during its execution.
Space Complexity = Auxiliary Space + Input space (1)
In Space Complexity of any algorithm, it usually considers only Data Space and it neglects the
Instruction Space and Environmental Stack. Big O particularly describes the worst-case scenario, and can
be used to describe the execution time required or the space used (e.g. in memory or on disk) by an
algorithm. Space complexity of O (1) means that the space required by an algorithm to process data is
constant, it does not grow with the size of the data on which the algorithm is operating. A space
complexity of O (N) describes an algorithm whose performance will grow linearly and in direct
proportion to the size of the input data set.
2. LITERATURE SURVEY
Thomas Niemann in his research work on sorting and searching algorithms stated about internal
and external sort, compared different sorting and searching algorithms on basics of time, space and
simplicity [1]. Brad Miller and David Ranumanalysed different sorting and searching techniques.[2].
Debadrita Roy and Arnab Kundu in their review summarized three searching algorithmsand also included
searching concept, algorithm, coding & time complexity [3]. Ajay Kumar, Bharat Kumar, Chirag Dawar
and Dinesh Bajaj explained about different sorting techniques their behavior for different inputs. The
research reveals that the Insertion sort is best for small data items and, Merge sort and quick sort is used
for large data sets [4]. Amy CsizmarDalal gave a brief description on sorting and searching techniques.
He explained about different searching and sorting techniques, algorithms and implementation of the
techniques with examples [5]. The website describes about Selection sort and binary search. It provides
JASC: Journal of Applied Science and Computations
Volume VI, Issue I, January/2019
ISSN NO: 1076-5131
Page No:140
complexity analysis of binary search and selection sort. It gives the detailed working procedure of binary
search and selection sort algorithms [6]. Sorting is a process of rearrangement a list of elements to the
correct order since handling the elements in a certain order more efficient than handling randomize
elements [7]. FahriyeGemciFurat explained and compared insertion and selection sort. Implementation
and running time of them is also explained in a detailed manner. He concluded that based on running time
selection sort is efficient [8]. The website provides the basic information about algorithms. It gives the
detailed information on different sorting and searching techniques. It provides the working procedure,
time complexity and pseudocode of searching and sorting algorithms [9]. Ramesh Chand Pandey explains
about the various sorting algorithms, their advantages and disadvantages. It compared sorting algorithms
based on time complexity, memory required and stability. It proposed a new sorting algorithm which
works on priority basis. The new algorithm gives the specific data first then general data if required [10].
They compared two-way linear search algorithm with linear search and provided the results with the help
of algorithm and comparison table. They concluded that in Linear Search it is not compulsory to arrange
an array in any order (Ascending or Descending) as in the case of binary search orderly [11].Manpreet
Singh Bajwa, Arun Prakash Agarwal and SumatiManchanda presented new algorithm for searching that is
Ternary search while comparing it with Binary search algorithm. The time complexity of ternary search is
reduced when compared to binary search. Hence, they concluded that it is efficient [12]. M. Goodrich and
R. Tamassia discussed that some sorting algorithms are stable by its nature such as insertion sort, merge
sort, bubble sort, while some sorting algorithms are not, such as quick sort, any given sorting algorithm
which is not stable can be modified to be stable [13]. Selection sorts the simplest of sorting techniques.
It's work very well for small files, also It's has a quite important application because each item is actually
moved at most once [14]. The third factor is memory space, algorithm that used recursive techniques need
more copies of sorting data that affect to memory space [15]. V.P.Kulalvaimozhi, M.Muthulakshmi,
R.Mariselvi, G.Santhana Devi, C.Rajalakshmi and C. Durai’s research dealt with most commonly used
internal sorting algorithms and their performance. It compared various sorting algorithms and found out
asymptotic complexity of each sorting algorithm. The study proposed a methodology for the users to
select an efficient sorting algorithm [19].
3. SEARCHING ALGORITHM
The searching technique can be classified into two categories:
Internal Searches: - If a file or table is kept in main memory this type of searching is called
internal search.
External Searches: - If a file or table is kept in auxiliary memory (hard disk, floppy, tape etc) this
type of searching is called External Search.
3.1. SEQUENTIAL SEARCH OR LINEAR SEARCH
The simplest form of searching technique is a sequence search or linear search. This search is
applicable to small size of arrays or linked list data structure. It finds out the searching element in
sequential manner on unordered list or ordered list. In this method searching process is started at the
beginning to end, it scans the elements one by one of the list from left to right until the searching
record/element is found. If we have taken an ordered list or table then the time given to fast searching and
JASC: Journal of Applied Science and Computations
Volume VI, Issue I, January/2019
ISSN NO: 1076-5131
Page No:141
good efficiency as a comparison on unordered list.
Unordered Linear Search: Suppose that the given array was not sorted. This might correspond, for
example, to collection exams which have not yet been sorted alphabetically.
Ordered Linear Search: Now suppose that the given array is in a sorted order. In this case, we
need not necessarily search through the entire list to find a particular object or determine that it does not
exist in the collection
Worst Case: Θ(n)Θ(n), search key is not present or is the last element, Best Case: Θ(1)Θ(1) ,the first
element, No. of comparisons: Θ(n)Θ(n) in worst case and 11 in the best case.
Algorithm: LINEAR _SEARCH (A, N, M, P)
1. Initialize Q=0
2. For N=0 to length [A] //scan the element from start to end
3. If (MP = =A [N]) //check on searching element
4. Q=Q+1 // increment on location variable
5. Return A[N] //print on finding element
6. END If
7. END For LOOP
8. If Q=0 then return “did not find the element”
9. Exit
For Example: An unordered list 33 22 55 11 33 44 sort it using linear search
Unordered
Array A[1-6]
A[0] A[1] A[2] A[3] A[4] A[5] Output
33 22 55 11 33 44
Step:1 MP=33 33 22 55 11 33 44 Return A[0],Q=1
Step:2 MP=33 33 22 55 11 33 44
Step:3 MP=33 33 22 55 11 33 44
Step:4 MP=33 33 22 55 11 33 44
Step:5 MP=33 33 22 55 11 33 44 Return a[1],Q=2
Step:6 MP=33 33 22 55 11 33 44
Table 3.1: Linear Search
3.2. BINARY SEARCH
Another simplest form of searching technique and the best efficient method is a binary search. It
can be used if the table is stored in an array and mid size of array is taken. If we use linked list and
insert/delete on data in linked list during searching time then problem are generated. This algorithm is
also based on Divide-and-Conquer algorithm.
JASC: Journal of Applied Science and Computations
Volume VI, Issue I, January/2019
ISSN NO: 1076-5131
Page No:142
Worst case/Average case: Θ(logn)Θ(logn), Best Case : Θ(1)Θ(1) ; when the key is the middle element ,
No. of comparisons : Θ(logn)Θ(logn) in the worst/average case & 11 in the best case.
Divide: The sorted list is divided into two parts.
Conquer: We first compare the search/input element with the mid element of the list. If they do not
match then we check search element and mid element if search element is less than the mid element then
go to first part/left part otherwise go to second/right part.
Combine: We apply the divide and conquer part whenever our search element is not found. When our
search element is found then list is automatically combined and returned.
Algorithm-
1. Assign n-1 to max and 0 to min.
2. Compute mid as the mean of max and min.
3. If array[mid] is equal to the search element, then stop and return mid.
4. If the mid is less than the search element then assign mid+1 to min.
5. If mid is greater than search element then assign mid-1 to max.
6. Go back to step 2.
For Example:
Unordered Array
A[1-6]
A[0] A[1] A[2] A[3] A[4] A[5]
33 22 55 11 33 44
Sort to Array A
and result Array
A is
11 22 33 33 44 55 Operation
Step: 1 Low=0, HIGHT=5, MID=0+5/2=2, A[MID]=33, SE=33
Check S==A[MID] 11 22 33 33 44 55 Return A[MID],Q=1
Step2: LOW=MID+1, 2+1=3, HIGHT=5, MID=3+5/2=4, A[MID]=44, SE=33
Check S==A[MID] 11 22 33 33 44 55
Step3: LOW=3, HIGHT=MID-1,4-1=3 MID=(3+3)/2=3, A[MID]=33, SE=33
Check S==A[MID] 11 22 33 33 44 55 ReturnA[MID],Q=2
Table 3.2: Binary Search
JASC: Journal of Applied Science and Computations
Volume VI, Issue I, January/2019
ISSN NO: 1076-5131
Page No:143
4. SORTING ALGORITHM
The sorting technique is classified into types:
I. Internal Sort: In internal sorting, all the data to be sorted is stored in memory at all the times while
sorting is in progress.
There are 3 types of internal sorts.
i. SELECTION SORT: - Ex: Selection sort algorithm and Heap Sort algorithm.
ii. INSERTION SORT: - Ex: Insertion sort algorithm and Shell Sort algorithm.
iii. EXCHANGE SORT: - Ex: Bubble Sort Algorithm and Quick sort algorithm.
II. External Sort: In external sorting data is stored outside the memory (like on disk) and only loaded
into memory in small chunks. External sorting is usually applied in cases when data can't fit into memory
entirely. Different parts of data are sorted separately and merged together. For Example: - Merge Sort
4.1. BUBBLE SORT
The complexity of sorting algorithm is dependent upon the number of comparisons that are made.
Total comparisons in a Bubble sort is: n (n – 1) / 2 ≈ n 2 – n
Best case: O (n2), Average case: O (n2), Worst case: O (n2)
In the bubble sort method list is divided into two sub-lists sorted and unsorted. The smallest
element is bubbled from an unsorted sub-list. Compare adjacent elements and swap the smallest once with
the other item if they are in wrong order. This passing procedure is repeated until no swaps are required,
indicating that the list is sorted [15] [18]. The bubble sort was originally written to bubble up the highest
element in the given list. But there is no difference whether highest or lowest element is bubbled. This
method is easy to understand but it is time consuming. In this type there are two successive elements that
are compared and swapping is done. Thus, step-by-step the entire array elements are checked. When a list
of ‘n’ elements is given the bubble sort requires up to n-1 passes to sort the data.
Algorithm-
1. Compare each pair of adjacent elements from the beginning of an array and if they are in reverse
order then swap them.
2. If at least one swap is done, repeat step 1.
For Example:
5 3 8 4 6
Initial Unsorted array
5 3 8 4 6
JASC: Journal of Applied Science and Computations
Volume VI, Issue I, January/2019
ISSN NO: 1076-5131
Page No:144
3 5 8 4 6
3 5 8 4 6
3 5 4 8 6
3 5 4 6 8
1. Step 1 : Compare 1st and 2nd (swap)
2. Step 2 : Compare 2nd and 3rd (do not swap)
3. Step 3 : Compare 3rd and 4th (swap)
4. Step 4 : Compare 4th and 5th (swap)
5. Step 5 : Repeat step1-5 until no more swaps needed
Table 4.1: Bubble Sort
4.2. INSERTION SORT
Insertion sort cannot exchange elements. In insertion sort the element is inserted at an appropriate
place which is similar to card insertion. Insertion sort provides several advantages Simple
implementation and, Efficient for small data sets [20]. Here the list is divided into two parts the sorted
and the unsorted sub-lists. In each pass, the first element of the unsorted sub list is picked up and moved
into the sorted sub list by inserting it in a suitable position. Suppose we have ‘n’ elements, we need n-1
passes to sort the elements in order. Insertion sort works in this way:
Algorithm-
INSERTION_SORT (B)
1. FOR j ← 2 TO length[B]
2. DO key ← B[j]
3. Put B[j] into the sorted sequence B[1 . . j − 1]
4. i ← j − 1
5. WHILE i > 0 and B[i] > key
6. DO B[i +1] ← B[i]
7. i ← i − 1
8. B[i + 1] ← key
For Example-
Unsorted list elements are:9,7,6,15,17,5,10,11 sort it using insertion sort.
9 7 6 15 17 5 10 11
9 7 6 15 17 5 10 11
7 9 6 15 17 5 10 11
6 7 9 15 17 5 10 11
6 7 9 15 17 5 10 11
6 7 9 15 17 5 10 11
5 6 7 9 15 17 10 11
5 6 7 9 10 15 17 11
5 6 7 9 10 11 15 17
JASC: Journal of Applied Science and Computations
Volume VI, Issue I, January/2019
ISSN NO: 1076-5131
Page No:145
Table 4.2: Insertion Sort
4.3. SHELL SORT
The shell sort, sometimes is called the “diminishing increment sort,” it improves the insertion sort
by breaking the original list into many number of smaller sublists, each of which is sorted using an
insertion sort. The unique way to choose these sublists is the key to the shell sort. Instead of breaking the
list into sublists of adjacent items, the shell sort uses an increment 'i', sometimes it is called the gap, it
used to create a sublist by choosing all the items which are 'i' items apart.
Algorithm-
1. Initialize the value of p
2. Divide the given list into smaller sub-list of equal intervals of i
3. Sort the sub-lists using the insertion sort
4. Repeat until complete list is sorted
For Example- Given 17, 26,20,44,55,31,54,77,93 is an unsorted list. Sort using shell sort.
17 26 20 44 55 31 54 77 93
1 shift for 20
17 20 26 44 55 31 54 77 93
2 shifts for 31
17 20 26 31 44 55 54 77 93
1 shift for 54
17 20 26 31 44 54 55 77 93
Sorted list
Figure4.3: Shell Sort
4.4. SELECTION SORT
This is a sorting algorithm, specifically it is an in-place comparison sort. It has O (n2) time
complexity, making it inefficient on large lists. Selection sort has one advantage over other sort
techniques[16][17] It is simple and has high performance on complicated algorithms during some
situations specially when the secondary memory is limited. Assume that they have taken a list of n
elements. By applying the selection sort, the first element is compared with all the remaining (n-1)
elements. The smallest element of the list is placed at the first location. Again, the second element is
compared with the remaining (n-1) elements. At the time of comparison, the smaller element is swapped
with the larger element. Similarly, entire array is checked for the smallest element and then swapping is
JASC: Journal of Applied Science and Computations
Volume VI, Issue I, January/2019
ISSN NO: 1076-5131
Page No:146
done according to it. Here they need n-1 passes or iterations to complete the rearranging of data.
Algorithm-
1. Set MIN to the location 0
2. Search for the least element in the considered or given list.
3. Swap it with the value at the location MIN.
4. Increment MIN so that it points to the next element in the list.
5. Repeat the same until list is sorted.
Example-26, 54, 93,17,77,31,44,55,20 is an unsorted list, sort it using selection sort
26 54 93 17 77 31 44 55 20
93 is largest
26 54 20 17 77 31 44 55 93
77 is largest
26 54 20 17 55 31 44 77 93
55 is largest
26 54 20 17 44 31 55 77 93
54 is largest
26 31 20 17 44 54 55 77 93
44 is largest and stays in its place
26 31 20 17 44 54 55 77 93
31 is largest
26 17 20 31 44 54 55 77 93
26 is largest
20 17 26 31 44 54 55 77 93
20 is largest
17 20 26 31 44 54 55 77 93
The list is sorted
JASC: Journal of Applied Science and Computations
Volume VI, Issue I, January/2019
ISSN NO: 1076-5131
Page No:147
Table 4.5: Selection
4.6. MERGE SORT
The basic concept of merge sort is to divide the list into two smaller sublists of approximately
equal size. Repeat this procedure till only one element is left in the sublist. After this, various sorted
sublists are merged together to form the sorted parent list. This process goes on recursively until the
original sorted list is obtained.
Algorithm -
Merge sort is based on the divide-and-conquer algorithm. Its worst-case run time has a lower order of
growth than insertion sort. Since we are dealing with subproblems, they state that each sub problem are
sorted as a sub-array A[q .. r]. Initially, q = 1 and r = n, but these values change as they recurse through
sub-problems. To sort A[q .. r]:
1. Divide Step: If a given array A has zero or one element, simply return as it is already sorted.
Otherwise, split A[q .. r] into two sub-arrays A[q .. s] and A[s + 1 .. r], each containing
approximately half of the elements of A[q .. r]. That is, s is the halfway point of A[q .. r].
2. Conquer Step: Conquer by recursively sorting the two sub-arrays A [q ... s] and A[s + 1 ... r].
3. Combine Step: Combine the elements back to A[q .. r] by merging the two sorted sub-arrays A[q
.. s] and A[s + 1 .. r] into a sorted sequence. To accomplish this step, they will define a procedure
MERGE (A, q, s, r). The recursion stops when the sub-array has just one element, so that it is
trivially sorted.
To sort the entire sequence A[1 .. n], make the initial call to the procedure as MERGE-SORT (A,
1, n).
MERGE-SORT (A, q, r)
1. IF q < r // Check for base case
2. THEN s= FLOOR[(q + r)/2] // Divide step
3. MERGE (A, q, s) // Conquer step.
4. MERGE (A, s + 1, r) // Conquer step.
5. MERGE (A, q, s, r) // Conquer step.
For Example-
JASC: Journal of Applied Science and Computations
Volume VI, Issue I, January/2019
ISSN NO: 1076-5131
Page No:148
38, 27, 43,3,9,82,10 is an unsorted, sort the list using merge sort.
38 27 43 3 9 82 10
Figure 4.6: Merge Sort
5. CONCLUSION
In this survey paper various searching and sorting techniques have been discussed. Authors have
seen sorting techniques based on time and space complexity. Various examples have been used to explain
the searching and sorting techniques. They have seen how a binary search is used in a sorted list. They
have also seen how quick sort and insertion sort are productive for large items and small items
respectively. They have also compared the searching algorithm on the basis of various factors like
complexity, searching type, etc.
38 27 43 3 9 82 10
10 38 27 43 3 9 82
38 27 3 9 43 10 82
9 82 10 27 38 3 43
3 27 38 43 9 10 82
3 9 10 27 38 43 82
JASC: Journal of Applied Science and Computations
Volume VI, Issue I, January/2019
ISSN NO: 1076-5131
Page No:149
6. REFRENCES
[1 ] Thomas Niemann, “Sorting and Searching Algorithms.
[2 ] Brad Miller and David Ranum, Problem Solving with Algorithms and Data Structures,
2013.
[3 ] Debadrita Roy and Arnab Kundu, A Comparative Analysis of Three Different Types of
Searching Algorithms in Data Structure, International Journal of Advanced Research in
Computer and Communication Engineering, 2014.
[4 ] Ajay Kumar, Bharat Kumar, Chirag Dawar and Dinesh Bajaj, Comparison Among
Different Sorting Techniques, International Journal for Research In Applied Science And
Engineering Technology (IJRASET), 2014
[5 ] Amy CsizmarDalal, Searching and Sorting Algorithms, 2004.
[6 ] “Analysis of Binary Search algorithm And Selection Sort algorithm”,
http://www.cartagena99.com/recursos/alumnos/apuntes/Binarysearch_sorting.pdf
[7 ] P.Adhikari, Review on Sorting Algorithms, "A comparative study on two sorting
algorithm", Mississppi state university, 2007.
[8 ] FahriyeGemciFurat, A Comparative Study of Selection Sort and Insertion Sort
Algorithms, International Research Journal of Engineering and Technology, 2016.
[9 ] http://www.tutorialspoint.com/data_structures_algorithms/data_structures_algorithms_tut
orial.pdf
[10 ] Ramesh Chand Pandey, Study and Comparison of various Sorting Algorithms, 2008.
[11 ] Nitin Arora, GarimaBhasin and Neha Sharma,”Two way Linear Search Algorithm”,
International Journal of Computer Applications, 2014.
[12 ] Manpreet Singh Bajwa, Arun Prakash Agarwal and SumatiManchanda, “Ternary Search
Algorithm: Improvement of Binary Search”, International Conference on Computing for
Sustainable Global Development, 2015.
[13 ] M. Goodrich and R. Tamassia, Data Structures and Algorithms in Java,John wiley & sons
4th edition, 2010,pp.241-243.
[14 ] R. Sedgewick and K. Wayne, Algorithms,Pearson Education, 4th Edition, 2011,pp.248-
249.
[15 ] D.Knuth, The Art of Computer Programming,Addison-Wesley,Third Edition 1997,pp.
106– 110 .
[16 ] R. Sedgewick, Algorithms in C++, Addison–Wesley Longman,1998,pp 273–274.
[17 ] Levitin, Introduction to the Design & Analysis of Algorithms, Addison–Wesley
Longman, 2007, pp 98–100.
[18 ] http://www.techopedia.com/definition/3757/bubble-sort.
[19 ] V.P.Kulalvaimozhi, M.Muthulakshmi, R.Mariselvi, G.Santhana Devi, C.Rajalakshmi and
C. Durai, Performance Analysis of Sorting Algorithm, International Journal of Computer
Science and Mobile Computing, 2015.
[20 ] http://corewar.co.uk/assembly/insertion.htm.
JASC: Journal of Applied Science and Computations
Volume VI, Issue I, January/2019
ISSN NO: 1076-5131
Page No:150
top related