36
A Comparison Based Analysis of Four Different Types of Sorting Algorithms in Data Structures with Their Performances Nidhi Imran Simarjeet International Journal of Advanced Research in Computer Science and Software Engineering Feb 2013 Vol. 3, Issue 2 Aman Kumar Department of Computer Science & Engineering, IIT Kanpur E-mail: [email protected] 3 November, 2014

A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

A Comparison Based Analysis of Four Different Types ofSorting Algorithms in Data Structures with Their

PerformancesNidhi Imran Simarjeet

International Journal of Advanced Research in ComputerScience and Software Engineering

Feb 2013 Vol. 3, Issue 2

Aman Kumar

Department of Computer Science & Engineering, IIT KanpurE-mail: [email protected]

3 November, 2014

Page 2: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Contents

1 Introduction

2 Sorting AlgorithmsQuicksortHeapsortInsertion SortMergesort

3 Experiment and Result

4 Conclusion

Perfomance of different sorting problems 0

Page 3: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Introduction

Which Algorithm to use and when?

Quick Sort

Heap Sort

Insertion Sort

Merge Sort

Perfomance of different sorting problems 1

Page 4: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Introduction

Which Algorithm to use and when?

Quick Sort

Heap Sort

Insertion Sort

Merge Sort

Perfomance of different sorting problems 1

Page 5: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Introduction

Which Algorithm to use and when?

Quick Sort

Heap Sort

Insertion Sort

Merge Sort

Perfomance of different sorting problems 1

Page 6: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Introduction

Which Algorithm to use and when?

Quick Sort

Heap Sort

Insertion Sort

Merge Sort

Perfomance of different sorting problems 1

Page 7: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Introduction

Which Algorithm to use and when?

Quick Sort

Heap Sort

Insertion Sort

Merge Sort

Perfomance of different sorting problems 1

Page 8: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Quicksort

This sorting algorithm is based on Divide-and-Conquer paradigmthat is the problem of sorting a set is reduced to the problem ofsorting two smaller sets.

Perfomance of different sorting problems 2

Page 9: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Quicksort

This sorting algorithm is based on Divide-and-Conquer paradigmthat is the problem of sorting a set is reduced to the problem ofsorting two smaller sets.

Perfomance of different sorting problems 2

Page 10: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Quicksort

This sorting algorithm is based on Divide-and-Conquer paradigmthat is the problem of sorting a set is reduced to the problem ofsorting two smaller sets.

Perfomance of different sorting problems 2

Page 11: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Time Complexity of Quick Sort

T(N) = 2T(N/2) + cN

Best Case Analysis: O(nlogn)

Average Case Analysis: O(nlogn)

Worst Case Analysis: O(n2)

Perfomance of different sorting problems 3

Page 12: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Time Complexity of Quick Sort

T(N) = 2T(N/2) + cN

Best Case Analysis: O(nlogn)

Average Case Analysis: O(nlogn)

Worst Case Analysis: O(n2)

Perfomance of different sorting problems 3

Page 13: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Time Complexity of Quick Sort

T(N) = 2T(N/2) + cN

Best Case Analysis: O(nlogn)

Average Case Analysis: O(nlogn)

Worst Case Analysis: O(n2)

Perfomance of different sorting problems 3

Page 14: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Time Complexity of Quick Sort

T(N) = 2T(N/2) + cN

Best Case Analysis: O(nlogn)

Average Case Analysis: O(nlogn)

Worst Case Analysis: O(n2)

Perfomance of different sorting problems 3

Page 15: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Time Complexity of Quick Sort

T(N) = 2T(N/2) + cN

Best Case Analysis: O(nlogn)

Average Case Analysis: O(nlogn)

Worst Case Analysis: O(n2)

Perfomance of different sorting problems 3

Page 16: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Time Complexity of Quick Sort

T(N) = 2T(N/2) + cN

Best Case Analysis: O(nlogn)

Average Case Analysis: O(nlogn)

Worst Case Analysis: O(n2)

Perfomance of different sorting problems 3

Page 17: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Heapsort

