Sorting Algorithms

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Sorting Algorithms

• A sorting algorithm is an algorithm that puts elements of a list in a certain order.

• Efficient sorting is important for optimizing the use of other algorithms (such
as search and merge algorithms) which require input data to be in sorted lists

Why Sorting?

 “When in doubt, sort” – one of the principles of algorithm design. Sorting used as a subroutine
in many of the algorithms:

 Searching in databases: we can do binary search on sorted data

 A large number of computer graphics and computational geometry problems

 Closest pair, element uniqueness, frequency distribution

 A large number of algorithms developed representing different algorithm design techniques.

 A lower bound for sorting W(n log n) is used to prove lower bounds of other problems

Sorting Algorithms so far

 Insertion sort, selection sort, bubble sort

 Worst-case running time Q(n2); in-place

 Merge sort

 Worst-case running time Q(n log n); but requires additional memory

The Idea of the insertion sort is similar to the Idea of sorting the Playing cards .

while some elements unsorted:

Using linear search, find the location in the sorted portion where the 1st element of the
unsorted portion should be inserted

 Move all the elements after the insertion location up one position to make space for the
new element

Way of working

Select – Compare – Shift - Insert


So,this method is composed of two phases, namely:

1. Selecting .

2. Compare – Shift – insert .

When we can use Insertion Sort ?


This method is effective when dealing with small numbers .

• Applications using insertion sort

• Mathematical applications : in the search for greater value, or the smallest value.

• In many other applications.

SELECTION SORT

Advantages

 Easy to write

 Can be done “in place”

 Can be done on linked lists too (keep a tail pointer).


Disadvantages

 It is about N2 even in the best case for comparisons.

 So the running time (over all inputs) is approximately O(N2).

 The primary disadvantage of selection sort is its poor efficiency when dealing with a
huge list of items.

BUBBLE SORT
“ A procedure for sorting a set of items that begins by sequencing the first and second items,
then second and third and so on, until the end of the set is reached. And repeat this process
until all items are correctly sequenced.”

Advantages

 Bubble sort is one of the easiest sort algorithm.

 It is easy to implement.

 Elements are swapped in place, not use extra array.

 It is stable and fast.

Disadvantages

 It does not deal well with a list containing a huge number of items.

 More than the number of comparisons.

 The code become complex for large amount of data.


Quicksort
 It is one of the fastest sorting techniques available.
 Like binary search, this method uses a recursive, divide and conquer strategy
 The basic idea is to separate a list of items into two parts, surrounding a distinguished
item called the pivot.
 At the end of the process, one part will contain items smaller than the pivot and the
other part will contain items larger than the pivot.
 The performance of the whole Quicksort algorithm depends on how well the pivot is
taken.

Partition / Divide

Since each element ultimately ends up in the correct position, the algorithm
correctly sorts. But how long does it take?.On this basis it is divided into
following three cases.
1. Best Case
2. Worst Case
3. Average Case
Best Case for Quick Sort
The best case for divide-and-conquer algorithms comes when we split the input as evenly as
possible. Thus in the best case, each sub problem is of size n/2. The partition step on each sub
problem is linear in its size. the total efficiency(time taken) in the best case is O(nlog2n).
Worst Case for Quicksort
Suppose instead our pivot element splits the array as unequally as possible. Thus instead of n/2
elements in the smaller half, we get zero, meaning that the pivot element is the biggest or
smallest element in the array.
The Average Case for Quicksort
Suppose we pick the pivot element at random in an array of n keys Half the time, the pivot
element will be from the centre half of the sorted array. Whenever the pivot element is from
positions n/4 to 3n/4, the larger remaining sub-array contains at most 3n/4 elements.

Merge Sort

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy