Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Data Structures UNIT: SORTING & SEARCHING By :Sneha Patel

 SORTING:-

 Sorting is the operation of arranging the records of a table into some sequential order
according to an ordering criterion (condition). Sorting is performed on internal & external
memory. It is classified into two categories:
1. Internal Sort: All data items reside in main memory
2. External Sort: This method is very useful when the volume of the data is very large & at
a time can not fit all the data items into a computer memory.
 The sort is performed according to the key value of each record. Records can be sorted either
numerically or alphanumerically. In numerical sorting, the records are arranged in ascending or
descending order according to the numerical value of the key.
 The complexity of sorting algorithm measures the running time as a function of the number of
items n to be sorted. So the complexity function measures only the number of comparisons.
 There are various types of internal sorting: bubble sort, selection sort, insertion sort, heap sort,
radix sort, shell sort, merge sort, and quick sort.

Sorting

Selection Sort
Bubble Sort
Insertion Sort Quick Sort
Divide & Conquer Methods
Heap Sort Merge Sort
Shell Sort

 The efficiency of any algorithm can be determined by three different case of a table.
a) Best Case:
The case occurs when sorting techniques have minimum number of comparison occurs.
b) Worst Case:
The case occurs when sorting techniques have maximum number of comparison occurs.
c) Average Case:
The case occurs when sorting techniques have average number of comparison occurs.

1) Selection Sort :-

The selection sort starts from first element & search the entire list until it find the minimum
value of the list then the selection sort will place the minimum value at the first place, then select
the second element & search for the 2nd smallest element from the list & this process will be
continue until the whole list will be sorted. From the algorithm it is clear that the algorithm
consume most of its time in the inner loop during the current pass. Outer loop will be (size-

BMU-BMCCA Page 1 of 15
Data Structures UNIT: SORTING & SEARCHING By :Sneha Patel

current) & inner loop will be (size-current-1) comparison. Time required for this algorithm is
E(n)=O(n2).

Algorithm:
Selection_sort (a, n)
a = list of element (array)
n = size of list of element

Step-1: [Repeat step-2 to 5]


For i = 0 to n-1
Step-2: [stored i value in variable min & compared with rest of element in array]
min = a[i]
loc = i
Step-3: [Repeat step- 4]
For j = i + 1 to n
Step-4: [find out the minimum value in the array & its location from i+1th position]
if min > a[j] then
min = a[j]
loc = j
Step-5: [Interchange the element]
temp = a[i]
a[i] = a[loc]
a[loc] = temp
Step-6: Exit

Example: - 44 33 55 22 11

BMU-BMCCA Page 2 of 15
Data Structures UNIT: SORTING & SEARCHING By :Sneha Patel

2) Bubble Sort :-

 It involves sorting an array in a repetitive manner.


 In this sort instead of finding the smallest record and the performing an interchange, two
elements are interchanged immediately upon discovering that they are out of order.
 When this approach is used, there is at most n-1 passes required.
 During 1st pass K1 & K2 are compared & if they are out of order then records r1 & r2 are
interchanged. This process is repeated for n records.
 This method will cause records with small key to move or “bubble up”.
 This method will move largest elements after first pass. It will be replaced in the nth position.

Analysis of the bubble sort:-

 The best case involves performing one pass which requires n-1 comparisons the best case is
O(n).
 The worst case performance of the bubble sort is n(n-1)/2 comparisons & n(n-1)/2
interchanges.
 The average case is more difficult to analyze than the other case. the average case analysis is
O(n2).

Algorithm:
Bubble_Sort (a, n)
a = list of element (array)
n = size of list of element

Step-1: [Repeat step-2 to 3]


For i = 0 to n-1
I will be increment by 1 after every iteration i = 0 to n-1
Step-2: [Repeat step- 3]
For j = 0 to n-1-i
J will be incremented by 1 after every iteration j = 0 to n-1-i
Step-3: [Interchange the element]
if a[j] > a[j+1] then
temp = a[j]
a[j] = a[j+1]
a[j+1] = temp
Step-4: Exit