The heap(binary) data structure is an array object that can beviewed as a complete binary tree as shown in figure:

Time Complexity: O(nlogn)

Perfomance of different sorting problems 4

Page 18: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Heapsort

The heap(binary) data structure is an array object that can beviewed as a complete binary tree as shown in figure:

Time Complexity: O(nlogn)

Perfomance of different sorting problems 4

Page 19: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Heapsort

The heap(binary) data structure is an array object that can beviewed as a complete binary tree as shown in figure:

Time Complexity: O(nlogn)

Perfomance of different sorting problems 4

Page 20: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Insertion Sort

This algorithm considers the elements one at a time, inserting eachin its suitable place among those already considered (keeping themsorted). Insertion sort is an example of an incremental algorithm.

It builds the sorted sequence one element at a time.

Best Case time Complexity: O(n)

Worst Case time Complexity: O(n2)

Perfomance of different sorting problems 5

Page 21: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Insertion Sort

This algorithm considers the elements one at a time, inserting eachin its suitable place among those already considered (keeping themsorted). Insertion sort is an example of an incremental algorithm.

It builds the sorted sequence one element at a time.

Best Case time Complexity: O(n)

Worst Case time Complexity: O(n2)

Perfomance of different sorting problems 5

Page 22: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Insertion Sort

This algorithm considers the elements one at a time, inserting eachin its suitable place among those already considered (keeping themsorted). Insertion sort is an example of an incremental algorithm.

It builds the sorted sequence one element at a time.

Best Case time Complexity: O(n)

Worst Case time Complexity: O(n2)

Perfomance of different sorting problems 5

Page 23: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Insertion Sort

This algorithm considers the elements one at a time, inserting eachin its suitable place among those already considered (keeping themsorted). Insertion sort is an example of an incremental algorithm.

It builds the sorted sequence one element at a time.

Best Case time Complexity: O(n)

Worst Case time Complexity: O(n2)

Perfomance of different sorting problems 5

Page 24: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Insertion Sort

This algorithm considers the elements one at a time, inserting eachin its suitable place among those already considered (keeping themsorted). Insertion sort is an example of an incremental algorithm.

It builds the sorted sequence one element at a time.

Best Case time Complexity: O(n)

Worst Case time Complexity: O(n2)

Perfomance of different sorting problems 5

Page 25: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Mergesort

This algorithm is also based on Divide-and-Conquer approach. Thegeneral idea is to imagine them split into two sets and each set is

individually sorted and the resulting sorted sequence are merged toproduce a single sorted sequence of n elements.

Time Complexity: O(nlogn)

Perfomance of different sorting problems 6

Page 26: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Mergesort

This algorithm is also based on Divide-and-Conquer approach. Thegeneral idea is to imagine them split into two sets and each set is

individually sorted and the resulting sorted sequence are merged toproduce a single sorted sequence of n elements.

Time Complexity: O(nlogn)

Perfomance of different sorting problems 6

Page 27: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Mergesort

This algorithm is also based on Divide-and-Conquer approach. Thegeneral idea is to imagine them split into two sets and each set is

individually sorted and the resulting sorted sequence are merged toproduce a single sorted sequence of n elements.

Time Complexity: O(nlogn)

Perfomance of different sorting problems 6

Page 28: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Mergesort

This algorithm is also based on Divide-and-Conquer approach. Thegeneral idea is to imagine them split into two sets and each set is

individually sorted and the resulting sorted sequence are merged toproduce a single sorted sequence of n elements.

Time Complexity: O(nlogn)Perfomance of different sorting problems 6

Page 29: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Experiment

In this experiment Turbo C++ 3.0 compiler is used in which thedata set contains random numbers. The initial range of data setstarts from 50 to 10000 elements with increment of 100 elementsand later the size of elements increased and reached to 30000 withthe interval of 1000 elements.

No. of clock ticks 10000 15000 20000 25000 300000

Quick sort Nil Nil Nil Nil Nil

Heap Sort Nil Nil Nil 3 3

Insertion Sort 1 3 5 7 10

Quick Sort Nil Nil - - -

Table : Shows the number of clock ticks taken by the four algorithms forsorting

