DS - Unit-5 - Sorting & Searching
DS - Unit-5 - Sorting & Searching
GTU # 3130702
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 & Example
# Input: Array A, integer key Search for 1 in given array
2 9 3 1 8
# Output: first index of key
in A
# or -1 if not found Comparing value of ith index with element to be search
one by one until we get searched element or end of the
Algorithm: Linear_Search array
Step 1: i=0 Step 1: i=2
for i = 0 to last index of A:
if A[i] equals key:
2 9 3 1 8 2 9 3 1 8
return i
return -1 i i
2 9 3 1 8 2 9 3 1 8
i i
Element found at ith index, i=
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 10 11
Index 0 1 2 3 4 5 6 7 2
8 4
9
left right
Key=6, No of Elements = 10, so left = 0, right=9
Step 1: 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 10 11
Index 0 1 2 3 4 5 6 7 2
8 4
9
left right
Binary Search - Algorithm
Step 2: 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 10 11
Index 0 1 2 3 4 5 6 7 2
8 4
9
left right
Step 3: 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 10 11
Index 0 1 2 3 4 5 6 7 2
8 4
9
Element Found
Sorting
Sorting is a process of arranging the data in a specific order
Ascending order: If the elements are arranged in increasing order then it is
called ascending order.
Descending Order: If the elements are arranged in decreasing order then
it is called descending order.
There are 2 types of sorting.
1. Internal Sorting: in which data resides on the main memory of computer.
2. External Sorting: in which there is huge amount of data and it resides.
The internal sorting techniques are Bubble sort, Selection Sort, Quick Sort,
Merge sort.
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.
Step 1 :
Unsorted Array
5 1 12 -5 16 2 12 14
0 1 2 3 4 5 6 7
Step 2 :
Unsorted Array (elements Min index = 0, value = 5
0 to 7)
1 1 1 1
-5
5 1 -5
5 2 Find min value from
2 6 2 4 Unsorted array
0 1 2 3 4 5 6 7
Index = 3, value = -5
Swap
Selection Sort
Step 3 : Min index = 1, value = 1
Unsorted Array
(elements 1 to 7)
1 1 1 1 Find min value from
-5 1 5 2 Unsorted array
2 6 2 4 Index = 1, value = 1
0 1 2 3 4 5 6 7
Step 4 :
Unsorted Array
(elements 2 to 7)
Min index = 2, value = 12
1 1 1 1 1
-5 1 2 5 2 Find min value from
2 6 2 2 4
0 1 2 3 4 5 6 7 Unsorted array
Index = 5, value = 2
Swap
Selection Sort
Step 5 :
Unsorted Array
(elements 3 to 7)
Min index = 3, value = 5
1 1 1 1
-5 1 2 5 Find min value from
6 2 2 4
0 1 2 3 4 5 6 7 Unsorted array
Index = 3, value = 5
Step 6 :
Unsorted Array
(elements 5 to 7)
Min index = 4, value = 16
1 1 1 1
-5 1 2 5 Find min value from
6
2 2
6 2 4
0 1 2 3 4 5 6 7 Unsorted array
Index = 5, value = 12
Swap
Selection Sort
Step 7 :
Unsorted Array
(elements 5 to 7)
Min index = 5, value = 16
1 1 1 1
-5 1 2 5 Find min value from
2 6
2 2
6 4
0 1 2 3 4 5 6 7 Unsorted array
Index = 6, value = 12
Swap
Step 8 :
Unsorted Array
(elements 6 to 7)
Min index = 6, value = 16
1 1 1 1
-5 1 2 5 Find min value from
2 2 6
4 4
6
0 1 2 3 4 5 6 7 Unsorted array
Index = 7, value = 14
Swap
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)
1. [Loop on the Pass index]
Repeat thru step 4 for PASS = 1,2,…….., N-1
2. [Initialize minimum index]
MIN_INDEX PASS
3. [Make a pass and obtain element with smallest value]
Repeat for I = PASS + 1, PASS + 2, …………….., N
If K[I] < K[MIN_INDEX]
Then MIN_INDEX I
4. [Exchange elements]
IF MIN_INDEX <> PASS
Then K[PASS] K[MIN_INDEX]
5. [Finished]
Return
Bubble Sort
Bubble sort is a simple sorting algorithm.
If the given array has to be sorted in ascending order, then bubble sort will start
by comparing the first element of the array with the second element, if the first
element is greater than the second element, it will swap both the elements, and
then move on to compare the second and the third element, and so on.
If we have total n elements, then we need to repeat this process for n-1 times.
Bubble Sort
It is known as bubble sort, because with every complete iteration the
largest element in the given array, bubbles up towards the last place or
the highest index, just like a water bubble rises up to the water surface.
Sorting takes place by stepping through all the elements one-by-one and
comparing it with the adjacent element and swapping them if required.
This algorithm is not suitable for large data sets as its average and worst
case complexity are of Ο(n2) where n is the number of items.
Bubble Sort
Unsorted Array
45 34 56 23 12
swap
swap
swap
5
4
3 4 4 4 4 4
2 4
2 4
3
2 3
1 3
2
1
swap
swap
4
5 5 5
2 swap 5
2 5
2 5
3
2
4 3
4
1 3
4
1 4
2
1
3 2
3
swap
6
2 6
2 6
3
2
5 3
5
1 3
1 3
5
1 5
2
1
4 2
4 2
4 4
swap
3
1 3
1 3
6
1 6
2
1
5 2
5 2
5 2
5 5 5 5
2 2 2 2
6 6 6 6 6 6 6
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)
1. [Initialize]
LAST N
2. [Loop on pass index]
Repeat thru step 5 for PASS = 1, 2, 3, …. , N-1
3. [Initialize exchange counter for this pass]
EXCHS 0
4. [Perform pairwise comparisons on unsorted elements]
Repeat for I = 1, 2, ……….., LAST – 1
IF K[I] > K [I+1]
Then K[I] K[I+1]
EXCHS EXCHS + 1
5. [Any exchange made in this pass?]
IF EXCHS = 0
Then Return (Vector is sorted, early return)
ELSE LAST LAST - 1
6. [Finished]
Return
Quick Sort
Quick sort is a sorting algorithm that uses the divide and conquer strategy.
In this method division is dynamically carried out.
The three steps of quick sort are as follows:
Divide :
Split the array into two sub arrays that each element in the Elements that are
Pivot element Elements that are left sub array is less than or equal the less
than pivot greater than pivot middle element and each element in the right
sub array is greater than the middle element.
The splitting of the array into two sub arrays is based on pivot element.
All the elements that are less than pivot should be in left sub array and all
the elements that are more than pivot should be in right sub array.
Quick Sort
Conquer : Recursively sort the two sub arrays.
Combine : Combine all the sorted elements in a group to form a list of
sorted elements.
Mid
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
The quick sort algorithm is performed using following two important
functions -Quick and partition.
Algorithm Quick(A[O...n-1),low, high)
Problem Description : This algorithm performs sorting of the elements
given in Array A[O...n-1]
Input: An array A[O...n-1) in which unsorted elements are given. The low
indicates the leftmost element in the list and high indicates the rightmost
element in the list
Output: Creates a sub array which is sorted in ascending order
if(low <high) then split the array into two sub arrays
M partition(A[low...high]) // m is mid of the array
Quick(A[low...m-1])
Quick Sort
In above algorithm call to partition algorithm is given.
The partition performs arrangement of the elements in ascending order.
The recursive quick routine is for dividing the list in two sub lists.
The pseudo code for Partition is as given below
Quick Sort
Algorithm Partition (A[low...high])
Problem Description: This algorithm partitions the subarray using the first
element as pivot element
Input: A subarray A with low as left most index of the array and high as
the rightmost index of the array.
Output: The partitioning of array A is done and pivot//occupies its proper
position and the rightmost index of the list is returned
Quick Sort
Pivot A[low] Swap (A[i],A[j]) // Swap A[i] and A[j]
I low }
J high+1 Swap (A[low],A[j]) // when I crosses j swap
while(i<=j) do A[low] and A[j]
{
while(A[i]<=pivot) do Return j
I i+1
while(A[j]>=pivot) do
J j-1;
if(i<=j) then
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
72 52 52 18 45
2 98 31
4 1 9 9 1
0 1 2 3 0 1 2 3
Merge Sort
Step: Select the left subarray, Split the selected array (as evenly as possible)
0 1 2 3 0 1 2 3
72 52 52 18 45
2 98 31
4 1 9 9 1
0 1 0 1 0 1 0 1
72 52 52 18 45
2 98 31
4 1 9 9 1
0 0 0 0 0 0 0 0
72 52 52 18 45
2 98 31
4 1 9 9 1
52 72 52 18 45
2 98 31
1 4 9 9 1
52 72 18 45 52
2 98 31
1 4 9 1 9
18 45 52 52 72
2 31 98
9 1 1 9 4
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.
6 5 3 1 8 7 2 4
Pass - 1 : Select First Record and considered as Sorter Sub-array
6 5 3 1 8 7 2 4
Sorted Unsorted
Pass - 2 : Select Second Record and Insert at proper place in sorted array
6 5 3 1 8 7 2 4
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.
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
1 3 5 6 8 7 2 4
Sorted Unsorted
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
Insertion Sort Example Cont.
Pass - 7 : Select Seventh record and Insert at proper place in sorted array
1 3 5 6 7 8 2 4
1 2 3 5 6 7 8 4
Sorted Unsorted
Pass - 8 : Select Eighth Record and Insert at proper place in sorted array
1 2 3 5 6 7 8 4
1 2 3 4 5 6 7 8
Sorted Unsorted
Data Structures (DS)
GTU # 3130702
Thank
You