SlideShare a Scribd company logo
Unit – 5
Searching & Sorting
Sorting & Searching
 Searching
• Linear/Sequential Search
• Binary Search
 Sorting
• Bubble sort
• Selection Sort
• Insertion Sort
• Quick Sort
• Merge Sort
Linear/Sequential Search
 In computer science, linear search or sequential search is a
method for finding a particular value in a list that consists of
checking every one of its elements, one at a time and in
sequence, until the desired one is found.
 Linear search is the simplest search algorithm.
 It is a special case of brute-force search.
 Its worst case cost is proportional to the number of elements in
the list.
Sequential Search - Algorithm
# Input: Array A, integer key
# Output: first index of key in A
# or -1 if not found
Algorithm: Linear_Search
for i = 0 to last index of A:
if A[i] equals key:
return i
return -1
Sequential Search - Example
Search for 1 in given array 2 9 3 1 8
Comparing value of ith index with element to be search one by one until we get searched
element or end of the array
Step 1: i=0
2 9 3 1 8
i
Step 1: i=1
2 9 3 1 8
i
Step 1: i=2
2 9 3 1 8
i
Step 1: i=3
2 9 3 1 8
i
1
Element found at ith index, i=3
Binary Search
 If we have an array that is sorted, we can use a much more
efficient algorithm called a Binary Search.
 In binary search each time we divide array into two equal half
and compare middle element with search element.
 Searching Logic
• If middle element is equal to search element then we got that
element and return that index
• if middle element is less than search element we look right part
of array
• if middle element is greater than search element we look left
part of array.
Binary Search - Algorithm
# Input: Sorted Array A, integer key
# Output: first index of key in A,
# or -1 if not found
Algorithm: Binary_Search (A, left, right)
left = 0, right = n-1
while left < right
middle = index halfway between left, right
if A[middle] matches key
return middle
else if key less than A[middle]
right = middle -1
else
left = middle + 1
return -1
Binary Search - Algorithm
Search for 6 in given array
-1 5 6 18 19 25 46 78 102 114
Key=6, No of Elements = 10, so left = 0, right=9
0 1 2 3 4 5 6 7 8 9
Index
middle index = (left + right) /2 = (0+9)/2 = 4
middle element value = a[4] = 19
Key=6 is less than middle element = 19, so right = middle – 1 = 4 – 1 = 3, left = 0
-1 5 6 18 19 25 46 78 102 114
0 1 2 3 4 5 6 7 8 9
Index
left right
left right
Step 1:
Binary Search - Algorithm
middle index = (left + right) /2 = (0+3)/2 = 1
middle element value = a[1] = 5
Key=6 is greater than middle element = 5, so left = middle + 1 =1 + 1 = 2, right = 3
-1 5 6 18 19 25 46 78 102 114
0 1 2 3 4 5 6 7 8 9
Index
left right
Step 2:
middle index = (left + right) /2 = (2+3)/2 = 2
middle element value = a[2] = 6
Key=6 is equals to middle element = 6, so element found
-1 5 6 18 19 25 46 78 102 114
0 1 2 3 4 5 6 7 8 9
Index
Element Found
Step 3:
6
Selection Sort
 Selection sort is a simple sorting algorithm.
 The list is divided into two parts,
• The sorted part at the left end and
• The unsorted part at the right end.
• Initially, the sorted part is empty and the unsorted part is the
entire list.
 The smallest element is selected from the unsorted array and
swapped with the leftmost element, and that element becomes a
part of the sorted array.
 This process continues moving unsorted array boundary by one
element to the right.
 This algorithm is not suitable for large data sets as its average and
