0% found this document useful (0 votes)
79 views151 pages

UNIT 2 Searching and Sorting Techniques

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views151 pages

UNIT 2 Searching and Sorting Techniques

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 151

UNIT 2 SEARCHING

TECHNIQUES
Sorting and Searching
 Fundamental problems in computer
science and programming
 Sorting done to make searching easier
 Multiple different algorithms to solve the
same problem
 How do we know which algorithm is
"better"?
Need for Searching &
Sorting
 There are some very common problems
that we use computers to solve:
 Searching through a lot of records for a
specific record or set of records
 Placing records in order, which we call sorting
 There are numerous algorithms to perform
searches and sorts.
Examples of Searching
 Finding telephone number in directory
 Search a book in library
 Searching a particular customer record
Examples of sorting

 Sorting Books in Library


 Sorting Individuals by Height (Feet and Inches)
 Sorting songs(Alphabetical)
 Arranging folders in order
 Sorting Numbers (Sequential)
Searching
 Searching refers to the operation of
finding the location of a given item or
“not found” in a collection of items.
 The search is said to be successful if

ITEM does appear in DATA and


unsuccessful otherwise.
 Examples

1. Sequential Searching
2. Binary Search
SEARCHING METHODS
1. Sequential/Linear Search
 The array contains the following element
5 7 2 10 15
a[0] a[1] a[2] a[3] a[4]

Now ITEM=2

Algorithm-
1.Give the name to element in the array as a[0] To
a[4]
2.Compare each element in the array with ITEM.
3.If ITEM=number
then search is successful.
else
search is unsuccessful.
 Three possibilities of finding element in array-
1.A ITEM found in 1st location.
2.A ITEM found in middle location.
3.A ITEM found in last location
Complexity-
1.If ITEM found in 1st location or position
then it is BEST Complexity.
2.If ITEM not found in list
then it is WORST Complexity.
Time Complexity-
This algorithm needs n+1
comparisons where n is the no. of records to be
searched.
ie. Time complexity is O(n).
 linear search
 start at first item
 is it the one I am looking for?
 if not go to next item
 repeat until found or all items checked
 If items not sorted or unsortable this
approach is necessary
Linear Search Algorithm

Set found to false


Set position to –1
Set index to 0
While index < number of elts and found is
false
If list [index] is equal to search value
found = true
position = index
End If
Add 1 to index
End While
Return position
Linear Search Example
 Array numlist contains

17 23 5 11 2 29 3
 Searching for the value 11, linear search
examines 17, 23, 5, and 11
 Searching for the value 7, linear
search examines 17, 23, 5, 11, 2,
29, and 3
Efficiency of the Linear
Search
 The advantage is its simplicity.
 It is easy to understand
 Easy to implement
 Does not require the array to be in order
 The disadvantage is its inefficiency
 Inefficient (slow): for array of N elements,
examines N/2 elements on average for value
that is found in the array, N elements for value
that is not in the array
 If there are 20,000 items in the array and what
you are looking for is in the 19,999th element,
you need to search through the entire list.
int linsearch(S st[],int rollno,int n)
{
int i, comp=0;
for(i=0;i<n ; i++)
{
comp++;
if(rollno==st[i].rollno)
{
cout<<“\nNo. of comparisons = %d ",comp;
return(i);
}
}
cout<<"\nNo. of comparisons” <<comp;
return(-1);
}
2. Binary Search
 The binary search is much more efficient
than the linear search.
 It requires the list to be in order.
 The algorithm starts searching with the
middle element.
 If the item is less than the middle element, it starts over
searching the first half of the list.
 If the item is greater than the middle element, the
search starts over starting with the middle element in
the second half of the list.
 It then continues halving the list until the item is
found.
Binary Search Algorithm

1. Divide a sorted array into three sections:


 middle element
 elements on one side of the middle element
 elements on the other side of the middle element
2. If the middle element is the correct value,
done. Otherwise, go to step 1, using only the
half of the array that may contain the correct
value.
3. Continue steps 1 and 2 until either the value is
found or there are no more elements to
examine.
Binary Search Example

 Array numlist2 contains

