You are on page 1of 13

Linear time

sorting algorithms
• Selection
• Bubble 
• Insertion
The reason that algorithm theory
 was coined
• Back in the days,  people tried to find an easier way to do their everyday stuff,
like cutting out the route length they traveled and turn of that more time they
had. This was the starting point of algorithm theory (not yet known) in early
ages. No one haven't been proved as the inventor of the theory.
• Algorithms have a long history and the word can be traced back to the 9th
century. At this time the Persian scientist, astronomer and mathematician
Abdullah Muhammad bin Musa al-Khwarizmi, often cited as “The father of
Algebra”, was indirect responsible for the creation of the term “Algorithm”.
• After that investigation, many scholar had contributed several solutions
for specific problem and added them in Algorithm Theory.
Algorithm is ...
• a set of well-defined instructions to solve a particular problem. It takes a set
of input and produces a desired output.
   Qualities of good algorithm:
• Input and output should be defined precisely.
• Each step in the algorithm should be clear and unambiguous.
• Algorithms should be most effective among many different ways to solve a
problem.
• An algorithm shouldn't include computer code. Instead, the algorithm should
be written in such a way that it can be used in different programming
languages.
Sorting Algorithms
• A Sorting Algorithm is used to rearrange a given array or list
elements according to a comparison operator on the elements. The
comparison operator is used to decide the new order of element in
the respective data structure.
Sorting Terminology

•   In-place algorithms
   An in-place algorithm transforms the input without using any extra memory. As the algorithm
executes, the input is usually overwritten by the output, and no additional space is needed for this
operation.
   Several sorting algorithms rearrange the input into sorted order in-place, such as insertion sort,
selection sort, quick sort, bubble sort, heap sort, etc. All these algorithms require a constant
amount of extra space for rearranging the elements in the input array.
• Out-of-place algorithms
    Unlike an in-place algorithm, the extra space used by an out-of-place algorithm
depends on the input size.
    The standard merge sort algorithm is an example of out-of-place algorithm as it
requires O(n) extra space for merging. The merging can be done in-place, but it
increases the time complexity of the sorting routine
External\Internal sorting
Stability 

• What are Internal and External Sorting?


When all data that needs to be sorted cannot be placed in-memory at a time,
the sorting is called external sorting. External Sorting is used for massive
amount of data. Merge Sort and its variations are typically used for external
sorting. Some external storage like hard-disk, CD is used for external sorting.
When all data is placed in-memory, then sorting is called internal sorting.

• Then stability?
   Stability
is mainly important when we have key value pairs with duplicate
keys possible (like people names as keys and their details as values). And we
wish to sort these objects by keys.
 sElection Sort
• The selection sort algorithm sorts an array by repeatedly finding
the minimum element (considering ascending order) from unsorted
part and putting it at the beginning. The algorithm maintains two
subarrays in a given array.
1) The subarray which is already sorted. 
2) Remaining subarray which is unsorted.
In every iteration of selection sort, the minimum element
(considering ascending order) from the unsorted subarray is picked
and moved to the sorted subarray. 
  
  Bubble Sort is the simplest sorting
algorithm that works
by  repeatedly  swapping the adjacent
Bubble Sort
elements if they are in the wrong order.
First Pass: 
Let’s take an example:
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here,
algorithm compares the first two
elements, and swaps since 5 > 1. 
( 1 5 4 2 8 ) –>  ( 1 4 5 2 8 ), Swap
since 5 > 4 
( 1 4 5 2 8 ) –>  ( 1 4 2 5 8 ), Swap
since 5 > 2 
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now,
since these elements are already in
order (8 > 5), algorithm does not Now, the array is already
swap them. sorted, but our
Second Pass: 
t algorithm does not know Third Pass: ​
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ) 
if it is completed. The ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 ) ​
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap
algorithm needs ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 ) ​
since 4 > 2 
one whole pass ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 ) ​
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 ) 
without any swap to ( 1 2 4 5 8 ) –> ( 1 2 4 5 8 ) 
( 1 2 4 5 8 ) –>  ( 1 2 4 5 8 ) 
know it is sorted.
Implementation
Insertion Sort
• Insertion sort is a simple sorting algorithm that works
similar to the way you sort playing cards in your hands.
The array is virtually split into a sorted and an unsorted
part. Values from the unsorted part are picked and
placed at the correct position in the sorted part.
Algorithm 
To sort an array of size n in ascending order: 
1: Iterate from arr[1] to arr[n] over the array. 
2: Compare the current element (key) to its predecessor. 
3: If the key element is smaller than its predecessor,
compare it to the elements before. Move the greater
elements one position up to make space for the swapped
element.
Example: 

You might also like