Reviw of Sorting Algorihms

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

International Journal of Scientific Engineering and Research (IJSER)

www.ijser.in
ISSN (Online): 2347-3878
Volume 2 Issue 3, March 2014

Analysis and Review of Sorting Algorithms


Gaurav Kocher1, Nikita Agrawal2
1,2
BE 8th Semester, CSE, Shri Shankaracharya Institute of Professional Management & Technology
Raipur, Chhattisgarh, India

Abstract: One of the fundamental issues in computer science is ordering a list of items. Although there is a huge number of sorting
algorithms, sorting problem has attracted a great deal of research; because efficient sorting is important to optimize the use of other
algorithms. Sorting algorithms have been studied extensively since past three decades. Their uses are found in many applications
including real-time systems, operating systems, and discrete event simulations. In most cases, the efficiency of an application itself
depends on usage of a sorting algorithm. Lately, the usage of graphic cards for general purpose computing has again revisited sorting
algorithms. In this paper we extended our previous work regarding parallel sorting algorithms on GPU, and are presenting an analysis
of parallel and sequential bitonic, odd-even and rank-sort algorithms on different GPU and CPU architectures. Their performance for
various queue sizes is measured with respect to sorting time and rate and also the speed up of bitonic sort over odd-even sorting
algorithms is shown on different GPUs and CPU. The algorithms have been written to exploit task parallelism model as available on
multi-core GPUs using the OpenCL specification.

Keywords: Selection sort, bubble sort, insertion sort, quick sort, merge sort, number of swaps, time complexity

1. Introduction algorithm concepts, such as big O notation, divide and


conquer algorithms, data structures such as heaps and binary
A Sorting Algorithm is an algorithm that puts elements of a trees, randomized algorithms, best, worst and average case
list in a certain order. The most-used orders are numerical analysis, time-space tradeoffs, and upper and lower bounds.
order and lexicographical order. Efficient sorting is
important for optimizing the use of other algorithms (such as 2. Sorting Algorithms
search and merge algorithms) which require input data to be
in sorted lists; it is also often useful for canonicalizing data A. Selection Sort
and for producing human-readable output. More formally,
the output must satisfy two conditions: The algorithm works by selecting the smallest unsorted item
and then swapping it with the item in the next position to be
1. The output is in non-decreasing order (each element is no filled. The selection sort works as follows: you look through
smaller than the previous element according to the the entire array for the smallest element, once you find it you
desired total order); swap it (the smallest element) with the first element of the
2. The output is a permutation (reordering) of the input. array. Then you look for the smallest element in the
remaining array (an array without the first element) and
Further, the data is often taken to be in an array, which swap it with the second element. Then you look for the
allows random access, rather than a list, which only allows smallest element in the remaining array (an array without
sequential access, though often algorithms can be applied first and second elements) and swap it with the third
with suitable modification to either type of data. element, and so on. Here is an example,

Since the dawn of computing, the sorting problem has void selectionSort(int[] ar){
attracted a great deal of research, perhaps due to the for (int i = 0; i ‹ ar.length-1; i++)
complexity of solving it efficiently despite its simple, {
familiar statement. For example, bubble sort was analyzed as int min = i;
early as 1956. A fundamental limit of comparison sorting for (int j = i+1; j ‹ ar.length; j++)
algorithms is that they require linearithmic time – O(n log n) if (ar[j] ‹ ar[min]) min = j;
– in the worst case, though better performance is possible on int temp = ar[i];
real-world data (such as almost-sorted data), and algorithms ar[i] = ar[min];
not based on comparison, such as counting sort, can have ar[min] = temp;
better performance. Although many consider sorting a }}
solved problem – asymptotically optimal algorithms have Example.
been known since the mid-20th century – useful new 29, 64, 73, 34, 20,
algorithms are still being invented, with the now widely used 20, 64, 73, 34, 29,
Timsort dating to 2002, and the library sort being first 20, 29, 73, 34, 64
published in 2006. 20, 29, 34, 73, 64
20, 29, 34, 64, 73
Sorting algorithms are prevalent in introductory computer The worst-case runtime complexity is O(n2).
science classes, where the abundance of algorithms for the
problem provides a gentle introduction to a variety of core

Paper ID: J2013181 81 of 84


International Journal of Scientific Engineering and Research (IJSER)
www.ijser.in
ISSN (Online): 2347-3878
Volume 2 Issue 3, March 2014

B. Insertion Sort void BubbleSort(int a[])