2 3 5 11 17 23 29
 Searching for the the value 11, binary
search examines 11 and stops
 Searching for the the value 7, binary
search examines 11, 3, 5, and stops
Binary Search Tradeoffs
 Benefit
 Much more efficient than linear search
(For array of N elements, performs at most
log2N comparisons)
 Disadvantage
 Requires that array elements be sorted
 Write program for binary search with
and without recursion.
SORTING METHODS
Sorting
 Sorting refers to arranging of data
elements in some given order.
 2 types:
 Internal Sorting
 External Sorting
Internal Sorting
 Internal Sorting takes place in the main
memory of a computer. The internal
sorting methods are applied to small
collection of data. It means that, the
entire collection of data to be sorted is
small enough that the sorting can take
place within main memory.
External Sort
 The External sorting methods are applied
only when the number of data elements
to be sorted is too large. These methods
involve as much external processing as
processing in the CPU.
 when sorting is done on data lying on
secondary devices then it is called
external sorting.
 we will normally use this method for
sorting elements.
 Internal Sort
 The data to be sorted is all stored in the
computer’s main memory.
 External Sort
 Some of the data to be sorted might be
stored in some external, slower, device.
Sort stability
 The sorting stability means comparing
the records of same value & expecting
them in the same order even after
sorting them.
Stable sort algorithms
 A STABLE sort
preserves relative
order of records with An An
98 98
equal keys n n
Bo
90 Joe 98
 A stable sort keeps b
Da Bo
equal elements in n
75
b
90
the same order Joe 98
Sa
90
m
 This may matter
Pat 86 Pat 86
when you are sorting
Sa
data according to m
90 Zöe 86
some characteristic Da
Zöe 86 75
n
 Example: sorting
students by test original stably
array sorted
scores
Unstable sort
algorithms
An
 An unstable n
98 Joe 98
sort may or Bo
90
An
98
b n
may not keep Da Bo
75 90
equal elements n b
Sa
in the same Joe 98 90
m
order Pat 86 Zöe 86
 Stability is Sa
90 Pat 86
m
usually not Zöe 86
Da
75
important, but n
original unstabl
sometimes it is array y
important sorted
General concepts:
 Sort order:
i.Ascending order
ii.Descending order.
Efficiency:
i. Efficiency of sorting algorithms is denoted in
terms of time complexity(i.e. How much time that
algorithm have taken to sort elements.)
ii. Time complexities are given in terms of big-oh
notations.
iii. Sorting techniques Commonly have O(n2) and
O(nlogn)
time complexities.
iv. Bubble, insertion, selection sort have time
complexities O(n2).
v. Merge sort & quick sort have complexities
O(nlogn)
vi. Where n contains number of records to be
sorted.
passes:
 While sorting the elements in some order there
are lot of arrangements of elements.
 The number of times we are visitting the same
elements
 The phases in which the elements are moving
to acquire their proper position is called passes.

 Ex.10,30,20,50,40
 Pass1:10,20,30,40,50
 Pass2:10,20,30,40,50
Sorting Techniques
1. Bubble sort
2. Selection sort
3. Insertion sort
4. Merge sort
5. Quick sort
6. Bucket sort
1.Bubble Sort
 Compares adjacent array elements and
exchanges their values if they are out of
order
 Smaller values bubble up to the top of
the array and larger values sink to the
bottom
Bubble sort

 This method compares neighboring


elements and swapping values at their
positions if necessary
 Ex.45 -40 190 99 11 sort numbers in

ascending order.
 For sorting n numbers n-1 passes are

needed.
Pass1:-40 45 99 11 190
Pass2:-40 45 11 99 190
Pass3:-40 11 45 99 190
Pass4:-40 11 45 99 190
Analysis of Bubble Sort
 Works best when array is nearly sorted
to begin with.
 Worst case number of comparisons is
O(n*n)
 Worst case number of exchanges is
O(n*n)
 Best case occurs when the array is