BMU-BMCCA Page 3 of 15
Data Structures UNIT: SORTING & SEARCHING By :Sneha Patel

 Consider Example for array of 5 elements :

5 2 6 3 9

0 1 2 3 4

 There are 5 elements, there will be 3 iterations. In the first iteration a[0] is compared with
a[1],a[1] is compared with a[2],…a[3] is compared with a[4] & each time we interchange the
values if a[j] >a[j+1] i.e. we try to bring the largest value in the array to the last position in
array.
Step 1:
5 2 6 3 9
2 5 6 3 9
2 5 6 3 9
2 5 3 6 9

Step 2:
2 5 3 6 9
2 3 5 6 9
2 3 5 6 9

Step 3:
2 3 5 6 9

 Example 2: 80 70 44 55 11 15

Step 1:
70 80 44 55 11 15
70 44 80 55 11 15
70 44 55 80 11 15
70 44 55 11 80 15
70 44 55 11 15 80
Step 2:
44 70 55 11 15 80
44 55 70 11 15 80
44 55 11 70 15 80
44 55 11 15 70 80
Step 3:
44 55 11 15 70 80
44 11 55 15 70 80
44 11 15 55 70 80
Step 4:
11 44 15 55 70 80
11 15 44 55 70 80
Step 5:
11 15 44 55 70 80

BMU-BMCCA Page 4 of 15
Data Structures UNIT: SORTING & SEARCHING By :Sneha Patel

3) Insertion Sort :-

 Insertion sort is also one that sorts a set of records by inserting records into an exiting sorted
file.
 Suppose A is the list of element & n is no of elements. We 1st scan all the elements in array
from A[1] to A[n].Inserting each element in its proper position in the previous sorted sub list.

 Algorithm can be explained through following step:


1. A[1] by itself trivial sorted.
2. A[2] is inserted before or after A[1] so that A[1] & A[2] will be sorted.
3. A[3] is inserted into its proper place in A[1],A[2]. So A[3] may be inserted in between
A[1] & A[2] or before A[1] or after A[2] so at last a[1],A[2],A[3] will be sorted. This
will be to continue up to n for smaller value of n.
 This algorithm is very useful. Time required for this algorithm is: E(n)=1+2……+n = O(n2)
 Consider Example for array of 5 elements :

7 2 6 3 9

0 1 2 3 4

 There are 5 elements, there will be 3 iterations. In the first iteration a[1] is compared with
a[0]. If a[1] > a[0] ,then no interchange otherwise interchanging will be take place. In second
iteration, a[2] is compared with a[1] & a[1] with a[0]. Again if a[2] > a[1], then no
interchange otherwise interchanging will be take place

Example 1:

7 2 6 3 9 (Comparison R → L)

Step 1:
7 2 6 3 9 (here 2 <7 =True then interchange 2 & 7)
2 7 6 3 9
Step 2:
2 7 6 3 9
2 6 7 3 9
2 6 7 3 9

Step 3:
2 6 7 3 9
2 6 3 7 9
2 3 6 7 9
2 3 6 7 9 It’s sorted array

BMU-BMCCA Page 5 of 15
Data Structures UNIT: SORTING & SEARCHING By :Sneha Patel

Example 2:

80 70 44 55 11 15

Step 1:
70 80 44 55 11 15
70 44 80 55 11 15
70 44 55 80 11 15
70 44 55 11 80 15
70 44 55 11 15 80
Step 2:
44 70 55 11 15 80
44 55 70 11 15 80
44 55 11 70 15 80
44 55 11 15 70 80
Step 3:
44 55 11 15 70 80
44 11 55 15 70 80
44 11 15 55 70 80
Step 4:
11 44 15 55 70 80
11 15 44 55 70 80
Step 5:
11 15 44 55 70 80

Algorithm:

Insertion_Sort (a, n)

a = list of element (array)


n = size of list of element

Step-1: [Repeat step-2 to 3]