{
Insertion sort iterates, consuming one input element each int i,j;
repetition, and growing a sorted output list. Each iteration,
insertion sort removes one element from the input data, finds for (i=MAXLENGTH; --i >=0;) {
the location it belongs within the sorted list, and inserts it swapped = 0;
there. It repeats until no input elements remain. for (j=0; j<i;j++) {
if (a[j]>a[j+1]) {
Sorting is typically done in-place, by iterating up the array, Swap[a[j],a[j+1]);
growing the sorted list behind it. At each array-position, it swapped=1;
checks the value there against the largest value in the sorted }
list (which happens to be next to it, in the previous array- }
position checked). If larger, it leaves the element in place if (!swapped) return;
and moves to the next. If smaller, it finds the correct position }
within the sorted list, shifts all the larger values up to make a }
space, and inserts into that correct position. The resulting
array after k iterations has the property where the first k + 1 D. Quick Sort
entries are sorted ("+1" because the first entry is skipped). In
each iteration the first remaining entry of the input is Quick sort is a comparison sort developed by Tony Hoare.
removed, and inserted into the result at the correct position. Also, like merge sort, it is a divide and conquer algorithm,
and just like merge sort, it uses recursion to sort the lists. It
Example. We color a sorted part in green, and an unsorted uses a pivot chosen by the programmer, and passes through
part in black. Here is an insertion sort step by step. We take the sorting list and on a certain condition, it sorts the data set.
an element from unsorted part and compare it with elements Quick sort algorithm can be depicted as follows:
in sorted part, moving from right to left.
QUICKSORT (A)
29, 20, 73, 34, 64 1: step←m;
29, 20, 73, 34, 64 2: while step > 0
20, 29, 73, 34, 64 3: for (i←0 to n with increment 1)
20, 29, 73, 34, 64 4: do temp←0;
20, 29, 34, 73, 64
5: do j←i;
20, 29, 34, 64, 73
6: for (k←j+step to n with increment step)
Let us compute the worst-time complexity of the insertion 7: do temp←A[k];
sort. In sorting the most expensive part is a comparison of 8: do j←k-step;
two elements. Surely that is a dominant factor in the running 9: while (j>=0 && A[j]>temp)
time. We will calculate the number of comparisons of an 10: do A[j+step]=A[j];
array of N elements: 11: do j←j-step;
12: do Array[j]+step←temp;
we need 0 comparisons to insert the first element 13: do step←step/2;
we need 1 comparison to insert the second element
we need 2 comparisons to insert the third element E. Merge Sort
...
we need (N-1) comparisons (at most) to insert the last Merge Sort is a Divide and Conquer algorithm. It divides
element input array in two halves, calls itself for the two halves and
Totally, then merges the two sorted halves. The merg() function is
1 + 2 + 3 + ... + (N-1) = O(n2) used for merging two halves. The merge (arr, l, m, r) is key
process that assumes that arr[l..m] and arr[m+1..r] are sorted
C. Bubble Sort and merges the two sorted sub-arrays into one. See following
C implementation for details.
In this task, the goal is to sort an array of elements using the
bubble sort algorithm. The elements must have a total order Complexity of Mergesort
and the index of the array can be of any discrete type. For Suppose T (n) is the number of comparisons needed to sort
languages where this is not possible, sort an array of an array of n elements by the Merge Sort algorithm. By
integers. The bubble sort is generally considered to be the splitting an array in two parts we reduced a problem to
simplest sorting algorithm. Because of its simplicity and sorting two parts but smaller sizes, namely n/2. Each part
ease of visualization, it is often taught in introductory can be sort in T(n/2). Finally, on the last step we perform n-1
computer science courses. Because of its abysmal O(n2) comparisons to merge these two parts in one. All together,
performance, it is not used often for large (or even medium- we have the following equation
sized) datasets.

Paper ID: J2013181 82 of 84


International Journal of Scientific Engineering and Research (IJSER)
www.ijser.in
ISSN (Online): 2347-3878
Volume 2 Issue 3, March 2014

T(n) = 2*T(n/2) + n - 1 explained by Knuth in The Art of Computer Programming,


this is the key to perform average case analysis of
The solution to this equation is beyond the scope of this algorithms. [1][3].
course. However I will give you a reasoning using a binary
tree. We visualize the merge sort dividing process as a tree. Bentley received a B.S. in mathematical sciences from
Stanford University in 1974, and M.S. and Ph.D in 1976
from the University of North Carolina at Chapel Hill; while a
student, he also held internships at the Xerox Palo Alto
Research Center and Stanford Linear Accelerator Center.
After receiving his Ph.D., he joined the faculty at Carnegie
Mellon University as an assistant professor of computer
science and mathematics. At CMU, his students included
Brian Reid, John Ousterhout, Jeff Eppinger, Joshua Bloch,
and James Gosling, and he was one of Charles Leiserson's
advisors. Later, Bentley moved to Bell Laboratories. [2][3].

Dr. Baecker is an expert in human-computer interaction