already sorted
 O(n) comparisons
 int main()
 {
 int a[20], i, n;
 int ch;
 cout<<“ Enter the number of elements u
want to sort";
 cin>>n;
 cout<<“ Enter the elements for the array \
n”;
 for(i=0;i<n; i++)
 cin>>a[i];
 bubblesort(a,n);
 return 0;
 }
 void bubblesort(int a[20], int n)
 {
 int i ,j ,temp;
 for(i=0;i<n-1;i++)
 {
 for(j=0;j<n-1;j++)
 {
 if(a[j]>a[j+1])
 {
 temp=a[j];
 a[j]=a[j+1];
 a[j+1]=temp;
 }
 }
 }
 cout<<“sorted nos. are\n”;
 for(i=0;i<n;i++)
 cout<<a[i];
 }
2. Selection Sort
 Selection sort is a relatively easy to
understand algorithm
 Sorts an array by making several passes
through the array, selecting the next
smallest item in the array each time and
placing it where it belongs in the array
 Efficiency is O(n*n)
 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
 Search elements 2 through n-1 and select the smallest
 Swap it with the element in location 2
 Search elements 3 through n-1 and select the smallest
 Swap it with the element in location 3
 Continue in this fashion until there’s nothing left to
search
Selection sort

 Here element at first position of an array


is compared with all the remaining
elements.
 No. of swappings are reduced.
 Ex. 45 -40 190 99 11
 Pass1:-40 45 190 99 11
 Pass2:-40 11 190 99 45
 Pass3:-40 11 45 190 99
 Pass4:-40 11 45 99 190
 Pass4:-40 11 45 99 190
Selection Sort (continued)
 Selection sort is called a quadratic sort
 Number of comparisons is O(n*n)

void selectsort(int a[20],int n)

{

int i ,j ,temp;

for(i=0;i<n-1;i++)

{

for(j=i+1; j<n ; j++)

{

if(a[i]>a[j])

{

temp=a[i];

a[i]=a[j];

a[j]=temp;

}

}

}

cout<<“sorted nos. are\n";

for(i=0;i<n;i++)

cout<<a[i];

}
Selection Sort
int main()
{
int a[20], i, n;
int ch;

cout<<“Enter the number of element in the sorted array";


cin>>n;
cout<<“Enter the elements for the array “;
for(i=0;i<n;i++)
cin>>a[i];
selectsort(a,n);
return 0;
}
Insertion sort:
 In this method element’s are inserted at
their appropriate places.
 Ex. 45 -40 190 99 11

43
0 1 2 3 4
 a 45 -40 190 99 11
 b 0 0 0 0 0
Step1:a 45 -40 190 99 11
b 45 0 0 0 0

Step2:a 45 -40 190 99 11


b -40 45 0 0 0

Step3:a 45 -40 190 99 11


b -40 45 190 0 0

Step4:a 45 -40 190 99 11


b -40 45 99 190 0

Step5:a 45 -40 190 99 11


44
b -40 11 45 99 190
Insertion sort example

45
Insertion sort example 2
Names: Fred, Alice, David, Bill, and Carol

46
Insertion sort example 2

47
Insertion sort example 2

48
 void insertsort(int a[20],int n)
 {
 int i ,k ,y;

 for(k=1;k<n;k++)
 {
 y=a[k];
 for(i=k-1;i>=0 && y<a[i];i--)
 {
 a[i+1]=a[i];
 a[i]=y;
 }
 }

 cout<<“sorted nos. are\n";


 for(i=0;i<n;i++)
 cout<<a[i];
 } 49
 int main()
 {
 int a[20],i,n;
 int ch;
 cout<<“ Enter the number of element in the sorted
array";
 cin>>n;
 cout<<"\n Enter the elements for the array \n";
 for(i=0;i<n;i++)
 cin>>a[i];
 insertsort(a,n);
 return 0;
 }
50
Merge sort
 merge sort: This method divides the list in half
until each sub-list has one element, then
recombining

 more specifically:
 divide the list into two roughly equal parts
 recursively divide each part in half, continuing until a
part contains only one element
 merge the two parts into one sorted list
 continue to merge parts as the recursion unfolds

 This is a "divide and conquer" algorithm.


51
Merge sort
 Merge sort idea:
 Divide the array into two halves.
 Recursively sort the two halves (using merge sort).
 Use merge to combine the two arrays.