worst case complexities are of Ο(n2), where n is the number of
items.
Selection Sort
5 1 12 -5 16 2 12 14
Unsorted Array
Step 1 :
5 1 12 -5 16 2 12 14
Unsorted Array
0 1 2 3 4 5 6 7
Step 2 :
Min index = 0, value = 5
5 1 12 -5 16 2 12 14
0 1 2 3 4 5 6 7
Find min value from
Unsorted array
Index = 3, value = -5
Unsorted Array (elements 0 to 7)
Swap
-5 5
Selection Sort
Step 3 :
-5 1 12 5 16 2 12 14
0 1 2 3 4 5 6 7
Unsorted Array (elements 1 to 7)
Min index = 1, value = 1
Find min value from
Unsorted array
Index = 1, value = 1
No Swapping as min value is already at right place
1
Step 4 :
-5 1 12 5 16 2 12 14
0 1 2 3 4 5 6 7
Unsorted Array
(elements 2 to 7)
Min index = 2, value = 12
Find min value from
Unsorted array
Index = 5, value = 2
Swap
2 12
Selection Sort
Step 5 :
-5 1 2 5 16 12 12 14
0 1 2 3 4 5 6 7
Min index = 3, value = 5
Find min value from
Unsorted array
Index = 3, value = 5
Step 6 :
-5 1 2 5 16 12 12 14
0 1 2 3 4 5 6 7
Min index = 4, value = 16
Find min value from
Unsorted array
Index = 5, value = 12
Swap
Unsorted Array
(elements 3 to 7)
No Swapping as min value is already at right place
5
Unsorted Array
(elements 5 to 7)
12 16
Selection Sort
Step 7 :
-5 1 2 5 12 16 12 14
0 1 2 3 4 5 6 7
Min index = 5, value = 16
Find min value from
Unsorted array
Index = 6, value = 12
Swap
12 16
Unsorted Array
(elements 5 to 7)
-5 1 2 5 12 12 16 14
0 1 2 3 4 5 6 7
Min index = 6, value = 16
Find min value from
Unsorted array
Index = 7, value = 14
Swap
14 16
Unsorted Array
(elements 6 to 7)
Step 8 :
SELECTION_SORT(K,N)
 Given a vector K of N elements
 This procedure rearrange the vector in ascending order using
Selection Sort
 The variable PASS denotes the pass index and position of the first
element in the vector
 The variable MIN_INDEX denotes the position of the smallest
element encountered
 The variable I is used to index elements
SELECTION_SORT(K,N)
procedure selection sort
list : array of items
n : size of list
for i = 1 to n - 1
/* set current element as minimum*/
min = i
/* check the element to be minimum */
for j = i+1 to n
if list[j] < list[min] then
min = j;
end if
end for
/* swap the minimum element with the current element*/
if indexMin != i then
swap list[min] and list[i]
end if
end for
end procedure
Bubble Sort
 Unlike selection sort, instead of finding the smallest record and
performing the interchange, two records are interchanged
immediately upon discovering that they are out of order
 During the first pass R1 and R2 are compared and interchanged in
case of our of order, this process is repeated for records R2 and
R3, and so on.
 This method will cause records with small key to move “bubble
up”,
 After the first pass, the record with largest key will be in the nth
position.
 On each successive pass, the records with the next largest key will
be placed in position n-1, n-2 ….., 2 respectively
 This approached required at most n–1 passes, The complexity of
bubble sort is O(n2)
Bubble Sort
45 34 56 23 12
Unsorted Array
Pass 1 :
45
34
56
23
12
34
45
56
23
12
34
45
56
23
12
34
45
23
56
12
34
45
swap
swap
23
56
swap
12
56
Bubble Sort
Pass 2 :
34
45
23
12
56
34
45
23
12
56
34
23
45
12
56
34
23
12
45
56
swap
23
45
swap
12
45
Pass 3 :
23
34
12
45
56
23
12
34
45
56
swap
23
34
swap
12
34
Pass 4 :
swap
12
23
BUBBLE_SORT(K,N)
 Given a vector K of N elements
 This procedure rearrange the vector in ascending order using
Bubble Sort
 The variable PASS & LAST denotes the pass index and position of
the first element in the vector
 The variable EXCHS is used to count number of exchanges made
on any pass
 The variable I is used to index elements
Procedure: BUBBLE_SORT (K, N)
begin BubbleSort(list)
for all elements of list
if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for
return list
end BubbleSort
Quick Sort
 Quick sort is a highly efficient sorting algorithm and is based on
partitioning of array of data into smaller arrays.
 Quick Sort is divide and conquer algorithm.
 At each step of the method, the goal is to place a particular record
in its final position within the table,
 In doing so all the records which precedes this record will have
smaller keys, while all records that follows it have larger keys.
 This particular records is termed pivot element.
 The same process can then be applied to each of these subtables
and repeated until all records are placed in their positions
Quick Sort
 There are many different versions of Quick Sort that pick pivot in
different ways.
• Always pick first element as pivot. (in our case we have consider
this version).
• Always pick last element as pivot (implemented below)
• Pick a random element as pivot.
• Pick median as pivot.
 Quick sort partitions an array and then calls itself recursively twice
to sort the two resulting sub arrays.
 This algorithm is quite efficient for large-sized data sets
 Its average and worst case complexity are of Ο(n2), where n is the
