You are on page 1of 28

COLLEGE OF COMPUTIN AND INFORMATICS.

SCHOOL OF GRADUATE STUDY.

DEPARTMENT OF COMPUTER SCIENCE.

DESIGN AND ANALYSIS ALGORITHM

Presentation of Assignment I:
Title: Sorting Algorithm
Presented by: Nuredin Abdumalik ID:PGP/1056/14
Submitted to: Dr.Million Meshesha

06/17/2022
Content

• Overview
• Sorting algorithm
• Bubble sort
• Merge sort
• Complexity Analysis
• Conclusion

06/17/2022
Overview of Sorting Algorithms
• Given a collection of items we want to arrange them in an increasing
or decreasing order. „
• You probably have seen a different number of sorting algorithms
including: bubble sort, Selection Sort ,Insertion Sort . Merge
Sort ,Quick Sort
• „ In terms of efficiency
complexity of the Bubble sort is O(n2).
 average

average complexity of merge sort is O(n lg n).


• In this section,
 we discuss about bubble sort and merge sort and their analysis.

 discuss Advantages and disadvantages of bubble and merge sort.

06/17/2022
Sorting algorithm
• What are Sorting Algorithms?
• A sorting algorithm is used to reorder a group
of items into a specified order.
• This sort could be by alphabetic order or some
increasing or decreasing order.
• The main aim of using sorting algorithms is to
make the record easier to search, insert, and
delete.

06/17/2022
Cont…
• Why do we need sorting algorithms?
• A sorting algorithm will put items in a list into an
order, such as alphabetical or numerical order. For
example, a list of customer names could be sorted
into alphabetical order by surname, or a list of
people could be put into numerical order by age.
• Sorting a list of items can take a long time,
especially if it is a large list.
• A computer program can be created to do this,
making sorting a list of data much easier.
06/17/2022
Cont…
• Information growth rapidly in our world leads to
increase developing sort algorithms.
• Developing sort algorithms through improved
performance and decreasing complexity.
• We can classified sorting algorithms by:
 Computational complexity,
 Stability,
 Number of swaps,
 Usage of memory.

06/17/2022
Advantage & Disadvantages of Sort

Insertion Sort:
Advantages Disadvantages
The main advantage of the The disadvantage of the insertion sort is that it
insertion sort is its simplicity. does not perform as well as other, better sorting
algorithms
It also exhibits a good performance With n-squared steps required for every n
when dealing with a small list. element to be sorted, the insertion sort does not
deal well with a huge list.
The insertion sort is an in-place The insertion sort is particularly useful only
sorting algorithm so the space when sorting a list of few items.
requirement is minimal.

06/17/2022
Selection Sort:
Advantages Disadvantages
The main advantage of the selection The primary disadvantage of the
sort is that it performs well on a small selection sort is its poor efficiency when
list. dealing with a huge list of items.

Because it is an in-place sorting The selection sort requires n-squared


algorithm, no additional temporary number of steps for sorting n elements.
storage is required beyond what is
needed to hold the original list.

Its performance is easily influenced by Quick Sort is much more efficient


the initial ordering of the items before than selection sort
the sorting process.
06/17/2022
Quick Sort
Advantages Disadvantages
The quick sort is regarded as The slight disadvantage of
the best sorting algorithm. quick sort is that its worst-
case performance is similar to
average performances of the
bubble, insertion or selections
sorts.
If the list is already sorted than bubble sort is
It is able to deal well with a much more efficient than quick sort
huge list of items.
If the sorting element is integers than radix
Because it sorts in place, no sort is more efficient than quick sort.
additional storage is required
as well

06/17/2022
Bubble sort

• Bubble sort is a simple sorting algorithm that works


by repeatedly.
• It's comparing each pair of adjacent items and
swapping them if they are in the wrong order.
• Bubble sort has to perform a large number
comparison when there are more elements in the list
and it increases as the number of items increases that
need to be sorted.

06/17/2022
Bubble sort(cnt’d)
• why do we need bubble sort algorithm?
 its capability to detect a very small error
• It is an in-place sorting algorithm.
• Bubble sort uses multiple passes (scans) through an
array.
• In each pass, bubble sort places the next largest element
to its proper position.
• It uses no auxiliary data structures (extra space)
while sorting.
06/17/2022
Bubble sort(cnt’d)
Step-by-Step Example
• Assume we have an array "5 1 4 2 8" and we want
to sort the array from the lowest number to the
greatest number using bubble sort.