Perfomance of different sorting problems 7

Page 30: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Experiment

In this experiment Turbo C++ 3.0 compiler is used in which thedata set contains random numbers. The initial range of data setstarts from 50 to 10000 elements with increment of 100 elementsand later the size of elements increased and reached to 30000 withthe interval of 1000 elements.

No. of clock ticks 10000 15000 20000 25000 300000

Quick sort Nil Nil Nil Nil Nil

Heap Sort Nil Nil Nil 3 3

Insertion Sort 1 3 5 7 10

Quick Sort Nil Nil - - -

Table : Shows the number of clock ticks taken by the four algorithms forsorting

Perfomance of different sorting problems 7

Page 31: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Experiment

In this experiment Turbo C++ 3.0 compiler is used in which thedata set contains random numbers. The initial range of data setstarts from 50 to 10000 elements with increment of 100 elementsand later the size of elements increased and reached to 30000 withthe interval of 1000 elements.

No. of clock ticks 10000 15000 20000 25000 300000

Quick sort Nil Nil Nil Nil Nil

Heap Sort Nil Nil Nil 3 3

Insertion Sort 1 3 5 7 10

Quick Sort Nil Nil - - -

Table : Shows the number of clock ticks taken by the four algorithms forsorting

Perfomance of different sorting problems 7

Page 32: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Experiment and Result

No. of clock ticks 10000 15000 20000 25000 300000

Quick sort Nil Nil Nil Nil Nil

Heap Sort Nil Nil Nil 0.164835 0.164835

Insertion Sort 0.054945 0.164835 0.274725 0.384615 0.549451

Quick Sort Nil Nil - - -

Table : Shows time taken(in seconds) by the four algorithms for sorting

Perfomance of different sorting problems 8

Page 33: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Experiment and Result

Quick Heap Insertion Merge

Time ComplexityBest O(nlogn) O(nlogn) O(n) O(nlogn)

Average O(nlogn) O(nlogn) O(n.n) O(nlogn)Worst O(n.n) O(nlogn) O(n.n) O(nlogn)

Space Complexity 1 1 1 N

Comparison based Yes Yes Yes Yes

Inplace Yes Yes Yes No

Type Internal Internal Internal Can be both Internal and external

Stable Yes Yes Yes No

Table : shows comparison of the three sorting techniques on variousparameters

Perfomance of different sorting problems 9

Page 34: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

Conclusion

From the above analysis it can be said that in a list of randomnumbers from 10000 to 30000, insertion sort takes more time tosort as compare to heap, quick and merge sorting techniques. If wetake worst case complexity of all the four sorting techniques theninsertion sort and quick sort technique gives the result of the orderof N square, but here if one needs to sort a list in this range thenquick sorting technique will be more helpful than the othertechniques.

Perfomance of different sorting problems 10

Page 35: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques

Introduction Sorting Algorithms Experiment and Result Conclusion

References I

Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest “Introduction toAlgorithms”

Ellis Horowitz, Sartaj Sahni, Sanguthevar Rajasekaran, “Computer Algorithms”

L M Wegner, J I Teuhola “The external Heapsort” 1988, 23, 317-333

P. Hennequin“Combinatorial analysis of Quick-sort algorithm” Alexandros Agapitosand Simon M. Lucas,“Evolving Efficient Recursive Sorting Algorithms”, 2006 IEEECongress on Evolutionary Computation Sheraton Vancouver Wall Centre Hotel,Vancouver, BC, Canada July 16- 21, 2006

Knuth D. (1997) “he Art of Computer Programming, Volume 3: Sorting andSearching”, Third Edition. Addison-Wesley, 1997. ISBN 0-201-89685-0. pp. 138141, ofSection 5.2.3: Sorting by Selection

Optimal stable merging by A. Symvonis. The Computer Journal, Vol. 38, No. 8

(1995). In-place and stable merge sort

Perfomance of different sorting problems 11

Page 36: A Comparison Based Analysis of Four Different …amanku/...sort as compare to heap, quick and merge sorting techniques. If we take worst case complexity of all the four sorting techniques