number of items.
Quick Sort
42 23 74 11 65 58 94 36
0 1 2 3 4 5 6 7
99 87
8 9
LB UB
Pivot
Element
Sort Following Array using Quick Sort Algorithm
We are considering first element as pivot element, so Lower bound
is First Index and Upper bound is Last Index
We need to find our proper position of Pivot element in sorted array
and perform same operations recursively for two sub array
Quick Sort
42 23 74 11 65 58 94 36
0 1 2 3 4 5 6 7
99 87
8 9
FLAG  true
IF LB < UB
Then
I  LB
J  UB + 1
KEY  K[LB]
Repeat While FLAG = true
I  I+1
Repeat While K[I] < KEY
I  I + 1
J  J – 1
Repeat While K[J] > KEY
J  J – 1
IF I<J
Then K[I] --- K[J]
Else FLAG  FALSE
K[LB] --- K[J]
LB = 0, UB = 9
I= 0
J= 10
KEY = 42
I J
FLAG= true
42 23 74 11 65 58 94 36 99 87
I J
36 74
Swap
42 23 36 11 65 58 94 74 99 87
I J
42
11
Swap
Quick Sort
FLAG  true
IF LB < UB
Then
I  LB
J  UB + 1
KEY  K[LB]
Repeat While FLAG = true
I  I+1
Repeat While K[I] < KEY
I  I + 1
J  J – 1
Repeat While K[J] > KEY
J  J – 1
IF I<J
Then K[I] --- K[J]
Else FLAG  FALSE
K[LB] --- K[J]
11 23 36 42 65 58 94 74
0 1 2 3 4 5 6 7
99 87
8 9
LB UB
11 23 36
I J
11
11 23 36 42 65 58 94 74 99 87
LB UB
23 36
I J
23
11 23 36 42 65 58 94 74 99 87
LB
UB
36
Quick Sort
FLAG  true
IF LB < UB
Then
I  LB
J  UB + 1
KEY  K[LB]
Repeat While FLAG = true
I  I+1
Repeat While K[I] < KEY
I  I + 1
J  J – 1
Repeat While K[J] > KEY
J  J – 1
IF I<J
Then K[I] --- K[J]
Else FLAG  FALSE
K[LB] --- K[J]
11 23 36 42 65 58 94 72 99 87
4 5 6 7 8 9
LB UB
65 58 94 72 99 87
I J
65 65
58
Swap
65 94 72 99 87
65
58 65
11 23 36 42 65 65 94 72 99 87
65
58
LB
UB
58
LB UB
Quick Sort
I
Swap
94 72 99 87
J
FLAG  true
IF LB < UB
Then
I  LB
J  UB + 1
KEY  K[LB]
Repeat While FLAG = true
I  I+1
Repeat While K[I] < KEY
I  I + 1
J  J – 1
Repeat While K[J] > KEY
J  J – 1
IF I<J
Then K[I] --- K[J]
Else FLAG  FALSE
K[LB] --- K[J]
UB
LB
94 87 99
94 72 87 99
94
I J
87 94
Swap
Swap
72
87
UB
LB
I J
87
72 87 99
94
72 87 99
94
LB
UB
LB
UB
72 99
11 23 36 42 65
58
Algorithm: QUICK_SORT(K,LB,UB)
1. [Initialize]
FLAG  true
2. [Perform Sort]
IF LB < UB
Then I  LB
J  UB + 1
KEY  K[LB]
Repeat While FLAG = true
I  I+1
Repeat While K[I] < KEY
I  I + 1
J  J – 1
Repeat While K[J] > KEY
J  J – 1
IF I<J
Then K[I] --- K[J]
Else FLAG  FALSE
K[LB] --- K[J]
CALL QUICK_SORT(K,LB, J-1)
CALL QUICK_SORT(K,J+1, UB)
CALL QUICK_SORT(K,LB, J-1)
3. [Finished]
Return
Merge Sort
 The operation of sorting is closely related to process of merging
 Merge Sort is a divide and conquer algorithm
 It is based on the idea of breaking down a list into several sub-
lists until each sub list consists of a single element
 Merging those sub lists in a manner that results into a sorted list
 Procedure
• Divide the unsorted list into N sub lists, each containing 1
element
• Take adjacent pairs of two singleton lists and merge them to form
a list of 2 elements. N will now convert into N/2 lists of size 2
• Repeat the process till a single sorted list of obtained
 Time complexity is O(n log n)
