Graph & Sorting Algorithm - Unit VI
Graph & Sorting Algorithm - Unit VI
77 42 35 12 101 5
1 2 3 4 5 6
5 12 35 42 77 101
Computer Science
Types of sorting
• Selection sort
• Insertion sort
• Merge sort
• Radix sort
Computer Science
Selection Sort
• Selection sort is a sorting algorithm which works as
follows:
– Find the minimum value in the list
– Swap it with the value in the first position
– Repeat the steps above for remainder of the list
(starting at the second position)
Computer Science
Selection Sort
Computer Science
Example: Selection Sort
• 26 33 43 100 46 88 52 17 53 77
• 17 | 33 43 100 46 88 52 26 53 77
• 17 26 | 43 100 46 88 52 33 53 77
• 17 26 33 | 100 46 88 52 43 53 77
• 17 26 33 43 | 46 88 52 100 53 77
• 17 26 33 43 46 | 88 52 100 53 77
• 17 26 33 43 46 52 | 88 100 53 77
• 17 26 33 43 46 52 53 | 100 88 77
• 17 26 33 43 46 52 53 77 | 88 100
• 17 26 33 43 46 52 53 77 88 | 100
Computer Science
Insertion Sort
Insertion Sort
Computer Science
Insertion Sort
• In insertion sort, each successive element in the
array to be sorted is inserted into its proper place
with respect to the other, already sorted elements.
36 36 24 10 6 6
24 24 36 24 10 10
10 10 10 36 24 12
6 6 6 6 36 24
12 12 12 12 12 36
Computer Science
Insertion Sort
• Our strategy is to search for insertion point from
the beginning of the array and shift the element
down to make room for new element
• We compare the item at array[current] to one
before it, and swap if it is less.
• We then compare array[current-1] to one before
it and swap if necessary.
•
Computer Science
Insertion Sort
Computer Science
Example: Insertion Sort
• 99 | 55 4 66 28 31 36 52 38 72
• 55 99 | 4 66 28 31 36 52 38 72
• 4 55 99 | 66 28 31 36 52 38 72
• 4 55 66 99 | 28 31 36 52 38 72
• 4 28 55 66 99 | 31 36 52 38 72
• 4 28 31 55 66 99 | 36 52 38 72
• 4 28 31 36 55 66 99 | 52 38 72
• 4 28 31 36 52 55 66 99 | 38 72
• 4 28 31 36 38 52 55 66 99 | 72
• 4 28 31 36 38 52 55 66 72 99 |
Computer Science
Merge Sort:Divide and Conquer
• Divide and Conquer cuts the problem in half
each time, but uses the result of both halves:
– cut the problem in half until the problem is
trivial
– solve for both halves
– combine the solutions
Computer Science
Merge Sort:Divide and Conquer
Computer Science
Merge Sort
Computer Science
Mergesort
• A divide-and-conquer algorithm:
• Divide the unsorted array into 2 halves until the
sub-arrays only contain one element
• Merge the sub-problem solutions together:
– Compare the sub-array’s first elements
– Remove the smallest element and put it into
the result array
– Continue the process until all elements have
been put into the result array
37 23 6 89 15 12 2 19
Computer Science
Merge Sort
• We don’t really pass in two arrays!
s1 f1 s2 f2
Computer Science
Radix Sort
Computer Science
Radix Sort
Computer Science
Radix Sort
Computer Science
Radix Sort
Computer Science
Radix Sort
Computer Science
Radix Sort
Computer Science
Radix Sort
Computer Science
Radix sort complexity
Time Complexity .1
Best Case Complexity - It occurs when there is no sorting required,
i.e. the array is already sorted. The best-case time complexity of
.Radix sort is Ω(n+k)
Average Case Complexity - It occurs when the array elements are
in jumbled order that is not properly ascending and not properly
.descending. The average case time complexity of Radix sort is θ(nk)
Worst Case Complexity - It occurs when the array elements are
required to be sorted in reverse order. That means suppose you have
to sort the array elements in ascending order, but its elements are in
descending order. The worst-case time complexity of Radix sort
.is O(nk)
Space Complexity .2
.The space complexity of Radix sort is O(n + k)
Computer Science
Graph
Computer Science
Graph
Computer Science
Graph
Computer Science
Graph Terminology
Computer Science
Graph Terminology
Computer Science
Graph Representation
Computer Science
Sequential Representation
Computer Science
Sequential Representation
Computer Science
Sequential Representation
Computer Science
Sequential Representation
Computer Science
Linked List Representation
Computer Science
Linked List Representation
Computer Science
Operations On Graph
Insertion of Nodes/Edges in the graph •
.– Insert a node into the graph
Deletion of Nodes/Edges in the graph •
.– Delete a node from the graph
Searching on Graphs – Search an •
.entity in the graph
Traversal of Graphs – Traversing all •
.the nodes in the graph
Computer Science
Traversing A Graph
:Two types of traversing
Breadth first search .1
It uses queue
Depth first search .2
It uses stack Logic
Add the starting node in the data structure,
remove it, process it and add its neighbor nodes to
the data structure. Repeat this procedure for each
node until data structure is empty and every node
is processed
Computer Science