For i = 1 to n-1 [I will be incremented by 1 after every iteration i = 1 to n-1]
Step-2: [Repeat step- 3]
For j = i to 0 (J will be decremented by 1 after every iteration j = i to 0)
Step-3: [Interchange the element]
if a[j] < a[j-1] then
temp = a[j]
a[j] = a[j-1]
a[j-1] = temp
Step-4: Exit

BMU-BMCCA Page 6 of 15
Data Structures UNIT: SORTING & SEARCHING By :Sneha Patel

4) Quick Sort :-

 It is also called partition exchange sort method. When sort is begins, it selects the Lists any
one element which is called pivot element. At each step, the goal is to place a pivot element
in its final position in a list.
 The sort then divides the list into two sub lists, one with elements that are less than pivots
elements element and a second list with elements are greater then or equal to pivot elements.
So this technique partition the list into two sub list & repeat until all elements in a list are
placed in their final position. So it is called partition exchange sort method (quick sort).

Example: Consider A as table that contain N elements, chose an element which works as a pivot
element (we can select the first element as pivot element).
For first pass, a pivot elements is set at place j element of list A. where
1) Each element from j-1 is smaller than pivot element.
2) Each element from j+1 is greater than pivot element.
So whole table or list is divided into two sub list, than each separate sub lists are further process
& then set a pivot to sub list & further divide into sub lists, this process can repeat until whole
table is sorted.
The recursive function or algorithm consists of four steps:
1) If there are one or less elements in array to be sorted. Return immediately.
2) Select an element in the array to serve as a “pivot” point.
3) Divide the array into two sub lists-list one is smaller than pivot & second list is grater
than pivot.
4) Recursively repeat the algorithm for both parts of the original array

Example: Array a[ ] consist of 8 element:- 66 22 77 44 99 55 70 88

If (a[i] < a[pivot]) If (a[j] > a[pivot]) If ( i < j ) else


i++; j--; a[i] a[j] a[j] a[pivot]

66 22 77 44 99 55 70 88 L→R
i

66 22 77 44 99 55 70 88 L→R & Stop at Larger value than 66(Pivot)


i

66 22 77 44 99 55 70 88 R→L
i j

66 22 77 44 99 55 70 88 R→L
i j

BMU-BMCCA Page 7 of 15
Data Structures UNIT: SORTING & SEARCHING By :Sneha Patel

66 22 77 44 99 55 70 88 R→L & stop at smaller value than 66(Pivot)


i j

66 22 77 44 99 55 70 88 Compare the value of i& j


i j if ( i < j )then Interchange the i& j

66 22 55 44 99 77 70 88 Interchange ( a[i] a[j] )


i j

66 22 55 44 99 77 70 88 L→R
i j

66 22 55 44 99 77 70 88 L→R & Stop at Larger value than 66(Pivot)


i j

66 22 55 44 99 77 70 88 R→L
i j

66 22 55 44 99 77 70 88 R→L & Stop at Smaller value than 66(Pivot)


j i Compare the value of j & pivot no

44 22 55 66 99 77 70 88 Interchange (a[j] a[pivot])

Now the original Key set has been partitioned into sub-tables, namely, the sets {44, 22, 55} &
{99, 77, 70, 88}.The same process can be applied to each of these sets until the table is
completely sorted.

44 22 55 66 99 77 70 88

Same process repeat in sublist-1 & sublist-2. (Solve it)

Algorithm:
Quick_Sort (a, LB, UB)
a = list of elements
LB = lower bound of sub list
UB = upper bound of sub list
Step-1: [Initialize]
flag = 1
Step-2: [Repeat Step-3 to 5]
If(LB < UB) then
{
i = LB
j = UB
pivot = LB

BMU-BMCCA Page 8 of 15
Data Structures UNIT: SORTING & SEARCHING By :Sneha Patel

Step-3: [Repeat Step-3 to 4]


[Compare the lower or higher value with pivot value and increment lower & decrement
the higher value]
Repeat While (flag = 1)
{
i=i+1;
Repeat while (a[i] < a[pivot]) [scan from left to right]
i++
Repeat while (a [j] > a[pivot]) [scan from Right to left]
j--;
(Interchange the value i & j)
If i < j then
temp = a[i]
a[i] = a[j]
a[j] = temp
else
flag = 0
}//End of while loop
Step-4: [Interchange Record for pivot number]
temp = a[j]
a[j] = a[pivot]
a[pivot] = temp
Step-5: Quick_sort (a, LB, j-1) [sort first left sub list]
Quick_sort (a, J+1, UB) [sort second right sub list]
}//End of IF loop
Step-6: Return & Exit