(“HCI”) and user interface (“UI”) design. His research
interests include work on electronic memory aids and other
cognitive prostheses; computer applications in education;
computer-supported cooperative learning, multimedia and
new media; software visualization; groupware and computer-
supported cooperative work; computer animation and
Figure1: Example of Merge Sort
interactive computer graphics; computer literacy and how
computers can help us work better and safer; and
F. Heap Sort
entrepreneurship and the management of small business as
well as the stimulation of innovation. Baecker is also
Heapsort is a comparison-based sorting algorithm. Heapsort
interested in the social implications of computing, especially
is part of the selection sort family; it improves on the basic
the issue of responsibility when humans and computers
selection sort by using a logarithmic-time priority queue
interact. [7].
rather than a linear-time search. Although somewhat slower
in practice on most machines than a well-implemented
quicksort, it has the advantage of a more favorable worst- 4. Conclusion
case O(n log n) runtime. Heapsort is an in-place algorithm,
but it is not a stable sort. It was invented by J. W. J. Williams This paper discusses comparison based sorting algorithms. It
in 1964. analyses the performance of these algorithms for the same
number of elements. It then concludes that selection sort
shows better performance than Quick sort but being the
simple structure selection sort is more preferred and widely
used. It is clear that both sorting techniques are not that
popular for the large arrays because as the arrays size
increases both timing is slower for integer and string, but
generally the study indicate that integer array have faster
CPU time than string arrays. Both have the upper bound
running time O (n2). Bubble Sort is a very simple algorithm
that is only suitable for small lists. There are lots of
alternative sorting algorithms that are more performant than
Bubble Sort. Bubble Sort is widely considered to be the least
Figure 2: Comparison of sorting algorithms performant of all the established sorting algorithms.

3. Literature Survey The sorting algorithm Mergesort produces a sorted sequence


by sorting its two halves and merging them. With a time
complexity of O(n log(n)) Mergesort is optimal. However,
Robert Sedgewick is the author of a well-known book series
selection sort has the property of minimizing the number of
Algorithms, published by Addison-Wesley. The first edition
swaps. In applications where the cost of swapping items is
of the book was published in 1983 and contained code in
high, selection sort very well may be the algorithm of choice.
Pascal. Subsequent editions used C, C++, Modula-3, and
Java. With Philippe Flajolet he wrote several books and
preprints which promoted analytic combinatorics, a 5. Future Scope
discipline which relies on the use of generating functions
and complex analysis in order to enumerate combinatorial An important key to algorithm design is to use sorting as a
structures, and to study their asymptotic properties. As basic building block, because once a set of items is sorted,

Paper ID: J2013181 83 of 84


International Journal of Scientific Engineering and Research (IJSER)
www.ijser.in
ISSN (Online): 2347-3878
Volume 2 Issue 3, March 2014

many other problems become easy. Consider the following


applications:

 Frequency distribution - Given a set of n items, which


element occurs the largest number of times in the set? If
the items are sorted, we can sweep from left to right and
count them, since all identical items will be lumped
together during sorting. To find out how often an arbitrary
element k occurs, start by looking up k using binary search
in a sorted array of keys. By walking to the left of this
point until the element is not k and then walking to the
right, we can find this count in time, where c is
the number of occurrences of k. The number of instances
of k can be found in time by using binary search to
look for the positions of both and , where is
arbitrarily small, and then taking the difference of these
positions.
 Selection - What is the kth largest item in the set? If the
keys are placed in sorted order in an array, the kth largest
can be found in constant time by simply looking at the kth
position of the array. In particular, the median element
appears in the (n/2)nd position in sorted order.

References
[1] Algorithms in Java, Parts 1-4, 3rd edition by Robert
Sedgewick. Addison Wesley, 2003.
[2] Programming Pearls by Jon Bentley. Addison Wesley,
1986.
[3] Quicksort is Optimal by Robert Sedgewick and Jon
Bentley, Knuthfest, Stanford University, January, 2002.
[4] Dual Pivot Quicksort: Code and Discussion.
[5] Bubble-sort with Hungarian ("Csángó") folk dance
YouTube video, created at Sapientia University, Tirgu
Mures (Marosvásárhely), Romania.
[6] Select-sort with Gypsy folk dance YouTube video,
created at Sapientia University, Tirgu Mures
(Marosvásárhely), Romania.
[7] Sorting Out Sorting, Ronald M. Baecker with the
assistance of David Sherman, 30 minute color sound
film, Dynamic Graphics Project, University of Toronto,
1981. Excerpted and reprinted in SIGGRAPH Video
Review 7, 1983. Distributed by Morgan Kaufmann,
Publishers.

Author Profile
Mr. Gaurav Kocher is perusing Bachelor of
Engineering (CSE) from Shri Shankaracharya Institute
of Professional Management & Technology, Raipur
and presently is in Final Year. His research interests
are Data Structure, Web Designing, Analysis and
design of algorithms.

Ms. Nikita Agrawal is perusing Bachelor of


Engineering (CSE) from Shri Shankaracharya Institute
of Professional Management & Technology, Raipur
and presently is in Final Year. Her research interests
are Data Structure, Web Designing, Analysis and
design of algorithms.

Paper ID: J2013181 84 of 84

You might also like