Searching
The process used to find the location
of a target among a list of objects
Searching an array finds the index of first element in an array
containing that value
Searching
search_sort_v1.pptgghghhhggggjjjjjjllllllllvbbbbbcfdsdfffg
search_sort_v1.pptgghghhhggggjjjjjjllllllllvbbbbbcfdsdfffg
Unordered Linear Search
Search an unordered array of integers for a value and return its index if
the value is found. Otherwise, return -1.
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7]
Algorithm:
Start with the first array element (index 0)
while(more elements in array){
if value found at current index, return index;
Try next element (increment index);
}
Value not found, return -1;
14 2 10 5 1 3 17 2
Unordered Linear Search
// Searches an unordered array of integers
int search(int data[], //input: array
int size, //input: array size
int value){ //input: search value
// output: if found, return index;
// otherwise, return –1.
for(int index = 0; index < size; index++){
if(data[index] == value)
return index;
}
return -1;
}
search_sort_v1.pptgghghhhggggjjjjjjllllllllvbbbbbcfdsdfffg
8
2
1 3 4 6
5 7 10
9 11 12 14
13
0
64
14
13 25 33 51
43 53 84
72 93 95 97
96
6
Binary Search
lo
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo]  value  a[hi].
Ex. Binary search for 33.
hi
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo]  value  a[hi].
Ex. Binary search for 33.
8
2
1 3 4 6
5 7 10
9 11 12 14
13
0
64
14
13 25 33 51
43 53 84
72 93 95 97
96
6
lo hi
mid
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo]  value  a[hi].
Ex. Binary search for 33.
8
2
1 3 4 6
5 7 10
9 11 12 14
13
0
64
14
13 25 33 51
43 53 84
72 93 95 97
96
6
lo hi
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo]  value  a[hi].
Ex. Binary search for 33.
8
2
1 3 4 6
5 7 10
9 11 12 14
13
0
64
14
13 25 33 51
43 53 84
72 93 95 97
96
6
lo mid hi
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo]  value  a[hi].
Ex. Binary search for 33.
8
2
1 3 4 6
5 7 10
9 11 12 14
13
0
64
14
13 25 33 51
43 53 84
72 93 95 97
96
6
lo hi
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo]  value  a[hi].
Ex. Binary search for 33.
8
2
1 3 4 6
5 7 10
9 11 12 14
13
0
64
14
13 25 33 51
43 53 84
72 93 95 97
96
6
lo hi
mid
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo]  value  a[hi].
Ex. Binary search for 33.
8
2
1 3 4 6
5 7 10
9 11 12 14
13
0
64
14
13 25 33 51
43 53 84
72 93 95 97
96
6
lo
hi
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo]  value  a[hi].
Ex. Binary search for 33.
8
2
1 3 4 6
5 7 10
9 11 12 14
13
0
64
14
13 25 33 51
43 53 84
72 93 95 97
96
6
lo
hi
mid
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo]  value  a[hi].
Ex. Binary search for 33.
8
2
1 3 4 6
5 7 10
9 11 12 14
13
0
64
14
13 25 33 51
43 53 84
72 93 95 97
96
6
lo
hi
mid
17
Example: Binary Search
Searching for an element k in a sorted array A with n
elements
Idea:

Choose the middle element A[n/2]

If k == A[n/2], we are done

If k < A[n/2], search for k between A[0] and A[n/2 -
1]

If k > A[n/2], search for k between A[n/2 + 1] and
A[n-1]

Repeat until either k is found, or no more elements
to search
Requires less number of comparisons than linear
search in the worst case (log2n instead of n)
18
int main() {
int A[100], n, k, i, mid, low, high;
scanf(“%d %d”, &n, &k);
for (i=0; i<n; ++i)
scanf(“%d”, &A[i]);
low = 0; high = n – 1; mid = (high + low)/2;
while (high >= low) {
if (A[mid] == k) {
printf(“%d is foundn”, k);
break;
}
if (k < A[mid]) high = mid – 1;
else
low = mid + 1;
mid = (high + low)/2;
}
If (high < low) printf(“%d is not foundn”, k);
}
Sorting
20
Bubble Sort
Stage 1: Compare each element (except the last one) with
its neighbor to the right