mergeSort(0, n/2-1) mergeSort(n/2, n-1)

sort sort
merge(0, n/2, n-1)

52
98 23 45 14 6 67 33 42

53
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

54
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

55
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

56
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

Merge

57
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

23
Merge

58
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

23 98
Merge

59
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98

60
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98

Merge

61
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14

Merge

62
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

Merge

63
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

Merge

64
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14

Merge

65
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14 23

Merge

66
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14 23 45

Merge

67
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14 23 45 98

Merge

68
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

23 98 14 45

14 23 45 98

69
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45

14 23 45 98

70
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45

14 23 45 98 Merge

71
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45 6

14 23 45 98 Merge

72
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45 6 67

14 23 45 98 Merge

73
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67

14 23 45 98

74
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67

14 23 45 98 Merge

75
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33

14 23 45 98 Merge

76
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 Merge

77
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98

Merge

78
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6

Merge

79
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33

Merge

80
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42

Merge

81
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

Merge

82
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

Merge
83
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

Merge
84
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14

Merge
85
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23

Merge
86
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33

Merge
87
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42

Merge
88
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45

Merge
89
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67

Merge
90
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67 98

Merge
91
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67 98

92
98 23 45 14 6 67 33 42

6 14 23 33 42 45 67 98

93
Merge sort example 2
13 6 21 18 9 4 8 20
0 7

13 6 21 18 9 4 8 20
0 3 4 7

13 6 21 18 9 4 8 20
0 1 2 3 4 5 6 7

13 6 21 18 9 4 8 20
0 1 2 3 4 5 6 7

6 13 18 21 4 9 8 20
0 1 2 3 4 5 6 7

6 13 18 21 4 8 9 20
0 3 4 7

4 6 8 9 13 18 20 21
0 94 7
Quick sort
 quick sort: orders a list of values by
partitioning the list around one element
called a pivot, then sorting each partition
 more specifically:
 choose one element in the list to be the pivot
(= partition element)
 organize the elements so that all elements less
than the pivot are to its left and all greater are
to its right
 apply the quick sort algorithm (recursively) to
both partitions
95
Quick sort illustrated
pick a pivot

40 18 37 2
10 32 6 35
12 17
partition
6 17 40 37
10 12 2 18 32 35

quicksort quicksort
2 6 10 12 17 18 32 35 37 40

combine
2 6 10 12 17 18 32 96
35 37 40
Quick Sort

0 1 2 3 4 5 6 7
25 57 48 38 10 91 84 33

Step 1- Set a[0] th element ie 25 as pivot element .The


index 0 of array a is lower bound and the index 7 is
the upper bound
Lb
ub

0 1 2 3 4 5 6 7
25 57 48 38 10 91 84 33

pivot
97
 Now find all the element lesser than 25 and place 25 after all the lesser
elements.

10 25
57 48 38 91 84 33
sublist1 pivot
Step2:
As in sub-list 1 only 1 element is present no need to sort it. But the sub-
list2 can be sorted.
For that purpose set new pivot element as 57.find all the lesser than 57
element & place them before 57.

0 1 2 3 4 5 6 7
10 25 48
48 3838 3333 57 91 8484

sublist1 pivot sublist2

98
Step 3:
As again from sublist1 & sublist2 sort the element take sublist1, 48
is new pivot element place all lesser than 48 element before it.

Step 4
0 1 2 3 4 5 6 7
10 25 38 33 48 57 84 91
91

pivot pivot

0 1 2 3 4 5 6 7
10 25 38 33
33 48 57 84 91

Pivot sublist 1 99
Sorted list

0 1 2 3 4 5 6 7
10 25 33 38 48 57 84 91

100
Analysis of sorting Techniques:

Sorting Technique Time Complexity cases)


Average Best Worst
Bubble sort n2 n2 n2
Selection sort n2 n2 n2
Insertion sort n2 n2 n2
Merge sort <=n <=n <=n
Quick sort nlogn nlogn n2

101
Divide-and-Conquer
 Divide and Conquer is a method of algorithm