5) Merge Sort :-

 It requires two sorted table which can be combined to produce a single sorted table. this
process can be accomplished easily by successively selecting the record with the smallest key
occurring in either of the tables & placing the record in a new table, there by creating an
order list.
 List A is a list of N elements & List B is a list of M elements that operation that combined
the elements of list A & list B into a single resulting list size is R which represent N+M. This
type of sort is called “Merge Sort”.

BMU-BMCCA Page 9 of 15
Data Structures UNIT: SORTING & SEARCHING By :Sneha Patel

 If the element of list A & list B are in sorted order than we may get resulting list in sorted
array. If the list A & B are not in sorted order we can make them in sorted order & than both
the list can be merge. It is also possible that 1st merging of A & B and then the sort merge
resulting list in the last execution time is less in this O(r) (order of r)= n + m.
Example:

List A:10 5 20 30 40
List B: 20 50 60 7 4 5 6

Both list are in unsorted order so first we can sort both list A & B and then after merger both list.

List A:5 10 20 30 40 n=5 i=5


List B: 4 5 6 7 20 50 60 m=7 j=5

The Single Resulting list is R

List R: 4 5 6 7 10 20 30 40 50 60

Algorithm:
Merge_Sort (list a, n, list b, m)

Step-1: [Initialize]
i = 0, j = 0, k = 0
Step-2: [Repeat step-3 to 4]
while (i < n && j < m) (5<5 && 5<7) --F
Step-3: [Compare the value of list a & b and insert into result list of k]
If list a[i] < list b[j] then (5<5) -F
Result[k] = list a[i]
i++;
k++;
Else If list b[j] < list a[i] then 5<5)
Result[k] = list b[j]
j++;
k++;
If list a[i] = = list b[j] then
Result[k] = list a[i]
i++;
j++’
k++;

BMU-BMCCA Page 10 of 15
Data Structures UNIT: SORTING & SEARCHING By :Sneha Patel

Step-4: [size of List1 is larger than List2]


If i < n then
Repeat For c = i to n-1
Result[k] = list a[i]
i++;
k++;
[size of List2 is larger than List1]
Else If j < m then
Repeat For c = j to m-1
Result[k] = list b[j]
j++;
k++;
Step-5: Exit

BMU-BMCCA Page 11 of 15
Data Structures UNIT: SORTING & SEARCHING By :Sneha Patel

 SEARCHING:-

 Searching refers to the operation of finding the location of given item in a collection of items.
 A table or file is a collection of elements, each of which is called a record.
 Search operation allows efficient retrieval of specific items from a set of items.
Types of Searching:-
1. Linear or sequential Search
2. Binary Search
3. Index Search
4. Hash Search

1) Linear Search:-
 It is known as Sequential search. It is simplest technique for searching an item from unsorted
list.
 It scans each entry in sequential manner until the desired item is found.
 In other words, search that looks through a list from the top to bottom while checking each item
with key fields for a match is know as a sequential search or linear search.
Algorithm:

Linear_search(a, n, k)

a=list of elements
n=no of elements in the list
k=value to be searched in the list

Step-1: [Initialize Search]


i=0
Step-2: [Search the array for particular key]
for i = 0 to n
if (a[i] = = k)
break;
go to step -3
Step-3: [Successful search or not]
if (i = = n) then
Print “Unsuccessful Search”;
return(0);
else

BMU-BMCCA Page 12 of 15
Data Structures UNIT: SORTING & SEARCHING By :Sneha Patel

print “Successful Search”;