Merge Sort
724 521 2 98 529 31 189 451
Unsorted Array
0 1 2 3 4 5 6 7
724 521 2 98 529 31 189 451
0 1 2 3 4 5 6 7
Step 1: Split the selected array (as evenly as possible)
724 521 2 98
0 1 2 3
529 31 189 451
0 1 2 3
Merge Sort
Step: Select the left subarray, Split the selected array (as evenly as possible)
724 521 2 98
0 1 2 3
529 31 189 451
0 1 2 3
724 521
0 1
2 98
0 1
724
0
521
0
2
0
98
0
521 724 2 98
2 98 521 724
529 31
0 1
189 451
0 1
529
0
31
0
189
0
451
0
31 529 189 451
31 189 451 529
2 31 98 189 451 521 529 724
Merge Sort
 step 1: start
 step 2: declare array and left, right, mid variable
 step 3: perform merge function.
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
Insertion Sort
In insertion sort, every iteration moves an element from unsorted
portion to sorted portion until all the elements are sorted in the list.
Steps for Insertion Sort
1
Assume that first element in the list is in sorted portion of the
list and remaining all elements are in unsorted portion.
2
Select first element from the unsorted list and insert that
element into the sorted list in order specified.
3
Repeat the above process until all the elements from the
unsorted list are moved into the sorted list.
This algorithm is not suitable for large data sets
Insertion Sort cont.
Complexity of the Insertion Sort Algorithm
To sort a unsorted list with 'n' number of elements we need to
make (1+2+3+......+n-1) = (n (n-1))/2 number of comparisons in the
worst case.
If the list already sorted, then it requires 'n' number of
comparisons.
• Worst Case : Θ(n2)
• Best Case : Ω(n)
• Average Case : Θ(n2)
Insertion Sort Example
Sort given array using Insertion Sort
Pass - 1 : Select First Record and considered as Sorter Sub-array
5 3 1 8 7 2 4
6 5 3 1 8 7 2 4
6 5 3 1 8 7 2 4
6
Sorted Unsorted
Pass - 2 : Select Second Record and Insert at proper place in sorted array
5 6 3 1 8 7 2 4
Sorted Unsorted
Insertion Sort Example Cont.
Pass - 3 : Select Third record and Insert at proper place in sorted array
5 6 3 1 8 7 2 4
3 5 6 1 8 7 2 4
Sorted Unsorted
Pass - 4 : Select Forth record and Insert at proper place in sorted array
3 5 6 1 8 7 2 4
1 3 5 6 8 7 2 4
Sorted Unsorted
Insertion Sort Example Cont.
1 3 5 6 8 7 2 4
Pass - 5 : Select Fifth record and Insert at proper place in sorted array
1 3 5 6 8 7 2 4
8 is at proper position
Pass - 6 : Select Sixth Record and Insert at proper place in sorted array
1 3 5 6 8 7 2 4
1 3 5 6 7 8 2 4
Sorted Unsorted
Sorted Unsorted
Insertion Sort Example Cont.
Pass - 7 : Select Seventh record and Insert at proper place in sorted array
1 2 3 5 6 7 8 4
Pass - 8 : Select Eighth Record and Insert at proper place in sorted array
Sorted Unsorted
1 3 5 6 7 8 2 4
1 2 3 5 6 7 8 4
1 2 3 5 6 7 8
4
Sorted Unsorted

More Related Content

PPT
Sorting algorithms
PPTX
Bubble sort, Selection sort SORTING .pptx
PPTX
Data Structures - Lecture 8 [Sorting Algorithms]
PPT
Searching algorithms
PPTX
Sorting algorithms
PPTX
Brute force method
PPT
Unit 3 graph chapter6
PPTX
Unit 5 internal sorting &amp; files
Sorting algorithms
Bubble sort, Selection sort SORTING .pptx
Data Structures - Lecture 8 [Sorting Algorithms]
Searching algorithms
Sorting algorithms
Brute force method
Unit 3 graph chapter6
Unit 5 internal sorting &amp; files

What's hot (20)

PPTX
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPTX
Binary search
PPTX
Insertion sort
PPT
Binary search tree in data structures
PPTX
PPTX
Linear search-and-binary-search
PPTX
Sorting Algorithms
PPT
PPT
Binary Search
PPT
Queue data structure
PPTX
concept of Array, 1D & 2D array
PPTX
sorting and its types
PPTX
Data Structure - Elementary Data Organization
PPTX
Doubly Linked List
PDF
Sorting algorithms
PPTX
Ppt bubble sort
PPT
Selection sort
PPT
Data Structures- Part5 recursion
PPTX
Queue and its operations
PPT
SEARCHING AND SORTING ALGORITHMS
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Binary search
Insertion sort
Binary search tree in data structures
Linear search-and-binary-search
Sorting Algorithms
Binary Search
Queue data structure
concept of Array, 1D & 2D array
sorting and its types
Data Structure - Elementary Data Organization
Doubly Linked List
Sorting algorithms
Ppt bubble sort
Selection sort
Data Structures- Part5 recursion
Queue and its operations
SEARCHING AND SORTING ALGORITHMS
Ad

Similar to sorting and searching.pptx (20)

