0% found this document useful (0 votes)
25 views

Sorting

Sorting algorithms discussed include bubble sort, insertion sort, selection sort, quick sort, and merge sort. Bubble sort has a time complexity of O(n^2) due to its repeated exchanges. Insertion sort runs in O(n) time if the input is already sorted but O(n^2) time if reversed. Selection sort is generally O(n log n) but can be quadratic in worst case. Quick sort uses divide and conquer and has average case time of O(n log n) but worst case of O(n^2). Merge sort also uses divide and conquer and has a best, average, and worst case time of O(n log n).

Uploaded by

Atif
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Sorting

Sorting algorithms discussed include bubble sort, insertion sort, selection sort, quick sort, and merge sort. Bubble sort has a time complexity of O(n^2) due to its repeated exchanges. Insertion sort runs in O(n) time if the input is already sorted but O(n^2) time if reversed. Selection sort is generally O(n log n) but can be quadratic in worst case. Quick sort uses divide and conquer and has average case time of O(n log n) but worst case of O(n^2). Merge sort also uses divide and conquer and has a best, average, and worst case time of O(n log n).

Uploaded by

Atif
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Sorting:

• Sorting is a process of ordering or placing a list


of elements from a collection in some kind of
order.
Types of sorting:
• Bubble Sort
• Insertion Sort
• Selection Sort
• Quick Sort
• Merge sort 
1
Design and Analysis Bubble Sort
Bubble Sort is an elementary sorting algorithm,
which works by repeatedly exchanging adjacent
elements, if necessary. When no exchanges are
required, the file is sorted.
Algorithm: Sequential-Bubble-Sort (A)
fori← 1 to length [A] do
for j ← length [A] down-to i +1 do
if A[A] < A[j - 1] then
Exchange A[j] ↔ A[j-1]

2
Analysis of Bubble Sort
• Here, the number of comparisons are
• 1 + 2 + 3 +...+ (n - 1) = n(n - 1)/2 = O(n2)
• Clearly, the graph shows the n2 nature of the
bubble sort.
• In this algorithm, the number of comparison is
irrespective of the data set, i.e. whether the
provided input elements are in sorted order or
in reverse order or at random.
.

3
4
Design and Analysis Insertion Sort
Insertion sort is a very simple method to sort numbers in an
ascending or descending order. This method follows the
incremental method. 

Analysis:
Run time of this algorithm is
very much dependent on the
given input.
If the given numbers are
sorted, this algorithm runs
in O(n) time. If the given
numbers are in reverse order,
the algorithm runs
in O(n2) time.
5
Example of Insertion Sort

6
7
Design and Analysis Selection Sort
The selection is a straightforward process of sorting
values. In this method, to sort the data in ascending order,
the 0th element is compared with all other elements.
Analysis Selection Sort
For each i from 1 to n - 1, there is one
exchange and n - i comparisons, so
there is a total of n - 1 exchanges and
(n − 1) + (n − 2) + ...+ 2 + 1 = n(n −
1)/2 comparisons.
These observations hold, no matter
what the input data is.
In the worst case, this could be
quadratic, but in the average case,
this quantity is O(n log n). It implies
that the running time of Selection
sort is quite insensitive to the input.

8
Selection Sort Example

9
10
Design and Analysis Quick Sort
It is used on the principle of divide-and-conquer.
It is a good general purpose sort and it consumes
relatively fewer resources during execution.
Advantages
• It is in-place since it uses only a small auxiliary stack.
• It requires only n (log n) time to sort n items.
• It has an extremely short inner loop.
Disadvantages
• It is recursive. Especially, if recursion is not available,
the implementation is extremely complicated.
• It requires quadratic (i.e., n2) time in the worst-case.

11
Algorithm: Quick-Sort (A, p, r)
if p < r then
q Partition (A, p, r)
Quick-Sort (A, p, q)
Quick-Sort (A, q + r, r)
Analysis of Quick-Sort
• The worst case complexity of Quick-Sort
algorithm is O(n2). However using this
technique, in average cases generally we get
the output in O(n log n) time.

12
Merge sort 
Merge sort is a sorting technique based on divide and conquer technique. With worst-
case time complexity being Ο(n log n), it is one of the most respected algorithms.
• Algorithm:
step 1: start step
2: declare array and left, right, mid variable step
3: perform merge function.
mergesort(array,left,right)
mergesort (array, left, right)
if left > right
return
mid= (left+right)/2
mergesort(array, left, mid)
mergesort(array, mid+1, right)
merge(array, left, mid, right) step
4: Stop

13
14
15

You might also like