sorting algorithms

29
Sorting Algorithms Technical Writing(CS1305) Assignment Shivam Singh 20148025 MNNIT August 2015 Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 1 / 20

Upload: shivam-singh

Post on 15-Apr-2017

412 views

Category:

Engineering


0 download

TRANSCRIPT

Sorting AlgorithmsTechnical Writing(CS1305) Assignment

Shivam Singh20148025

MNNIT

August 2015

Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 1 / 20

What Is A Sorting Algorithm ?

Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 2 / 20

Bubble Sort

Compare each element (except the last one) with its neighbor to theright

If they are out of order, swap themThis puts the largest element at the very endThe last element is now in the correct and final place

Compare each element (except the last two) with its neighbor to theright

Compare each element (except the last three) with its neighbor to theright

Continue as above until you have no unsorted elements on the left

Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 3 / 20

Bubble Sort

Compare each element (except the last one) with its neighbor to theright

If they are out of order, swap themThis puts the largest element at the very endThe last element is now in the correct and final place

Compare each element (except the last two) with its neighbor to theright

Compare each element (except the last three) with its neighbor to theright

Continue as above until you have no unsorted elements on the left

Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 3 / 20

Bubble Sort

Compare each element (except the last one) with its neighbor to theright

If they are out of order, swap themThis puts the largest element at the very endThe last element is now in the correct and final place

Compare each element (except the last two) with its neighbor to theright

Compare each element (except the last three) with its neighbor to theright

Continue as above until you have no unsorted elements on the left

Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 3 / 20

Bubble SortExplanation

Figure: Graphical illustration of Bubble Sort

Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 4 / 20

Bubble Sort

Pseudo-Code

for i ← 0toN − 2 dofor j ← 0toN − 2 do

if A[j ] ≥ A[j + 1] thenSwap(A[j],A[j+1])

end ifend for

end for

Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 5 / 20

Selection Sort

Given an array of length n :

Search elements 0 through n-1 and select the smallest

Swap it with the element in location 0

Search elements 1 through n-1 and select the smallest

Swap it with the element in location 1

Continue in this fashion until theres nothing left to search

Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 6 / 20

Selection Sort

Given an array of length n :

Search elements 0 through n-1 and select the smallest

Swap it with the element in location 0

Search elements 1 through n-1 and select the smallest

Swap it with the element in location 1

Continue in this fashion until theres nothing left to search

Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 6 / 20

Selection Sort

Given an array of length n :

Search elements 0 through n-1 and select the smallest

Swap it with the element in location 0

Search elements 1 through n-1 and select the smallest

Swap it with the element in location 1

Continue in this fashion until theres nothing left to search

Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 6 / 20

Selection Sort

Given an array of length n :

Search elements 0 through n-1 and select the smallest

Swap it with the element in location 0

Search elements 1 through n-1 and select the smallest

Swap it with the element in location 1

Continue in this fashion until theres nothing left to search

Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 6 / 20

Selection SortGraphical Illustration

Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 7 / 20

Selection Sort

Pseudo-Code

for i ← 0toN − 1 doMin← ifor j ← 0toN − 1 doif A[j ] ≤ A[Min] then

Min← jend if

end forif i 6= Min thenSwap(A[i],A[Min])

end ifend for

Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 8 / 20

Insertion Sort

Outer Loop

The Outer Loop i goes from 1→ N

all the elements to the left of i are sorted with respect to one another

Inner Loop

In every iteration of the Inner Loop we are :1 Finding the elements proper place2 Making room for the inserted element (by shifting over other elements)3 Inserting the element

Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 9 / 20

Insertion Sort

Outer Loop

The Outer Loop i goes from 1→ N

all the elements to the left of i are sorted with respect to one another

Inner Loop

In every iteration of the Inner Loop we are :1 Finding the elements proper place2 Making room for the inserted element (by shifting over other elements)3 Inserting the element

Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 9 / 20