If they are out of order, swap them

This puts the largest element at the very end

The last element is now in the correct and final place
Stage 2: Compare each element (except the last two)
with its neighbor to the right

If they are out of order, swap them

This puts the second largest element next to last

The last two elements are now in their correct and final places
Stage 3: Compare each element (except the last three)
with its neighbor to the right

Continue as above until you have no unsorted elements on the left
21
Example of Bubble Sort
7 2 8 5 4
2 7 8 5 4
2 7 8 5 4
2 7 5 8 4
2 7 5 4 8
2 7 5 4 8
2 5 7 4 8
2 5 4 7 8
2 7 5 4 8
2 5 4 7 8
2 4 5 7 8
2 5 4 7 8
2 4 5 7 8
2 4 5 7 8
(done)
Stage 1 Stage 2 Stage 3 Stage 4
22
Code for Bubble Sort
void bubbleSort(int[] a) {
int stage, inner, temp;
for (stage = a.length - 1; stage > 0; stage--) { // counting down
for (inner = 0; inner < stage; inner++) { // bubbling up
if (a[inner] > a[inner + 1]) { // if out of order...
temp = a[inner]; // ...then swap
a[inner] = a[inner + 1];
a[inner + 1] = temp;
}
}
}
}
23
Insertion Sort
24
Insertion Sort
Suppose we know how to insert a new element x in its proper
place in an already sorted array A of size k, to get a new
sorted array of size k+1
Use this to sort the given array A of size n as follows:

Insert A[1] in the sorted array A[0]. So now A[0],A[1] are
sorted

Insert A[2] in the sorted array A[0],A[1]. So now
A[0],A[1],A[2] are sorted

Insert A[3] in the sorted array A[0],A[1],A[2]. So now
A[0],A[1],A[2],A[3] are sorted

…..

Insert A[i] in the sorted array A[0],A[1],…,A[i-1]. So now
A[0],A[1],…A[i] are sorted

Continue until i = n-1 (outer loop)
25
How to do the first step
Compare x with A[k-1] (the last element)

If x ≥ A[k-1], we can make A[k] = x (as x is the max
of all the elements)

