SlideShare a Scribd company logo
JSPM’s JSCOE ,HADAPSAR ,Pune-412207
Department of Computer Engineering
Unit 03: Sorting and Searching
Presented By: Prof .S.A. Patil
Objectives
After you have read and studied this chapter, you
should be able to
• Perform linear and binary search algorithms on small arrays.
• Determine whether a linear or binary search is more effective
for a given situation.
• Perform selection and bubble sort algorithms.
• Describe the heapsort algorithm and show how its performance
is superior to the other two algorithms.
• Apply basic sorting algorithms to sort an array of objects.
Searching
• When we maintain a collection of data, one of the
operations we need is a search routine to locate desired
data quickly.
• Here’s the problem statement:
Given a value X, return the index of X in the array, if such X exists. Otherwise,
return NOT_FOUND (-1). We assume there are no duplicate entries in the
array.
• We will count the number of comparisons the algorithms
make to analyze their performance.
– The ideal searching algorithm will make the least possible number
of comparisons to locate the desired data.
– Two separate performance analyses are normally done, one for
successful search and another for unsuccessful search.
Search Result
number
23 17 5 90 12 44 38
0 1 2 3 4 5 6 7 8
84 77
Unsuccessful Search:
Successful Search:
NOT_FOUND
search( 45 )
search( 12 ) 4
Linear Search
• Search the array from the first to the last position
in linear progression.
public int linearSearch ( int[] number, int searchValue ) {
int loc = 0;
while (loc < number.length && number[loc] !=
searchValue) {
loc++;
}
if (loc == number.length) { //Not found
return NOT_FOUND;
} else {
return loc; //Found, return the position
}
}
Linear Search Performance
• We analyze the successful and unsuccessful
searches separately.
• We count how many times the search value is
compared against the array elements.
• Successful Search
– Best Case – 1 comparison
– Worst Case – N comparisons (N – array size)
• Unsuccessful Search
– Best Case =
Worst Case – N comparisons
Binary Search
• If the array is sorted, then we can apply the binary search
technique.
• The basic idea is straightforward. First search the value in
the middle position. If X is less than this value, then search
the middle of the left half next. If X is greater than this
value, then search the middle of the right half next.
Continue in this manner.
number
5 12 17 23 38 44 77
0 1 2 3 4 5 6 7 8
84 90
Sequence of Successful Search - 1
5 12 17 23 38 44 77
0 1 2 3 4 5 6 7 8
84 90
search( 44 )
low high mid
0 8
#1
high
low





 

2
high
low
mid
38 < 44 low = mid+1 = 5
mid
4
Sequence of Successful Search - 2
5 12 17 23 38 44 77
0 1 2 3 4 5 6 7 8
84 90
search( 44 )
low high mid
0 8
#1





 

2
high
low
mid
44 < 77
high = mid-1=5
4
mid
6
high
low
5 8
#2
Sequence of Successful Search - 3
5 12 17 23 38 44 77
0 1 2 3 4 5 6 7 8
84 90
search( 44 )
low high mid
0 8
#1





 

2
high
low
mid
44 == 44
4
5 8
#2 6
high
low
5 5
#3
mid
5
Successful Search!!
Sequence of Unsuccessful Search - 1
5 12 17 23 38 44 77
0 1 2 3 4 5 6 7 8
84 90
search( 45 )
low high mid
0 8
#1
high
low





 

2
high
low
mid
38 < 45 low = mid+1 = 5
mid
4
Sequence of Unsuccessful Search - 2
5 12 17 23 38 44 77
0 1 2 3 4 5 6 7 8
84 90
search( 45 )
low high mid
0 8
#1





 

2
high
low
mid
45 < 77
high = mid-1=5
4
mid
6
high
low
5 8
#2
Sequence of Unsuccessful Search - 3
5 12 17 23 38 44 77
0 1 2 3 4 5 6 7 8
84 90
search( 45 )
low high mid
0 8
#1





 