PPTX
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
PPTX
Searching and Sorting algorithms and working
PPTX
All Searching and Sorting Techniques in Data Structures
PPT
Data Structures - Searching & sorting
PPTX
Data Structure and algorithms for software
PPTX
Weak 11-12 Sorting update.pptxbhjiiuuuuu
PDF
Sorting
PDF
Sorting
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
PPTX
Chapter 2. data structure and algorithm
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
PPT
Unit 7 sorting
PPTX
Sorting Algorithms to arrange data in particular format
PPTX
searching in data structure.pptx
PPTX
2.Problem Solving Techniques and Data Structures.pptx
PPTX
Searching, Sorting and Hashing Techniques
PPTX
Chapter 3 - Data Structure and Algorithms.pptx
PPT
search_sort.ppt
PPTX
Unit 5 dsuc
PPTX
Different Searching and Sorting Methods.pptx
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
Searching and Sorting algorithms and working
All Searching and Sorting Techniques in Data Structures
Data Structures - Searching & sorting
Data Structure and algorithms for software
Weak 11-12 Sorting update.pptxbhjiiuuuuu
Sorting
Sorting
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
Chapter 2. data structure and algorithm
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
Unit 7 sorting
Sorting Algorithms to arrange data in particular format
searching in data structure.pptx
2.Problem Solving Techniques and Data Structures.pptx
Searching, Sorting and Hashing Techniques
Chapter 3 - Data Structure and Algorithms.pptx
search_sort.ppt
Unit 5 dsuc
Different Searching and Sorting Methods.pptx
Ad

Recently uploaded (20)

PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Geodesy 1.pptx...............................................
PDF
Well-logging-methods_new................
PPTX
Construction Project Organization Group 2.pptx
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPT
Project quality management in manufacturing
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
additive manufacturing of ss316l using mig welding
PDF
Digital Logic Computer Design lecture notes
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Structs to JSON How Go Powers REST APIs.pdf
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Lecture Notes Electrical Wiring System Components
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
OOP with Java - Java Introduction (Basics)
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Geodesy 1.pptx...............................................
Well-logging-methods_new................
Construction Project Organization Group 2.pptx
Strings in CPP - Strings in C++ are sequences of characters used to store and...
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Project quality management in manufacturing
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
additive manufacturing of ss316l using mig welding
Digital Logic Computer Design lecture notes
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Structs to JSON How Go Powers REST APIs.pdf
Arduino robotics embedded978-1-4302-3184-4.pdf
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Lecture Notes Electrical Wiring System Components
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
CYBER-CRIMES AND SECURITY A guide to understanding
OOP with Java - Java Introduction (Basics)