Insertion SortGraphical Illustration

Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 10 / 20

Insertion Sort

Pseudo-Code

for i ← 1toN − 1 doTemp ← A[i ]j ← iwhile A[j ] > Tempandj > 0 doA[j ]← A[j − 1]j ← j − 1

end whileA[j ]← Temp

end for

Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 11 / 20

Merge Sort

A Divide and Conquer Algorithm

Divide the unsorted array into 2 halves until the sub-arrays onlycontain one element

Merge the sub-problem solutions together1 Compare the sub-arrays first elements2 Remove the smallest element and put it into the result array3 Continue the process until all elements have been put into the result

array

Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 12 / 20

Merge Sort

A Divide and Conquer Algorithm

Divide the unsorted array into 2 halves until the sub-arrays onlycontain one element

Merge the sub-problem solutions together

1 Compare the sub-arrays first elements2 Remove the smallest element and put it into the result array3 Continue the process until all elements have been put into the result

array

Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 12 / 20

Merge Sort

A Divide and Conquer Algorithm

Divide the unsorted array into 2 halves until the sub-arrays onlycontain one element

Merge the sub-problem solutions together1 Compare the sub-arrays first elements2 Remove the smallest element and put it into the result array3 Continue the process until all elements have been put into the result

array

Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 12 / 20

Merge Sort-Explanation

Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 13 / 20

Merge SortPseudo-Code

Divide And Conquer

Sort(A, N)if N ≤ 1 thenreturn

end ifnL← N/2 , nR ← N − N/2for i ← 0toMid − 1 do

Left[i ]← A[i ]end forfor i ← MidtoN − 1 doRight[i ]← A[i ]

end forSort(Left, nL)Sort(Right, nR)Merge(Left, Right, A)

Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 14 / 20

Merge SortPseudo-Code

Merging The SubArrays

Merge(Left, Right, A, N)nL← N/2 , nR ← N − N/2 , i ← j ← k ← 0while i < nLandj < nR do

if Left[i ] ≤ Right[j ] thenA[k]← Left[i ]i ← i + 1

elseA[k]← Right[j ]j ← j + 1

end ifk ← k + 1

end while

Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 15 / 20

Merge SortPseudo-Code

Merging The SubArrays Cont...

while i < nL doA[k]← Left[i ]i ← i + 1k ← k + 1

end whilewhile j < nR doA[k]← Right[j ]j ← j + 1k ← k + 1

end while

Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 16 / 20

Which Is The Best ?

Algorithm Time ComplexityBubble Sort O(n2)

Selection Sort O(n2)

Insertion Sort O(n2)

Merge Sort O(n ∗ (log2n))

Answer :

As we can observe from the Time Complexities, Merge Sort will be thefastest Sorting Algorithm !

Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 17 / 20

Which Is The Best ?

Algorithm Time ComplexityBubble Sort O(n2)

Selection Sort O(n2)

Insertion Sort O(n2)

Merge Sort O(n ∗ (log2n))

Answer :

As we can observe from the Time Complexities, Merge Sort will be thefastest Sorting Algorithm !

Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 17 / 20

Special MentionQuickSort

Recursive Quick Sort

QuickSort(A, Start, End)if Start ≥ End thenreturn

end ifpIndex ←Partition(A, Start, End)QuickSort(A, Start, pIndex-1)QuickSort(A, pIndex+1, End)

Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 18 / 20

Special MentionQuickSort

Partitioning Around The Pivot

Partition(A, Start, End)Pivot ← A[End ]PIndex ← Startfor i ← StarttoEnd − 1 do

if A[i ] ≤ Pivot thenSwap(A[i],A[PIndex])PIndex ← PIndex + 1

end ifend forSwap(A[i],A[PIndex])return PIndex

Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 19 / 20

End

Thank you

Shivam Singh 20148025 (MNNIT) Sorting Algorithms August 2015 20 / 20