2
high
low
mid
44 < 45
4
5 8
#2 6
high
low
5 5
#3
mid
5
low = mid+1 = 6
Sequence of Unsuccessful Search - 4
5 12 17 23 38 44 77
0 1 2 3 4 5 6 7 8
84 90
search( 45 )
low high mid
0 8
#1





 

2
high
low
mid
4
5 8
#2 6
5 5
#3 5
high low
6 5
#4
Unsuccessful Search
low > high
no more elements to search
Binary Search Routine
public int binarySearch (int[] number, int searchValue) {
int low = 0,
high = number.length - 1,
mid = (low + high) / 2;
while (low <= high && number[mid] != searchValue) {
if (number[mid] < searchValue) {
low = mid + 1;
} else { //number[mid] > searchValue
high = mid - 1;
}
mid = (low + high) / 2; //integer division
will truncate
}
if (low > high) {
mid = NOT_FOUND;
}
return mid;
}
Binary Search Performance
• Successful Search
– Best Case – 1 comparison
– Worst Case – log2N comparisons
• Unsuccessful Search
– Best Case =
Worst Case – log2N comparisons
• Since the portion of an array to search is cut into half after
every comparison, we compute how many times the array
can be divided into halves.
• After K comparisons, there will be N/2K
elements in the list.
We solve for K when N/2K
= 1, deriving K = log2N.
Comparing N and log2N Performance
Array Size Linear – N Binary –
log2N
10 10 4
50 50 6
100 100 7
500 500 9
1000 1000 10
2000 2000 11
3000 3000 12
4000 4000 12
5000 5000 13
6000 6000 13
7000 7000 13
8000 8000 13
9000 9000 14
10000 10000 14
Sorting
• When we maintain a collection of data, many applications call
for rearranging the data in certain order, e.g. arranging
Person information in ascending order of age.
• Here’s the problem statement:
Given an array of N integer values, arrange the values into ascending
order.
• We will count the number of comparisons the algorithms
make to analyze their performance.
– The ideal sorting algorithm will make the least possible number of
comparisons to arrange data in a designated order.
• We will compare different sorting algorithms by analyzing
their worst-case performance.
Selection Sort
1. Find the smallest element
in the list.
2. Exchange the element in
the first position and the
smallest element. Now
the smallest element is in
the first position.
3. Repeat Step 1 and 2 with
the list having one less
element (i.e., the smallest
element is discarded from
further processing).
0 1 2 3 4 5 6 7 8
23 17 5 90 12 44 38 84 77
min
first
exchange
0 1 2 3 4 5 6 7 8
5 17 23 90 12 44 38 84 77
sorted unsorted
This is the result of one pass.
Selection Sort Passes
0 1 2 3 4 5 6 7 8
5 17 23 90 12 44 38 84 77
sorted
5 12 23 90 17 44 38 84 77
2
5 12 17 90 23 44 38 84 77
3
5 12 17 23 38 44 77 84 90
7
5 12 17 23 38 44 77 84 90
8
1
Pass #
Result AFTER one
pass is completed.
Selection Sort Routine
public void selectionSort(int[] number) {
int startIndex, minIndex, length, temp;
length = number.length;
for (startIndex = 0; startIndex <= length-2; startIndex++){
//each iteration of the for loop is one pass
minIndex = startIndex;
//find the smallest in this pass at position
minIndex
for (int i = startIndex+1; i <= length-1; i++) {
if (number[i] < number[minIndex])
minIndex = i;
}
//exchange number[startIndex] and number[minIndex]
temp =
number[startIndex];
number[startIndex] = number[minIndex];
number[minIndex] = temp;
}
}
Selection Sort Performance
• We derive the total number of
comparisons by counting the
number of times the inner loop
is executed.
• For each execution of the outer
loop, the inner loop is executed
length – start times.
• The variable length is the size of
the array. Replacing length with
N, the array size, the sum is
derived as…
Bubble Sort
• With the selection sort, we make one exchange at the end
of one pass.
• The bubble sort improves the performance by making
more than one exchange during its pass.
• By making multiple exchanges, we will be able to move
more elements toward their correct positions using the
same number of comparisons as the selection sort makes.
• The key idea of the bubble sort is to make pairwise
comparisons and exchange the positions of the pair if they
are out of order.
One Pass of Bubble Sort
The largest value 90 is at the end of
the list.
0 1 2 3 4 5 6 7 8
23 17 5 90 12 44 38 84 77
17 23 5 90 12 44 38 84 77
17 5 23 90 12 44 38 84 77
17 5 23 12 90 44 38 84 77
17 5 23 12 44 90 38 84 77
17 5 23 12 44 38 90 84 77
17 5 23 12 44 38 84 90 77
17 5 23 12 44 38 84 77 90
exchange
ok
exchange
exchange
exchange
exchange
exchange
exchange
Bubble Sort Routine
public void bubbleSort(int[] number) {
int temp, bottom, i;
boolean exchanged = true;
bottom = number.length - 2;
while (exchanged) {
exchanged = false;
for (i = 0; i <= bottom; i++) {
if (number[i] > number[i+1]) {
temp =
number[i]; //exchange
number[i] = number[i+1];
number[i+1] = temp;
exchanged = true; //exchange
is made
}
}
bottom--;
}
}