sorting and searching.pptx

  • 2. Sorting & Searching  Searching • Linear/Sequential Search • Binary Search  Sorting • Bubble sort • Selection Sort • Insertion Sort • Quick Sort • Merge Sort
  • 3. Linear/Sequential Search  In computer science, linear search or sequential search is a method for finding a particular value in a list that consists of checking every one of its elements, one at a time and in sequence, until the desired one is found.  Linear search is the simplest search algorithm.  It is a special case of brute-force search.  Its worst case cost is proportional to the number of elements in the list.
  • 4. Sequential Search - Algorithm # Input: Array A, integer key # Output: first index of key in A # or -1 if not found Algorithm: Linear_Search for i = 0 to last index of A: if A[i] equals key: return i return -1
  • 5. Sequential Search - Example Search for 1 in given array 2 9 3 1 8 Comparing value of ith index with element to be search one by one until we get searched element or end of the array Step 1: i=0 2 9 3 1 8 i Step 1: i=1 2 9 3 1 8 i Step 1: i=2 2 9 3 1 8 i Step 1: i=3 2 9 3 1 8 i 1 Element found at ith index, i=3
  • 6. Binary Search  If we have an array that is sorted, we can use a much more efficient algorithm called a Binary Search.  In binary search each time we divide array into two equal half and compare middle element with search element.  Searching Logic • If middle element is equal to search element then we got that element and return that index • if middle element is less than search element we look right part of array • if middle element is greater than search element we look left part of array.
  • 7. Binary Search - Algorithm # Input: Sorted Array A, integer key # Output: first index of key in A, # or -1 if not found Algorithm: Binary_Search (A, left, right) left = 0, right = n-1 while left < right middle = index halfway between left, right if A[middle] matches key return middle else if key less than A[middle] right = middle -1 else left = middle + 1 return -1
  • 8. Binary Search - Algorithm Search for 6 in given array -1 5 6 18 19 25 46 78 102 114 Key=6, No of Elements = 10, so left = 0, right=9 0 1 2 3 4 5 6 7 8 9 Index middle index = (left + right) /2 = (0+9)/2 = 4 middle element value = a[4] = 19 Key=6 is less than middle element = 19, so right = middle – 1 = 4 – 1 = 3, left = 0 -1 5 6 18 19 25 46 78 102 114 0 1 2 3 4 5 6 7 8 9 Index left right left right Step 1:
  • 9. Binary Search - Algorithm middle index = (left + right) /2 = (0+3)/2 = 1 middle element value = a[1] = 5 Key=6 is greater than middle element = 5, so left = middle + 1 =1 + 1 = 2, right = 3 -1 5 6 18 19 25 46 78 102 114 0 1 2 3 4 5 6 7 8 9 Index left right Step 2: middle index = (left + right) /2 = (2+3)/2 = 2 middle element value = a[2] = 6 Key=6 is equals to middle element = 6, so element found -1 5 6 18 19 25 46 78 102 114 0 1 2 3 4 5 6 7 8 9 Index Element Found Step 3: 6
  • 10. Selection Sort  Selection sort is a simple sorting algorithm.  The list is divided into two parts, • The sorted part at the left end and • The unsorted part at the right end. • Initially, the sorted part is empty and the unsorted part is the entire list.  The smallest element is selected from the unsorted array and swapped with the leftmost element, and that element becomes a part of the sorted array.  This process continues moving unsorted array boundary by one element to the right.  This algorithm is not suitable for large data sets as its average and worst case complexities are of Ο(n2), where n is the number of items.
  • 11. Selection Sort 5 1 12 -5 16 2 12 14 Unsorted Array Step 1 : 5 1 12 -5 16 2 12 14 Unsorted Array 0 1 2 3 4 5 6 7 Step 2 : Min index = 0, value = 5 5 1 12 -5 16 2 12 14 0 1 2 3 4 5 6 7 Find min value from Unsorted array Index = 3, value = -5 Unsorted Array (elements 0 to 7) Swap -5 5
  • 12. Selection Sort Step 3 : -5 1 12 5 16 2 12 14 0 1 2 3 4 5 6 7 Unsorted Array (elements 1 to 7) Min index = 1, value = 1 Find min value from Unsorted array Index = 1, value = 1 No Swapping as min value is already at right place 1 Step 4 : -5 1 12 5 16 2 12 14 0 1 2 3 4 5 6 7 Unsorted Array (elements 2 to 7) Min index = 2, value = 12 Find min value from Unsorted array Index = 5, value = 2 Swap 2 12
  • 13. Selection Sort Step 5 : -5 1 2 5 16 12 12 14 0 1 2 3 4 5 6 7 Min index = 3, value = 5 Find min value from Unsorted array Index = 3, value = 5 Step 6 : -5 1 2 5 16 12 12 14 0 1 2 3 4 5 6 7 Min index = 4, value = 16 Find min value from Unsorted array Index = 5, value = 12 Swap Unsorted Array (elements 3 to 7) No Swapping as min value is already at right place 5 Unsorted Array (elements 5 to 7) 12 16
  • 14. Selection Sort Step 7 : -5 1 2 5 12 16 12 14 0 1 2 3 4 5 6 7 Min index = 5, value = 16 Find min value from Unsorted array Index = 6, value = 12 Swap 12 16 Unsorted Array (elements 5 to 7) -5 1 2 5 12 12 16 14 0 1 2 3 4 5 6 7 Min index = 6, value = 16 Find min value from Unsorted array Index = 7, value = 14 Swap 14 16 Unsorted Array (elements 6 to 7) Step 8 :
  • 15. SELECTION_SORT(K,N)  Given a vector K of N elements  This procedure rearrange the vector in ascending order using Selection Sort  The variable PASS denotes the pass index and position of the first element in the vector  The variable MIN_INDEX denotes the position of the smallest element encountered  The variable I is used to index elements
  • 16. SELECTION_SORT(K,N) procedure selection sort list : array of items n : size of list for i = 1 to n - 1 /* set current element as minimum*/ min = i /* check the element to be minimum */ for j = i+1 to n if list[j] < list[min] then min = j; end if end for /* swap the minimum element with the current element*/ if indexMin != i then swap list[min] and list[i] end if end for end procedure
  • 17. Bubble Sort  Unlike selection sort, instead of finding the smallest record and performing the interchange, two records are interchanged immediately upon discovering that they are out of order  During the first pass R1 and R2 are compared and interchanged in case of our of order, this process is repeated for records R2 and R3, and so on.  This method will cause records with small key to move “bubble up”,  After the first pass, the record with largest key will be in the nth position.  On each successive pass, the records with the next largest key will be placed in position n-1, n-2 ….., 2 respectively  This approached required at most n–1 passes, The complexity of bubble sort is O(n2)
  • 18. Bubble Sort 45 34 56 23 12 Unsorted Array Pass 1 : 45 34 56 23 12 34 45 56 23 12 34 45 56 23 12 34 45 23 56 12 34 45 swap swap 23 56 swap 12 56
  • 19. Bubble Sort Pass 2 : 34 45 23 12 56 34 45 23 12 56 34 23 45 12 56 34 23 12 45 56 swap 23 45 swap 12 45 Pass 3 : 23 34 12 45 56 23 12 34 45 56 swap 23 34 swap 12 34 Pass 4 : swap 12 23
  • 20. BUBBLE_SORT(K,N)  Given a vector K of N elements  This procedure rearrange the vector in ascending order using Bubble Sort  The variable PASS & LAST denotes the pass index and position of the first element in the vector  The variable EXCHS is used to count number of exchanges made on any pass  The variable I is used to index elements
  • 21. Procedure: BUBBLE_SORT (K, N) begin BubbleSort(list) for all elements of list if list[i] > list[i+1] swap(list[i], list[i+1]) end if end for return list end BubbleSort
  • 22. Quick Sort  Quick sort is a highly efficient sorting algorithm and is based on partitioning of array of data into smaller arrays.  Quick Sort is divide and conquer algorithm.  At each step of the method, the goal is to place a particular record in its final position within the table,  In doing so all the records which precedes this record will have smaller keys, while all records that follows it have larger keys.  This particular records is termed pivot element.  The same process can then be applied to each of these subtables and repeated until all records are placed in their positions
  • 23. Quick Sort  There are many different versions of Quick Sort that pick pivot in different ways. • Always pick first element as pivot. (in our case we have consider this version). • Always pick last element as pivot (implemented below) • Pick a random element as pivot. • Pick median as pivot.  Quick sort partitions an array and then calls itself recursively twice to sort the two resulting sub arrays.  This algorithm is quite efficient for large-sized data sets  Its average and worst case complexity are of Ο(n2), where n is the number of items.
  • 24. Quick Sort 42 23 74 11 65 58 94 36 0 1 2 3 4 5 6 7 99 87 8 9 LB UB Pivot Element Sort Following Array using Quick Sort Algorithm We are considering first element as pivot element, so Lower bound is First Index and Upper bound is Last Index We need to find our proper position of Pivot element in sorted array and perform same operations recursively for two sub array
  • 25. Quick Sort 42 23 74 11 65 58 94 36 0 1 2 3 4 5 6 7 99 87 8 9 FLAG  true IF LB < UB Then I  LB J  UB + 1 KEY  K[LB] Repeat While FLAG = true I  I+1 Repeat While K[I] < KEY I  I + 1 J  J – 1 Repeat While K[J] > KEY J  J – 1 IF I<J Then K[I] --- K[J] Else FLAG  FALSE K[LB] --- K[J] LB = 0, UB = 9 I= 0 J= 10 KEY = 42 I J FLAG= true 42 23 74 11 65 58 94 36 99 87 I J 36 74 Swap 42 23 36 11 65 58 94 74 99 87 I J 42 11 Swap
  • 26. Quick Sort FLAG  true IF LB < UB Then I  LB J  UB + 1 KEY  K[LB] Repeat While FLAG = true I  I+1 Repeat While K[I] < KEY I  I + 1 J  J – 1 Repeat While K[J] > KEY J  J – 1 IF I<J Then K[I] --- K[J] Else FLAG  FALSE K[LB] --- K[J] 11 23 36 42 65 58 94 74 0 1 2 3 4 5 6 7 99 87 8 9 LB UB 11 23 36 I J 11 11 23 36 42 65 58 94 74 99 87 LB UB 23 36 I J 23 11 23 36 42 65 58 94 74 99 87 LB UB 36
  • 27. Quick Sort FLAG  true IF LB < UB Then I  LB J  UB + 1 KEY  K[LB] Repeat While FLAG = true I  I+1 Repeat While K[I] < KEY I  I + 1 J  J – 1 Repeat While K[J] > KEY J  J – 1 IF I<J Then K[I] --- K[J] Else FLAG  FALSE K[LB] --- K[J] 11 23 36 42 65 58 94 72 99 87 4 5 6 7 8 9 LB UB 65 58 94 72 99 87 I J 65 65 58 Swap 65 94 72 99 87 65 58 65 11 23 36 42 65 65 94 72 99 87 65 58 LB UB 58 LB UB
  • 28. Quick Sort I Swap 94 72 99 87 J FLAG  true IF LB < UB Then I  LB J  UB + 1 KEY  K[LB] Repeat While FLAG = true I  I+1 Repeat While K[I] < KEY I  I + 1 J  J – 1 Repeat While K[J] > KEY J  J – 1 IF I<J Then K[I] --- K[J] Else FLAG  FALSE K[LB] --- K[J] UB LB 94 87 99 94 72 87 99 94 I J 87 94 Swap Swap 72 87 UB LB I J 87 72 87 99 94 72 87 99 94 LB UB LB UB 72 99 11 23 36 42 65 58
  • 29. Algorithm: QUICK_SORT(K,LB,UB) 1. [Initialize] FLAG  true 2. [Perform Sort] IF LB < UB Then I  LB J  UB + 1 KEY  K[LB] Repeat While FLAG = true I  I+1 Repeat While K[I] < KEY I  I + 1 J  J – 1 Repeat While K[J] > KEY J  J – 1 IF I<J Then K[I] --- K[J] Else FLAG  FALSE K[LB] --- K[J] CALL QUICK_SORT(K,LB, J-1) CALL QUICK_SORT(K,J+1, UB) CALL QUICK_SORT(K,LB, J-1) 3. [Finished] Return
  • 30. Merge Sort  The operation of sorting is closely related to process of merging  Merge Sort is a divide and conquer algorithm  It is based on the idea of breaking down a list into several sub- lists until each sub list consists of a single element  Merging those sub lists in a manner that results into a sorted list  Procedure • Divide the unsorted list into N sub lists, each containing 1 element • Take adjacent pairs of two singleton lists and merge them to form a list of 2 elements. N will now convert into N/2 lists of size 2 • Repeat the process till a single sorted list of obtained  Time complexity is O(n log n)
  • 31. Merge Sort 724 521 2 98 529 31 189 451 Unsorted Array 0 1 2 3 4 5 6 7 724 521 2 98 529 31 189 451 0 1 2 3 4 5 6 7 Step 1: Split the selected array (as evenly as possible) 724 521 2 98 0 1 2 3 529 31 189 451 0 1 2 3
  • 32. Merge Sort Step: Select the left subarray, Split the selected array (as evenly as possible) 724 521 2 98 0 1 2 3 529 31 189 451 0 1 2 3 724 521 0 1 2 98 0 1 724 0 521 0 2 0 98 0 521 724 2 98 2 98 521 724 529 31 0 1 189 451 0 1 529 0 31 0 189 0 451 0 31 529 189 451 31 189 451 529 2 31 98 189 451 521 529 724
  • 33. Merge Sort  step 1: start  step 2: declare array and left, right, mid variable  step 3: perform merge function. 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
  • 34. Insertion Sort In insertion sort, every iteration moves an element from unsorted portion to sorted portion until all the elements are sorted in the list. Steps for Insertion Sort 1 Assume that first element in the list is in sorted portion of the list and remaining all elements are in unsorted portion. 2 Select first element from the unsorted list and insert that element into the sorted list in order specified. 3 Repeat the above process until all the elements from the unsorted list are moved into the sorted list. This algorithm is not suitable for large data sets
  • 35. Insertion Sort cont. Complexity of the Insertion Sort Algorithm To sort a unsorted list with 'n' number of elements we need to make (1+2+3+......+n-1) = (n (n-1))/2 number of comparisons in the worst case. If the list already sorted, then it requires 'n' number of comparisons. • Worst Case : Θ(n2) • Best Case : Ω(n) • Average Case : Θ(n2)
  • 36. Insertion Sort Example Sort given array using Insertion Sort Pass - 1 : Select First Record and considered as Sorter Sub-array 5 3 1 8 7 2 4 6 5 3 1 8 7 2 4 6 5 3 1 8 7 2 4 6 Sorted Unsorted Pass - 2 : Select Second Record and Insert at proper place in sorted array 5 6 3 1 8 7 2 4 Sorted Unsorted
  • 37. Insertion Sort Example Cont. Pass - 3 : Select Third record and Insert at proper place in sorted array 5 6 3 1 8 7 2 4 3 5 6 1 8 7 2 4 Sorted Unsorted Pass - 4 : Select Forth record and Insert at proper place in sorted array 3 5 6 1 8 7 2 4 1 3 5 6 8 7 2 4 Sorted Unsorted
  • 38. Insertion Sort Example Cont. 1 3 5 6 8 7 2 4 Pass - 5 : Select Fifth record and Insert at proper place in sorted array 1 3 5 6 8 7 2 4 8 is at proper position Pass - 6 : Select Sixth Record and Insert at proper place in sorted array 1 3 5 6 8 7 2 4 1 3 5 6 7 8 2 4 Sorted Unsorted Sorted Unsorted
  • 39. Insertion Sort Example Cont. Pass - 7 : Select Seventh record and Insert at proper place in sorted array 1 2 3 5 6 7 8 4 Pass - 8 : Select Eighth Record and Insert at proper place in sorted array Sorted Unsorted 1 3 5 6 7 8 2 4 1 2 3 5 6 7 8 4 1 2 3 5 6 7 8 4 Sorted Unsorted