If x < A[k-1], put A[k] = A[k-1] to create a hole in
the k-th position, put x there
Now repeat by comparing x with A[k-2] (inserting x in
its proper place in the sorted subarray A[0],A[1],…
A[k-1] of k-2 elements)
The value x bubbles to the left until it finds an
element A[i] such that x ≥ A[i]
No need to compare any more as all elements A[0],
A[1], A[i] are less than x
26
Example of first step
5 7 11 13 20 22
A Insert x = 15
27
Example of first step
5 7 11 13 20 22
5 7 11 13 20 15 22
A Insert x = 15
Compare with 22. x < 22, so move 22 right
28
Example of first step
5 7 11 13 20 22
5 7 11 13 20 15 22
5 7 11 13 15 20 22
A Insert x = 15
Compare with 22. x < 22, so move 22 right
Compare with 20. x < 20, so move 20 right
29
Example of first step
5 7 11 13 20 22
5 7 11 13 20 15 22
5 7 11 13 15 20 22
5 7 11 13 15 20 22
A Insert x = 15
Compare with 22. x < 22, so move 22 right
Compare with 20. x < 20, so move 20 right
Compare with 13. x > 13, so stop
A
30
Sort using the insertion
7 5 13 11 22 20
5 7 13 11 22 20
5 7 13 11 22 20
5 7 11 13 22 20
A
Insert 5 in 7
Insert 13 in 5, 7
Insert 11 in 5, 7, 13
Insert 22 in 5, 7, 11, 13
Insert 20 in 5, 7, 11, 13, 22
5 7 11 13 20 22
5 7 11 13 22 20
31
Insertion Sort Code
void InsertionSort (int A[ ], int size)
{
int i, j, item;
for (i=1; i<size; i++)
{ /* Insert the element in A[i] */
item = A[i] ;
for (j = i-1; j >= 0; j--)
if (item < A[j])
{ /* push elements down*/
A[j+1] = A[j];
A[j] = item ; /* can do this on
}
else break; /*inserted, exit loop */
}
}
32
Look at the sorting!
8
2
9
4
7
6
2
1
5
i = 1:: 2, 9, 4, 7, 6, 2, 1, 5
i = 2:: 9, 2, 4, 7, 6, 2, 1, 5
i = 3:: 9, 4, 2, 7, 6, 2, 1, 5
i = 4:: 9, 7, 4, 2, 6, 2, 1, 5
i = 5:: 9, 7, 6, 4, 2, 2, 1, 5
i = 6:: 9, 7, 6, 4, 2, 2, 1, 5
i = 7:: 9, 7, 6, 4, 2, 2, 1, 5
Result = 9, 7, 6, 5, 4, 2, 2, 1
void InsertionSort (int A[ ], int size) {
int i,j, item;
for (i=1; i<size; i++) {
printf("i = %d:: ",i);
for (j=0;j<size;j++) printf("%d, ",A[j]);
printf("n"); item = A[i] ;
for (j=i-1; j>=0; j--)
if (item > A[j])
{ A[j+1] = A[j]; A[j] = item ; }
else break;
}
int main() {
int X[100], i, size;
scanf("%d",&size);
for (i=0;i<size;i++) scanf("%d",&X[i]);
InsertionSort(X,size);
printf("Result = ");
for (i=0;i<size;i++) printf("%d, ",X[i]);
printf("n"); return 0;
}
33
Mergesort
34
Basic Idea
Divide the array into two halves
Sort the two sub-arrays
Merge the two sorted sub-arrays into a single
sorted array
Step 2 (sorting the sub-arrays) is done
recursively (divide in two, sort, merge) until
the array has a single element (base
condition of recursion)
35
Merging Two Sorted Arrays
3 4 7 8 9
2 5 7
2
3 4 7 8 9
2 5 7
2 3
3 4 7 8 9
2 5 7
2 3 4
3 4 7 8 9
2 5 7
Problem: Two sorted arrays A and B are given. We are required
produce a final sorted array C which contains all elements of A
36
2 3 4 5
3 4 7 8 9
2 5 7
2 3 4 5 7
3 4 7 8 9
2 5 7
2 3 4 5 7 7
3 4 7 8 9
2 5 7
2 3 4 5 7 7 8
3 4 7 8 9
2 5 7
37
Merge Code
2 3 4 5 7 7 8 9
3 4 7 8 9
2 5 7
void
merge (int A[], int B[], int C[], int m,int n)
{
int i=0,j=0,k=0;
while (i<m && j<n)
{
if (A[i] < B[j]) C[k++] = A[i++];
else C[k++] = B[j++];
}
while (i<m) C[k++] = A[i++];
while (j<n) C[k++] = B[j++];
}
38
Merge Sort: Sorting an array
recursively
void mergesort (int A[], int n)
{
int i, j, B[max];
if (n <= 1) return;
i = n/2;
mergesort(A, i);
mergesort(A+i, n-i);
merge(A, A+i, B, i, n-i);
for (j=0; j<n; j++) A[j] = B[j];
free(B);
}

More Related Content

PPT
search_sort.ppt
PPTX
searching in data structure.pptx
PDF
Chapter 14 Searching and Sorting
PDF
Searching and Sorting in Data Structures with examples
PPTX
Chapter 2. data structure and algorithm
PDF
L 14-ct1120
PPTX
PPT.pptx Searching and Sorting Techniques
PPTX
Searching and sorting Techniques in Data structures
search_sort.ppt
searching in data structure.pptx
Chapter 14 Searching and Sorting
Searching and Sorting in Data Structures with examples
Chapter 2. data structure and algorithm
L 14-ct1120
PPT.pptx Searching and Sorting Techniques
Searching and sorting Techniques in Data structures

Similar to search_sort_v1.pptgghghhhggggjjjjjjllllllllvbbbbbcfdsdfffg (20)

PPTX
2.Problem Solving Techniques and Data Structures.pptx
PPT
Arrays
PPT
Insert Sort & Merge Sort Using C Programming
PPTX
Data Structure and algorithms for software
PPSX
Data Structure and Algorithm Chapter 2.ppsx Chapter 2.ppsx
PPTX
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
PDF
Chapter Two.pdf
PPTX
Chapter-2.pptx
PPT
Sorting algorithms
PPT
Unit6 C
PPT
Unit 7 sorting
PPTX
Chapter 2 Sorting and Searching .pptx.soft
PPTX
Chapter 2 Sorting and Searching .pptx.soft
PPTX
Algorithm & data structures lec4&5
PPTX
All Searching and Sorting Techniques in Data Structures
PPT
Lecture_4 (Sorting Algorithms) before mids - Copy.ppt
PDF
Chapter 1 - Introduction to Searching and Sorting Algorithms - Student.pdf
PPT
Sorting algorithums > Data Structures & Algorithums
PPT
Unit6 jwfiles
PPTX
Searching and Sorting Algorithms in Data Structures
2.Problem Solving Techniques and Data Structures.pptx
Arrays
Insert Sort & Merge Sort Using C Programming
Data Structure and algorithms for software
Data Structure and Algorithm Chapter 2.ppsx Chapter 2.ppsx
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
Chapter Two.pdf
Chapter-2.pptx
Sorting algorithms
Unit6 C
Unit 7 sorting
Chapter 2 Sorting and Searching .pptx.soft
Chapter 2 Sorting and Searching .pptx.soft
Algorithm & data structures lec4&5
All Searching and Sorting Techniques in Data Structures
Lecture_4 (Sorting Algorithms) before mids - Copy.ppt
Chapter 1 - Introduction to Searching and Sorting Algorithms - Student.pdf
Sorting algorithums > Data Structures & Algorithums
Unit6 jwfiles
Searching and Sorting Algorithms in Data Structures
Ad

Recently uploaded (20)

PPTX
MODULE 02 - CLOUD COMPUTING-Virtual Machines and Virtualization of Clusters a...
PPT
Module_1_Lecture_1_Introduction_To_Automation_In_Production_Systems2023.ppt
PPTX
Software-Development-Life-Cycle-SDLC.pptx
PPTX
Module1.pptxrjkeieuekwkwoowkemehehehrjrjrj
PPTX
Soft Skills Unit 2 Listening Speaking Reading Writing.pptx
PPTX
SE unit 1.pptx aaahshdhajdviwhsiehebeiwheiebeiev
PDF
Introduction to Machine Learning -Basic concepts,Models and Description
PPTX
INTERNET OF THINGS - EMBEDDED SYSTEMS AND INTERNET OF THINGS
PDF
electrical machines course file-anna university
PDF
B461227.pdf American Journal of Multidisciplinary Research and Review
PPTX
22ME926Introduction to Business Intelligence and Analytics, Advanced Integrat...
PDF
VTU IOT LAB MANUAL (BCS701) Computer science and Engineering
PPTX
Research Writing, Mechanical Engineering
PDF
THE PEDAGOGICAL NEXUS IN TEACHING ELECTRICITY CONCEPTS IN THE GRADE 9 NATURAL...
PPTX
1. Effective HSEW Induction Training - EMCO 2024, O&M.pptx
PDF
Performance, energy consumption and costs: a comparative analysis of automati...
PPT
Basics Of Pump types, Details, and working principles.
PPTX
SC Robotics Team Safety Training Presentation
PDF
Mechanics of materials week 2 rajeshwari
PDF
ST MNCWANGO P2 WIL (MEPR302) FINAL REPORT.pdf
MODULE 02 - CLOUD COMPUTING-Virtual Machines and Virtualization of Clusters a...
Module_1_Lecture_1_Introduction_To_Automation_In_Production_Systems2023.ppt
Software-Development-Life-Cycle-SDLC.pptx
Module1.pptxrjkeieuekwkwoowkemehehehrjrjrj
Soft Skills Unit 2 Listening Speaking Reading Writing.pptx
SE unit 1.pptx aaahshdhajdviwhsiehebeiwheiebeiev
Introduction to Machine Learning -Basic concepts,Models and Description
INTERNET OF THINGS - EMBEDDED SYSTEMS AND INTERNET OF THINGS
electrical machines course file-anna university
B461227.pdf American Journal of Multidisciplinary Research and Review
22ME926Introduction to Business Intelligence and Analytics, Advanced Integrat...
VTU IOT LAB MANUAL (BCS701) Computer science and Engineering
Research Writing, Mechanical Engineering
THE PEDAGOGICAL NEXUS IN TEACHING ELECTRICITY CONCEPTS IN THE GRADE 9 NATURAL...
1. Effective HSEW Induction Training - EMCO 2024, O&M.pptx
Performance, energy consumption and costs: a comparative analysis of automati...
Basics Of Pump types, Details, and working principles.
SC Robotics Team Safety Training Presentation
Mechanics of materials week 2 rajeshwari
ST MNCWANGO P2 WIL (MEPR302) FINAL REPORT.pdf
Ad

search_sort_v1.pptgghghhhggggjjjjjjllllllllvbbbbbcfdsdfffg

  • 2. The process used to find the location of a target among a list of objects Searching an array finds the index of first element in an array containing that value Searching
  • 5. Unordered Linear Search Search an unordered array of integers for a value and return its index if the value is found. Otherwise, return -1. A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] Algorithm: Start with the first array element (index 0) while(more elements in array){ if value found at current index, return index; Try next element (increment index); } Value not found, return -1; 14 2 10 5 1 3 17 2
  • 6. Unordered Linear Search // Searches an unordered array of integers int search(int data[], //input: array int size, //input: array size int value){ //input: search value // output: if found, return index; // otherwise, return –1. for(int index = 0; index < size; index++){ if(data[index] == value) return index; } return -1; }
  • 8. 8 2 1 3 4 6 5 7 10 9 11 12 14 13 0 64 14 13 25 33 51 43 53 84 72 93 95 97 96 6 Binary Search lo Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo]  value  a[hi]. Ex. Binary search for 33. hi
  • 9. Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo]  value  a[hi]. Ex. Binary search for 33. 8 2 1 3 4 6 5 7 10 9 11 12 14 13 0 64 14 13 25 33 51 43 53 84 72 93 95 97 96 6 lo hi mid
  • 10. Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo]  value  a[hi]. Ex. Binary search for 33. 8 2 1 3 4 6 5 7 10 9 11 12 14 13 0 64 14 13 25 33 51 43 53 84 72 93 95 97 96 6 lo hi
  • 11. Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo]  value  a[hi]. Ex. Binary search for 33. 8 2 1 3 4 6 5 7 10 9 11 12 14 13 0 64 14 13 25 33 51 43 53 84 72 93 95 97 96 6 lo mid hi
  • 12. Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo]  value  a[hi]. Ex. Binary search for 33. 8 2 1 3 4 6 5 7 10 9 11 12 14 13 0 64 14 13 25 33 51 43 53 84 72 93 95 97 96 6 lo hi
  • 13. Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo]  value  a[hi]. Ex. Binary search for 33. 8 2 1 3 4 6 5 7 10 9 11 12 14 13 0 64 14 13 25 33 51 43 53 84 72 93 95 97 96 6 lo hi mid
  • 14. Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo]  value  a[hi]. Ex. Binary search for 33. 8 2 1 3 4 6 5 7 10 9 11 12 14 13 0 64 14 13 25 33 51 43 53 84 72 93 95 97 96 6 lo hi
  • 15. Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo]  value  a[hi]. Ex. Binary search for 33. 8 2 1 3 4 6 5 7 10 9 11 12 14 13 0 64 14 13 25 33 51 43 53 84 72 93 95 97 96 6 lo hi mid
  • 16. Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo]  value  a[hi]. Ex. Binary search for 33. 8 2 1 3 4 6 5 7 10 9 11 12 14 13 0 64 14 13 25 33 51 43 53 84 72 93 95 97 96 6 lo hi mid
  • 17. 17 Example: Binary Search Searching for an element k in a sorted array A with n elements Idea:  Choose the middle element A[n/2]  If k == A[n/2], we are done  If k < A[n/2], search for k between A[0] and A[n/2 - 1]  If k > A[n/2], search for k between A[n/2 + 1] and A[n-1]  Repeat until either k is found, or no more elements to search Requires less number of comparisons than linear search in the worst case (log2n instead of n)
  • 18. 18 int main() { int A[100], n, k, i, mid, low, high; scanf(“%d %d”, &n, &k); for (i=0; i<n; ++i) scanf(“%d”, &A[i]); low = 0; high = n – 1; mid = (high + low)/2; while (high >= low) { if (A[mid] == k) { printf(“%d is foundn”, k); break; } if (k < A[mid]) high = mid – 1; else low = mid + 1; mid = (high + low)/2; } If (high < low) printf(“%d is not foundn”, k); }
  • 20. 20 Bubble Sort Stage 1: Compare each element (except the last one) with its neighbor to the right  If they are out of order, swap them  This puts the largest element at the very end  The last element is now in the correct and final place Stage 2: Compare each element (except the last two) with its neighbor to the right  If they are out of order, swap them  This puts the second largest element next to last  The last two elements are now in their correct and final places Stage 3: Compare each element (except the last three) with its neighbor to the right  Continue as above until you have no unsorted elements on the left
  • 21. 21 Example of Bubble Sort 7 2 8 5 4 2 7 8 5 4 2 7 8 5 4 2 7 5 8 4 2 7 5 4 8 2 7 5 4 8 2 5 7 4 8 2 5 4 7 8 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8 2 5 4 7 8 2 4 5 7 8 2 4 5 7 8 (done) Stage 1 Stage 2 Stage 3 Stage 4
  • 22. 22 Code for Bubble Sort void bubbleSort(int[] a) { int stage, inner, temp; for (stage = a.length - 1; stage > 0; stage--) { // counting down for (inner = 0; inner < stage; inner++) { // bubbling up if (a[inner] > a[inner + 1]) { // if out of order... temp = a[inner]; // ...then swap a[inner] = a[inner + 1]; a[inner + 1] = temp; } } } }
  • 24. 24 Insertion Sort Suppose we know how to insert a new element x in its proper place in an already sorted array A of size k, to get a new sorted array of size k+1 Use this to sort the given array A of size n as follows:  Insert A[1] in the sorted array A[0]. So now A[0],A[1] are sorted  Insert A[2] in the sorted array A[0],A[1]. So now A[0],A[1],A[2] are sorted  Insert A[3] in the sorted array A[0],A[1],A[2]. So now A[0],A[1],A[2],A[3] are sorted  …..  Insert A[i] in the sorted array A[0],A[1],…,A[i-1]. So now A[0],A[1],…A[i] are sorted  Continue until i = n-1 (outer loop)
  • 25. 25 How to do the first step Compare x with A[k-1] (the last element)  If x ≥ A[k-1], we can make A[k] = x (as x is the max of all the elements)  If x < A[k-1], put A[k] = A[k-1] to create a hole in the k-th position, put x there Now repeat by comparing x with A[k-2] (inserting x in its proper place in the sorted subarray A[0],A[1],… A[k-1] of k-2 elements) The value x bubbles to the left until it finds an element A[i] such that x ≥ A[i] No need to compare any more as all elements A[0], A[1], A[i] are less than x
  • 26. 26 Example of first step 5 7 11 13 20 22 A Insert x = 15
  • 27. 27 Example of first step 5 7 11 13 20 22 5 7 11 13 20 15 22 A Insert x = 15 Compare with 22. x < 22, so move 22 right
  • 28. 28 Example of first step 5 7 11 13 20 22 5 7 11 13 20 15 22 5 7 11 13 15 20 22 A Insert x = 15 Compare with 22. x < 22, so move 22 right Compare with 20. x < 20, so move 20 right
  • 29. 29 Example of first step 5 7 11 13 20 22 5 7 11 13 20 15 22 5 7 11 13 15 20 22 5 7 11 13 15 20 22 A Insert x = 15 Compare with 22. x < 22, so move 22 right Compare with 20. x < 20, so move 20 right Compare with 13. x > 13, so stop A
  • 30. 30 Sort using the insertion 7 5 13 11 22 20 5 7 13 11 22 20 5 7 13 11 22 20 5 7 11 13 22 20 A Insert 5 in 7 Insert 13 in 5, 7 Insert 11 in 5, 7, 13 Insert 22 in 5, 7, 11, 13 Insert 20 in 5, 7, 11, 13, 22 5 7 11 13 20 22 5 7 11 13 22 20
  • 31. 31 Insertion Sort Code void InsertionSort (int A[ ], int size) { int i, j, item; for (i=1; i<size; i++) { /* Insert the element in A[i] */ item = A[i] ; for (j = i-1; j >= 0; j--) if (item < A[j]) { /* push elements down*/ A[j+1] = A[j]; A[j] = item ; /* can do this on } else break; /*inserted, exit loop */ } }
  • 32. 32 Look at the sorting! 8 2 9 4 7 6 2 1 5 i = 1:: 2, 9, 4, 7, 6, 2, 1, 5 i = 2:: 9, 2, 4, 7, 6, 2, 1, 5 i = 3:: 9, 4, 2, 7, 6, 2, 1, 5 i = 4:: 9, 7, 4, 2, 6, 2, 1, 5 i = 5:: 9, 7, 6, 4, 2, 2, 1, 5 i = 6:: 9, 7, 6, 4, 2, 2, 1, 5 i = 7:: 9, 7, 6, 4, 2, 2, 1, 5 Result = 9, 7, 6, 5, 4, 2, 2, 1 void InsertionSort (int A[ ], int size) { int i,j, item; for (i=1; i<size; i++) { printf("i = %d:: ",i); for (j=0;j<size;j++) printf("%d, ",A[j]); printf("n"); item = A[i] ; for (j=i-1; j>=0; j--) if (item > A[j]) { A[j+1] = A[j]; A[j] = item ; } else break; } int main() { int X[100], i, size; scanf("%d",&size); for (i=0;i<size;i++) scanf("%d",&X[i]); InsertionSort(X,size); printf("Result = "); for (i=0;i<size;i++) printf("%d, ",X[i]); printf("n"); return 0; }
  • 34. 34 Basic Idea Divide the array into two halves Sort the two sub-arrays Merge the two sorted sub-arrays into a single sorted array Step 2 (sorting the sub-arrays) is done recursively (divide in two, sort, merge) until the array has a single element (base condition of recursion)
  • 35. 35 Merging Two Sorted Arrays 3 4 7 8 9 2 5 7 2 3 4 7 8 9 2 5 7 2 3 3 4 7 8 9 2 5 7 2 3 4 3 4 7 8 9 2 5 7 Problem: Two sorted arrays A and B are given. We are required produce a final sorted array C which contains all elements of A
  • 36. 36 2 3 4 5 3 4 7 8 9 2 5 7 2 3 4 5 7 3 4 7 8 9 2 5 7 2 3 4 5 7 7 3 4 7 8 9 2 5 7 2 3 4 5 7 7 8 3 4 7 8 9 2 5 7
  • 37. 37 Merge Code 2 3 4 5 7 7 8 9 3 4 7 8 9 2 5 7 void merge (int A[], int B[], int C[], int m,int n) { int i=0,j=0,k=0; while (i<m && j<n) { if (A[i] < B[j]) C[k++] = A[i++]; else C[k++] = B[j++]; } while (i<m) C[k++] = A[i++]; while (j<n) C[k++] = B[j++]; }
  • 38. 38 Merge Sort: Sorting an array recursively void mergesort (int A[], int n) { int i, j, B[max]; if (n <= 1) return; i = n/2; mergesort(A, i); mergesort(A+i, n-i); merge(A, A+i, B, i, n-i); for (j=0; j<n; j++) A[j] = B[j]; free(B); }