06/17/2022
Cont..
Algorithm
1 /input “n” elements of the array ;
2/initialize i=0 and repeat through step 4 if (i<n)
3/initialize i=0 and repeat through step 4 if (j<n-1-i)
4/if(array[j]>array[j+1])
{
temp = array[j]
array[j]=array[j+1]
array[j+1]=temp
}
5/Display the sorted element of array
6/exit

06/17/2022
Cont…
Performance
• Worst case performance: O(n2)
• Best case performance: O(n)
• Average case performance: O(n2)
• Worst case space complexity: O(n) total, O(1)
auxiliary

06/17/2022
Cont…
• The advantages bubble sort algorithm.
 It is simple to write,
 easy to understand and it only takes a few
lines of code.
 The data is sorted in place so there is little
memory overhead and,
 once sorted, the data is in memory,
 ready for processing.
06/17/2022
Cont..
• The major disadvantage is the amount of
time it takes to sort. The average time
increases almost exponentially as the number
of table elements increase.

06/17/2022
Merge Sort
• Merge sort is an O(n log n) comparison-based sorting
algorithm. It is an example of the divide and conquer
algorithmic paradigm.
Algorithm
• Conceptually, a merge sort works as follows:
 If the list is of length 0 or 1, then it is already sorted.
Otherwise:
 Divide the unsorted list into two sublists of about half the size.
 Sort each sublist recursively by re-applying merge sort.
 Merge the two sublists back into one sorted list.

06/17/2022
Cont…
• Merge sort incorporates two main ideas to
improve its runtime:
 A small list will take fewer steps to sort than a
large list.
 Fewer steps are required to construct a sorted
list from two sorted lists than two unsorted
lists.

06/17/2022
The division procedure of merge sort
algorithm
MergeSort(A) }
{ for(int i=mid ; i<=n-1 ; ++i)
n = length(A) {
if n<2 return right[i-mid] = A[i] // Copying elements
mid = n/2 from A to right
left = new_array_of_size(mid) // }
Creating temporary array for left MergeSort(left) // Recursively solving
right = new_array_of_size(n-mid) // for left sub array
and right sub arrays MergeSort(right) // Recursively
for(int i=0 ; i<=mid-1 ; ++i) solving for right sub array
{ merge(left, right, A) // Merging two
left[i] = A[i] // Copying elements from sorted left/right sub array to final array
•}
A to left

06/17/2022
Merge Sort: Implementation

void mergeSort(int a[], int low, int high) {


if (low < high) {
int mid = (low+high) / 2;
mergeSort(a, low , mid );
mergeSort(a, mid+1, high);
merge(a, low, mid, high);
}
}
06/17/2022
example

06/17/2022
Time Complexity Analysis-
• In merge sort, we divide the array into two
(nearly) equal halves and solve them
recursively using merge sort only.
So, we have-

06/17/2022
Cont…
• If T(n) is the time required by merge sort for
sorting an array of size n, then the recurrence
relation for time complexity of merge sort is-

• On solving this recurrence relation, we get T(n) = Θ(nlogn).

06/17/2022
Space Complexity Analysis-

• Merge sort uses additional memory for left


and right sub arrays.
• Hence, total Θ(n) extra memory is needed.

06/17/2022
Advantage merge sort
• It is quicker for larger lists because unlike insertion and
bubble sort it doesnt go through the whole list seveal times.
• It has a consistent running time, carries out different bits
with similar times in a stage.
What Disadvantage of the Merge Sort? 
 For small datasets, merge sort is slower than other
sorting algorithms.
 For the temporary array, mergesort requires an additional
space of O(n). Even if the array is sorted, the merge sort
goes through the entire process

06/17/2022
Conclusions
• A sorting algorithm is said to be stable if two objects with
equal keys appear in the same order in sorted output as they
appear in the input unsorted array.
• Some sorting algorithms are stable by nature like Insertion
sort, Merge Sort, Bubble Sort, etc. In this paper we
introduced Merge Sort algorithm, an O (N logN) time and
Bubble sort algorithm , O(n2) time .
• Strengths Bubble sort:It is easy to understand,Easy to
implement, No demand for large amounts of memory,Once
sorted, data is available for processing.
• Weaknesses:Sorting takes a long time.

06/17/2022
Cont…
Strengths Merge sort:
• More applicable in accessing data with slow access
rates, typically tape drives and hard disks.
• The size of the file does not adversely affect the
performance.
• It is good for sorting through data sets that are
accessed in sequence.

06/17/2022
THANK YOU

06/17/2022

You might also like