return(i);
Step-4: Exit
Example: Search Key 65 from this array

I 0 1 2 3 4 5
0 6 23 39 14 65 18 65 compare with 6
1 6 23 39 14 65 18 65 compare with 23
2 6 23 39 14 65 18 65 compare with 39

3 6 23 39 14 65 18 65 compare with 14

4 6 23 39 14 65 18 65 compare with 65

2) Binary Search:-
 The sequential search is very slow. if we have an array of 1000 elements, we must do 1000
comparison in the worse case.
 If the array is not sorted, the sequential search is only alternative.
 If array is sorted, we can use more efficient algorithm called binary search.
 A search for a particular item with a certain key value resembles the search for a name in a table.
 The approximate middle entry of the table is located, and its middle value is examined with the
search key value.
 If its key value is low, the key value of the middle entry of first half of the table is examined &
the procedure is repeated on the first half until the required item is found.
 If its key value is too high, then the key value of middle entry of second half of table is tried &
the producer is repeated on the second half. This process continues until the desired key is found
or the search interval become empty.
 The major drawback of binary search is that it assumes that one has direct access to the middle
value in the list or a sub list, this means that the list must be sorted in an array is typical task &
requires a lot of shuffling of elements up & down.
Algorithm:
Binary_search(a, n, k)

a=list of elements
n=no of elements in the list
k=value to be searched in the list

BMU-BMCCA Page 13 of 15
Data Structures UNIT: SORTING & SEARCHING By :Sneha Patel

Step-1: [Initialize Search]


low = 0
high = n-1
Step-2: [Perform Search]
Repeat step 3 & 4 while(low < = high)
Step-3: [obtain mid point of interval]
mid = (low + high)/2
Step-4: [Compare with search item]
if (k < a[mid]) then(left)
high = mid - 1
else if (k > a[mid] ) then
low = mid + 1
else
print “Successful Search”
return(mid)
Step-5: [Unsuccessful Search]
Print “Unsuccessful Search”;
Return(0)
Example:

Index 0 1 2 3 4 5 Searching Key value is 27


Data 5 15 23 27 31 48

Iteration Low High Mid

0 0 5 2

1 3 5 4

3 Here successfully search the key


2 3 3
value 27 on position 3

BMU-BMCCA Page 14 of 15
Data Structures UNIT: SORTING & SEARCHING By :Sneha Patel

UNIT:- SORTING & SEARCHING

Short Question Answer (Marks-1 or 2)

1) Give best and worst case complexity of selection sort.


2) Explain through example 2-way merge sort.
3) Write the best, average and worst case complexities of bubble sort and insertion sort.
4) What is internal sort? Give examples.
5) What is external sort? Give examples.
6) What is sorting? List out various types of sorting.
7) What is difference between bubble and selection sort?
8) List out various sorting methods that falls under divide and conquer category.

Long Question Answer (Marks: 5-8)

1) Explain Quick Sort with example.


2) Short note on: Difference between quick sort and bubble sort.
3) Differential between internal sorting and external sorting. Explain selection sort algorithm with
suitable example.
4) Differential between internal sorting and external sorting. Write a program to sort a following
array using Insertion sort.
34, 41, 22, 46, 54, 29, 49, 36, 56, 52
5) Write a note on Divide and Conquer.
6) Write a short note on Insertion Sort
7) Which sorting technique is best among merge, quick and insertion sort? Justify your answer.
OR
Name various sorting methods. Which sorting technique is faster?
8) Write a short note on Merge Sort.
9) Write a short note on Selection sort
10) Write an algorithm to sort element using insertion sort.
11) Write an algorithm to sort elements of given array using quick sort.
12) What is difference between sequential search and binary search? Explain their performance.
13) What is searching? Discuss difference between binary search and liner search. Discuss binary
search algorithm for following example.
Index 1 2 3 4 5 6 7 8 9 10 11 12 13
Element 11 22 30 33 40 44 55 60 66 77 80 88 99
Search out key = 40. Show all the steps of its.

BMU-BMCCA Page 15 of 15

You might also like