design.

 This method has three distinct steps:


 Divide: If the input size is too large to deal

with in a straightforward manner, divide the


data into two or more disjoint subsets.
 Recurse: Use divide and conquer to solve the

subproblems associated with the data


subsets.
 Conquer: Take the solutions to the

subproblems and “merge” these solutions


4. Quick-Sort

1) Divide : If the sequence S has 2 or more


elements, select an element x from S to be
your pivot. Any arbitrary element, like the
last, will do. Remove all the elements of S
and divide them into 3 sequences:
L, holds S’s elements less than x
E, holds S’s elements equal to x
G, holds S’s elements greater than x
2) Recurse: Recursively sort L and G
3) Conquer: Finally, to put elements back into
S in order, first inserts the elements of L,
then those of E, and those of G.
Quick-Sort Tree
In-Place Quick-Sort

Divide step: l scans the sequence from the left, and r from the right.

A swap is performed when l is at an element larger than the pivot and r is at one
smaller than the pivot.
In Place Quick Sort
(cont’d)

A final swap with the pivot completes the divide step


Quick Sort Running Time
 Worst case: when the pivot does not divide the sequence
in two
 At each step, the length of the sequence is only reduced
by 1
 Total running time
1

 length
i n
( S i ) O ( n 2
)

 General case:
 Time spent at level i in the tree is O(n)
 Running time: O(n) * O(height)

 Average case:
 O(n log n)
5. Merge-Sort

 Algorithm:
 Divide: If S has at least two elements