More Related Content

PPT
Chapter 11 - Sorting and Searching
PPT
(Data Structure) Chapter11 searching & sorting
PPTX
Searching searching in in arrays arrays.pptx
PPT
Data Structure (MC501)
PPT
Data Structures 6
PPTX
SORT AND SEARCH ARRAY WITH WITH C++.pptx
PPTX
Lecture_Oct26.pptx
PPTX
Unit 7 sorting
Chapter 11 - Sorting and Searching
(Data Structure) Chapter11 searching & sorting
Searching searching in in arrays arrays.pptx
Data Structure (MC501)
Data Structures 6
SORT AND SEARCH ARRAY WITH WITH C++.pptx
Lecture_Oct26.pptx
Unit 7 sorting

Similar to Searching Sorting-SELECTION ,BUBBBLE.ppt (20)

PPTX
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
PPTX
Unit vii sorting
PDF
advanced searching and sorting.pdf
PPTX
Searching and sorting Techniques in Data structures
PPTX
Sorting and hashing concepts
PPTX
Sorting and hashing concepts
PPTX
sorting and searching.pptx
PPTX
Parallel Sorting Algorithms. Quicksort. Merge sort. List Ranking
PPTX
Algorithms and Data Structures for Sorting Numerical Data
PPTX
DSA_chapter and chapter 3 _03_Sorting Algorithms.pptx
PPTX
Analysis and Design of Algorithms -Sorting Algorithms and analysis
PPTX
sorting-160810203705.pptx
PDF
Algo PPT.pdf
PPTX
Insertion Sort, Quick Sort And Their complexity
PPTX
Algorithm & data structures lec4&5
PDF
Chapter 8 advanced sorting and hashing for print
PPTX
Searching and Sorting algorithms and working
PDF
Advanced Topics In Java Core Concepts In Data Structures Noel Kalicharan
PPTX
Chapter 8 Sorting in the context of DSA.pptx
PPTX
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
Unit vii sorting
advanced searching and sorting.pdf
Searching and sorting Techniques in Data structures
Sorting and hashing concepts
Sorting and hashing concepts
sorting and searching.pptx
Parallel Sorting Algorithms. Quicksort. Merge sort. List Ranking
Algorithms and Data Structures for Sorting Numerical Data
DSA_chapter and chapter 3 _03_Sorting Algorithms.pptx
Analysis and Design of Algorithms -Sorting Algorithms and analysis
sorting-160810203705.pptx
Algo PPT.pdf
Insertion Sort, Quick Sort And Their complexity
Algorithm & data structures lec4&5
Chapter 8 advanced sorting and hashing for print
Searching and Sorting algorithms and working
Advanced Topics In Java Core Concepts In Data Structures Noel Kalicharan
Chapter 8 Sorting in the context of DSA.pptx
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Ad

Recently uploaded (20)

PPT
Drone Technology Electronics components_1
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
PPTX
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
PDF
Structs to JSON How Go Powers REST APIs.pdf
PPTX
Practice Questions on recent development part 1.pptx
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
B.Tech (Electrical Engineering ) 2024 syllabus.pdf
PDF
Introduction to Data Science: data science process
PPTX
TE-AI-Unit VI notes using planning model
PPTX
web development for engineering and engineering
PPTX
Road Safety tips for School Kids by a k maurya.pptx
PDF
Top 10 read articles In Managing Information Technology.pdf
PPTX
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
PPTX
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
Drone Technology Electronics components_1
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
Structs to JSON How Go Powers REST APIs.pdf
Practice Questions on recent development part 1.pptx
Strings in CPP - Strings in C++ are sequences of characters used to store and...
Lesson 3_Tessellation.pptx finite Mathematics
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
B.Tech (Electrical Engineering ) 2024 syllabus.pdf
Introduction to Data Science: data science process
TE-AI-Unit VI notes using planning model
web development for engineering and engineering
Road Safety tips for School Kids by a k maurya.pptx
Top 10 read articles In Managing Information Technology.pdf
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
Ad

Searching Sorting-SELECTION ,BUBBBLE.ppt

  • 1. JSPM’s JSCOE ,HADAPSAR ,Pune-412207 Department of Computer Engineering Unit 03: Sorting and Searching Presented By: Prof .S.A. Patil
  • 2. Objectives After you have read and studied this chapter, you should be able to • Perform linear and binary search algorithms on small arrays. • Determine whether a linear or binary search is more effective for a given situation. • Perform selection and bubble sort algorithms. • Describe the heapsort algorithm and show how its performance is superior to the other two algorithms. • Apply basic sorting algorithms to sort an array of objects.
  • 3. Searching • When we maintain a collection of data, one of the operations we need is a search routine to locate desired data quickly. • Here’s the problem statement: Given a value X, return the index of X in the array, if such X exists. Otherwise, return NOT_FOUND (-1). We assume there are no duplicate entries in the array. • We will count the number of comparisons the algorithms make to analyze their performance. – The ideal searching algorithm will make the least possible number of comparisons to locate the desired data. – Two separate performance analyses are normally done, one for successful search and another for unsuccessful search.
  • 4. Search Result number 23 17 5 90 12 44 38 0 1 2 3 4 5 6 7 8 84 77 Unsuccessful Search: Successful Search: NOT_FOUND search( 45 ) search( 12 ) 4
  • 5. Linear Search • Search the array from the first to the last position in linear progression. public int linearSearch ( int[] number, int searchValue ) { int loc = 0; while (loc < number.length && number[loc] != searchValue) { loc++; } if (loc == number.length) { //Not found return NOT_FOUND; } else { return loc; //Found, return the position } }
  • 6. Linear Search Performance • We analyze the successful and unsuccessful searches separately. • We count how many times the search value is compared against the array elements. • Successful Search – Best Case – 1 comparison – Worst Case – N comparisons (N – array size) • Unsuccessful Search – Best Case = Worst Case – N comparisons
  • 7. Binary Search • If the array is sorted, then we can apply the binary search technique. • The basic idea is straightforward. First search the value in the middle position. If X is less than this value, then search the middle of the left half next. If X is greater than this value, then search the middle of the right half next. Continue in this manner. number 5 12 17 23 38 44 77 0 1 2 3 4 5 6 7 8 84 90
  • 8. Sequence of Successful Search - 1 5 12 17 23 38 44 77 0 1 2 3 4 5 6 7 8 84 90 search( 44 ) low high mid 0 8 #1 high low         2 high low mid 38 < 44 low = mid+1 = 5 mid 4
  • 9. Sequence of Successful Search - 2 5 12 17 23 38 44 77 0 1 2 3 4 5 6 7 8 84 90 search( 44 ) low high mid 0 8 #1         2 high low mid 44 < 77 high = mid-1=5 4 mid 6 high low 5 8 #2
  • 10. Sequence of Successful Search - 3 5 12 17 23 38 44 77 0 1 2 3 4 5 6 7 8 84 90 search( 44 ) low high mid 0 8 #1         2 high low mid 44 == 44 4 5 8 #2 6 high low 5 5 #3 mid 5 Successful Search!!
  • 11. Sequence of Unsuccessful Search - 1 5 12 17 23 38 44 77 0 1 2 3 4 5 6 7 8 84 90 search( 45 ) low high mid 0 8 #1 high low         2 high low mid 38 < 45 low = mid+1 = 5 mid 4
  • 12. Sequence of Unsuccessful Search - 2 5 12 17 23 38 44 77 0 1 2 3 4 5 6 7 8 84 90 search( 45 ) low high mid 0 8 #1         2 high low mid 45 < 77 high = mid-1=5 4 mid 6 high low 5 8 #2
  • 13. Sequence of Unsuccessful Search - 3 5 12 17 23 38 44 77 0 1 2 3 4 5 6 7 8 84 90 search( 45 ) low high mid 0 8 #1         2 high low mid 44 < 45 4 5 8 #2 6 high low 5 5 #3 mid 5 low = mid+1 = 6
  • 14. Sequence of Unsuccessful Search - 4 5 12 17 23 38 44 77 0 1 2 3 4 5 6 7 8 84 90 search( 45 ) low high mid 0 8 #1         2 high low mid 4 5 8 #2 6 5 5 #3 5 high low 6 5 #4 Unsuccessful Search low > high no more elements to search
  • 15. Binary Search Routine public int binarySearch (int[] number, int searchValue) { int low = 0, high = number.length - 1, mid = (low + high) / 2; while (low <= high && number[mid] != searchValue) { if (number[mid] < searchValue) { low = mid + 1; } else { //number[mid] > searchValue high = mid - 1; } mid = (low + high) / 2; //integer division will truncate } if (low > high) { mid = NOT_FOUND; } return mid; }
  • 16. Binary Search Performance • Successful Search – Best Case – 1 comparison – Worst Case – log2N comparisons • Unsuccessful Search – Best Case = Worst Case – log2N comparisons • Since the portion of an array to search is cut into half after every comparison, we compute how many times the array can be divided into halves. • After K comparisons, there will be N/2K elements in the list. We solve for K when N/2K = 1, deriving K = log2N.
  • 17. Comparing N and log2N Performance Array Size Linear – N Binary – log2N 10 10 4 50 50 6 100 100 7 500 500 9 1000 1000 10 2000 2000 11 3000 3000 12 4000 4000 12 5000 5000 13 6000 6000 13 7000 7000 13 8000 8000 13 9000 9000 14 10000 10000 14
  • 18. Sorting • When we maintain a collection of data, many applications call for rearranging the data in certain order, e.g. arranging Person information in ascending order of age. • Here’s the problem statement: Given an array of N integer values, arrange the values into ascending order. • We will count the number of comparisons the algorithms make to analyze their performance. – The ideal sorting algorithm will make the least possible number of comparisons to arrange data in a designated order. • We will compare different sorting algorithms by analyzing their worst-case performance.
  • 19. Selection Sort 1. Find the smallest element in the list. 2. Exchange the element in the first position and the smallest element. Now the smallest element is in the first position. 3. Repeat Step 1 and 2 with the list having one less element (i.e., the smallest element is discarded from further processing). 0 1 2 3 4 5 6 7 8 23 17 5 90 12 44 38 84 77 min first exchange 0 1 2 3 4 5 6 7 8 5 17 23 90 12 44 38 84 77 sorted unsorted This is the result of one pass.
  • 20. Selection Sort Passes 0 1 2 3 4 5 6 7 8 5 17 23 90 12 44 38 84 77 sorted 5 12 23 90 17 44 38 84 77 2 5 12 17 90 23 44 38 84 77 3 5 12 17 23 38 44 77 84 90 7 5 12 17 23 38 44 77 84 90 8 1 Pass # Result AFTER one pass is completed.
  • 21. Selection Sort Routine public void selectionSort(int[] number) { int startIndex, minIndex, length, temp; length = number.length; for (startIndex = 0; startIndex <= length-2; startIndex++){ //each iteration of the for loop is one pass minIndex = startIndex; //find the smallest in this pass at position minIndex for (int i = startIndex+1; i <= length-1; i++) { if (number[i] < number[minIndex]) minIndex = i; } //exchange number[startIndex] and number[minIndex] temp = number[startIndex]; number[startIndex] = number[minIndex]; number[minIndex] = temp; } }
  • 22. Selection Sort Performance • We derive the total number of comparisons by counting the number of times the inner loop is executed. • For each execution of the outer loop, the inner loop is executed length – start times. • The variable length is the size of the array. Replacing length with N, the array size, the sum is derived as…
  • 23. Bubble Sort • With the selection sort, we make one exchange at the end of one pass. • The bubble sort improves the performance by making more than one exchange during its pass. • By making multiple exchanges, we will be able to move more elements toward their correct positions using the same number of comparisons as the selection sort makes. • The key idea of the bubble sort is to make pairwise comparisons and exchange the positions of the pair if they are out of order.
  • 24. One Pass of Bubble Sort The largest value 90 is at the end of the list. 0 1 2 3 4 5 6 7 8 23 17 5 90 12 44 38 84 77 17 23 5 90 12 44 38 84 77 17 5 23 90 12 44 38 84 77 17 5 23 12 90 44 38 84 77 17 5 23 12 44 90 38 84 77 17 5 23 12 44 38 90 84 77 17 5 23 12 44 38 84 90 77 17 5 23 12 44 38 84 77 90 exchange ok exchange exchange exchange exchange exchange exchange
  • 25. Bubble Sort Routine public void bubbleSort(int[] number) { int temp, bottom, i; boolean exchanged = true; bottom = number.length - 2; while (exchanged) { exchanged = false; for (i = 0; i <= bottom; i++) { if (number[i] > number[i+1]) { temp = number[i]; //exchange number[i] = number[i+1]; number[i+1] = temp; exchanged = true; //exchange is made } } bottom--; } }

Editor's Notes

  • #6: Note: In the case of unsuccessful search, all elements in the array are compared against the search value, so the total number of comparisons is N. However, the actual number of times the while loop is executed is N+1 because we need one more go around to determine that loc becomes equal to number.length.
  • #17: Note: The value of log2N is a real number. Since the number of comparisons done is an integer, the result of log2N is rounded up to an integer in the third column.
  • #20: The figure shows the whole eight passes. The illustration for each pass shows the state right BEFORE the exchange was made for that pass. The illustrations in the slide show the state AFTER the exchange was made for that pass.
  • #24: What is the meaning behind the name “bubble” sort? Notice the effect of one pass is to migrate the largest element to the last position of the array. In addition, because we are exchanging the pairs if they are out of order, other elements migrate toward the end of the array. If we view the array vertically with the first position at the bottom and the last position at the top, the data movements we see is like bubbles moving toward the surface of water.
  • #25: The bubble sort routine is implemented by exploiting the following two properties: 1. After one pass through the array, the largest element will be at the end of the array. 2. During one pass, if no pair of consecutive entries is out of order, then the array is sorted.