(nothing needs to be done if S has zero or
one elements), remove all the elements
from S and put them into two sequences,
S1 and S2, each containing about half of
the elements of S. (i.e. S1 contains the first
n/2 elements and S2 contains the
remaining n/2 elements.
 Recur: Recursive sort sequences S1 and
S 2.
 Conquer: Put back the elements into S by
Merge-Sort Example
Divide and Conquer
1. Base case: the problem is small
enough, solve directly

2. Divide the problem into two or more


similar and smaller subproblems

3. Recursively solve the subproblems

4. Combine solutions to the


subproblems
Divide and Conquer - Sort
• Base case
single element (n=1), return
• Divide A into two subarrays: FirstPart,
SecondPart
Two Subproblems:
• sort the FirstPart
• sort the SecondPart

• Recursively
• sort FirstPart
• sort SecondPart

• Combine sorted FirstPart and sorted


second part
Merge Sort: Idea

Divide into
A: FirstPart SecondPart
two halves
Recursively
sort
FirstPart SecondPart

Merge

A is sorted!
Merge Sort: Algorithm
Merge-Sort (A, n)
if n=1 return
else
Space:
n n1 ← n2 ← n/2
Time: n
create array L[n1], R[n2]
for i ← 0 to n1-1 do L[i] ← A[i]
for j ← 0 to n2-1 do R[j] ← A[n1+j]Call
Recursive
Merge-Sort(L, n1)
Merge-Sort(R, n2)
Merge(A, L, n1, R, n2 )
Merge-Sort: Merge
Sorted

A:

merge

Sorted Sorted

L: R:
Merge-Sort: Merge
Example

A:

L: 1 2 6 8 R: 3 4 5 7
Merge-Sort: Merge
Example

A:
3
1 5 15 28 10 14

k=0

L: R:
3
1 15
2 28
6 30
8 6
3 10
4 14
5 22
7

i= j=0
0
Merge-Sort: Merge
Example

A:
1 2
5 15 28 30 6 10 14

k=1

L: R:
3
1 5
2 15
6 28
8 6
3 10
4 14
5 22
7

i= j=0
1
Merge-Sort: Merge
Example

A:
1 2 3 28 30
15 6 10 14

k=2

L: R:
1 2 6 8 6
3 10
4 14
5 22
7

i= j=0
2
Merge-Sort: Merge
Example

A:
1 2 3 4 6 10 14

k=3

L: R:
1 2 6 8 6
3 10
4 14
5 22
7

i= j=1
2
Merge-Sort: Merge
Example

A:
1 2 3 4 5 6 10 14

k=4

L: R:
1 2 6 8 6
3 10
4 14
5 22
7

i= j=2
2
Merge-Sort: Merge
Example

A:
1 2 3 4 5 6 10 14

k=5

L: R:
1 2 6 8 6
3 10
4 14
5 22
7

i= j=3
2
Merge-Sort: Merge
Example

A:
1 2 3 4 5 6 7 14

k=6

L: R:
1 2 6 8 6
3 10
4 14
5 22
7

i= j=3
3
Merge-Sort: Merge
Example

A:
1 2 3 4 5 5 7 8
14

k=7

L: R:
3
1 5
2 15
6 28
8 6
3 10
4 14
5 22
7

i= j=4
3
Merge-Sort: Merge
Example

A:
1 2 3 4 5 6 7 8

k=8

L: R:
3
1 5
2 15
6 28
8 6
3 10
4 14
5 22
7

i= j=4
4
merge(A,L,n1,R,n2) Number of iterations: (n1+n2)
i ← j ← 0 Total time: c(n1+n2) for some c
for k ← 0 to n1+n2-1
if i < n1
if j = n2 or L[i] ≤ R[j]
A[k] ← L[i]
i ← i + 1
else
if j < n2
A[k] ← R[j]
j ← j + 1
Merge-Sort Execution Example
Divide
6 2 8 4 33 77 55 1
Merge-Sort Execution Example
Recursive call, divide
3 7 5 1

6 2 88 44
Merge-Sort Execution Example
Recursive call, divide
3 7 5 1

8 4

6 2
2
Merge-Sort Execution Example
Recursive call, base case
3 7 5 1

8 4

6
Merge-Sort Execution Example
Recursive call return
3 7 5 1

8 4

6 2
Merge-Sort Execution Example
Recursive call, base case
3 7 5 1

8 4

2
Merge-Sort Execution Example
Recursive call return
3 7 5 1

8 4

6 2
Merge-Sort Execution Example
Merge
3 7 5 1

8 4

2 6
Merge-Sort Execution Example
Recursive call return
3 7 5 1

2 6 8 4
Merge-Sort Execution Example
Recursive , divide
call 3 7 5 1

2 6

8 44
Merge-Sort Execution Example
Recursive call, base case
3 7 5 1

2 6

8
Merge-Sort Execution Example
Recursive call return
3 7 5 1

2 6

8 4
Merge-Sort Execution Example
Recursive call, base case

2 6

4
Merge-Sort Execution Example
Recursive call return
3 7 5 1

2 6

8 4
Merge-Sort Execution Example
merge
3 7 5 1

2 6

4 8
Merge-Sort Execution Example
Recursive call return
3 7 5 1

2 6 4 8
Merge-Sort Execution Example
merge
3 7 5 1

2 4 6 8
Merge-Sort Execution Example
Recursive call return
2 4 6 8 3 7 5 1
Merge-Sort Execution Example
Recursive call
2 4 6 8

3 7 5 1
Merge-Sort Execution Example

2 4 6 8

1 3 5 7
Merge-Sort Execution Example
Recursive call return
2 4 6 8 1 3 5 7
Merge-Sort Execution Example
merge
1 2 3 4 5 6 7 8
Merge-Sort Analysis
• Time, divide
n
n

n/2 n/2 2 × n/2 = n

log n levels

n/4 n/4 n/4 n/4 4 × n/4 = n

n/2 × 2 = n
Total time for divide: n log n
Merge-Sort Analysis
• Time, merging
n
cn

n/2 n/2 2 × cn/2 = n

log n levels

n/4 n/4 n/4 n/4 4 × cn/4 = n

n/2 × 2c = n
Total time for merging: cn log n

• Total running time: order of nlogn


• Total Space: order of n
Running Time of Merge-Sort

 At each level in the binary tree created for


Merge Sort, there are n elements, with O(1)
time spent at each element
  O(n) running time for processing one level
 The height of the tree is O(log n)

 Therefore, the time complexity is O(nlog n)


Big-Oh to Primary Sorts


Bubble Sort = n²

Selection Sort = n²

Insertion Sort = n²

Merge Sort =n
log(n)

Quick Sort = n log(n)

You might also like