SlideShare a Scribd company logo
Chapter 11 Sorting  and Searching
Chapter Objectives Examine the linear search and binary search algorithms Examine several (vài)  sorting algorithms, including: selection sort insertion sort bubble sort quick sort merge sort Discuss the complexity (phức tạp)  of these algorithms
Searching Searching  is the process of finding a  target element  among a group of items (the  search pool ), or determining that it isn't there This requires repetitively comparing the target to candidates in the search pool An efficient sort performs no more comparisons than it has to The size of the search pool is a factor
The Comparable Interface We want to define the algorithms such that they can search any set of objects Therefore we will search objects that implement the  Comparable  interface It contains one method,  compareTo , which is designed to return an integer that specifies the relationship between two objects: obj1.compareTo(obj2) This call returns a number less than, equal to, or greater than 0 if  obj1  is less than, equal to, or greater than  obj2 , respectively
The Comparable Interface All of the methods presented in this chapter are part of the SortingandSearching class This class makes use of the generic type T For this class, we make the further distinction (khác biệt)  that T extends Comparable public class SortingandSearching<T extends Comparable>
Linear Search A  linear search  simply examines each item in the search pool, one at a time, until either the target is found or until the pool is exhausted This approach does not assume the items in the search pool are in any particular order We just need to be able to examine each element in turn (in a linear fashion) It's fairly easy to understand, but not very efficient
FIGURE 11.1  A linear search
linearSearch
 
Binary Search If the search pool is sorted, then we can be more efficient than a linear search A  binary search  eliminates large parts of the search pool with each comparison Instead of starting the search at one end, we begin in the middle If the target isn't found, we know that if it is in the pool at all, it is in one half or the other We can then jump to the middle of that half, and continue similarly
FIGURE 11.2  A binary search
Binary Search For example, find the number 29 in the following sorted list of numbers: 8  15  22  29  36  54  55  61  70  73  88 Compare the target to the middle value 54 We now know that if 29 is in the list, it is in the front half of the list With one comparison, we've eliminated half of the data Then compare to 22, eliminating another quarter of the data, etc.
Binary Search A binary search algorithm is often implemented recursively Each recursive call searches a smaller portion of the search pool The base case of the recursion is running out of  viable candidates  to search, which means the target is not in the search pool At any point there may be two &quot;middle&quot; values, in which case either could be used
binarySearch
Comparing Search Algorithms On average, a linear search would examine n/2 elements before finding the target Therefore, a linear search is O(n) The worst case for a binary search is (log 2 n) comparisons A binary search is a  logarithmic algorithm It has a time complexity of O(log 2 n) But keep in mind that the search pool must be sorted For large n, a binary search is much faster
Sorting Sorting  is the process of arranging a group of items into a defined order based on particular criteria Many sorting algorithms have been designed Sequential sorts  require approximately n 2  comparisons to sort n elements Logarithmic sorts  typically require nlog 2 n comparisons to sort n elements
Sorting Let's define a generic sorting problem that any of our sorting algorithms could help solve As with searching, we must be able to compare one element to another
Listing 11.1
Listing 11.1  (cont.)
Listing 11.2
Listing 11.2  (cont.)
Selection Sort Selection sort  orders a list of values by repetitively putting a particular value into its final position More specifically: find the smallest value in the list switch it with the value in the first position find the next smallest value in the list switch it with the value in the second position repeat until all values are in their proper places
FIGURE 11.3   Illustration  of selection sort processing
selectionSort
Insertion Sort Insertion sort  orders a list of values by repetitively inserting a particular value into a sorted subset of the list More specifically: consider the first item to be a sorted sublist of length 1 insert the second item into the sorted sublist, shifting the first item if needed insert the third item into the sorted sublist, shifting the other items as needed repeat until all values have been inserted into their proper positions
FIGURE 11.4   Illustration  of insertion sort processing
insertionSort
Bubble Sort Bubble sort  orders a list of values by repetitively comparing neighboring elements and swapping their positions if necessary More specifically: scan the list, exchanging adjacent elements if they are not in relative order; this bubbles the highest value to the top scan the list again, bubbling up the second highest value repeat until all elements have been placed in their proper order
bubbleSort
Comparing Sorts We've seen three sorts so far: selection sort insertion sort bubble sort They all use nested loops and perform approximately n 2  comparisons They are relatively inefficient Now we will turn our attention to more efficient sorts
Quick Sort Quick sort  orders a list of values by partitioning the list around one element, then sorting each partition More specifically: choose one element in the list to be the partition element organize the elements so that all elements less than the partition element are to the left and all greater are to the right apply the quick sort algorithm (recursively) to both partitions
Quick Sort The choice of the partition element is arbitrary For efficiency, it would be nice if the partition element divided the list roughly in half The algorithm will work in any case, however We will divide the work into two methods: quickSort  – performs the recursive algorithm findPartition  – rearranges the elements into two partitions
quickSort
findPartition
findPartition
findPartition
Merge Sort Merge sort  orders a list of values by recursively dividing 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
FIGURE 11.5   The decomposition of merge sort
FIGURE 11.6  The merge portion  of the merge sort algorithm
mergeSort
mergeSort
Efficiency of quickSort and mergeSort Both quickSort and mergeSort use a recursive structure that takes log 2 n processing steps to decompose the original list into lists of length one At each step, both algorithms either compare or merge all n elements Thus both algorithms are O(nlogn)

More Related Content

PDF
Search Algprithms
Shivam Singh
 
PPTX
Binary search
sana younas
 
PPTX
Insertion and Redix Sort
Sukhvinder Singh
 
PDF
Unit 6 dsa SEARCHING AND SORTING
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
PDF
Bca data structures linked list mrs.sowmya jyothi
Sowmya Jyothi
 
PDF
Binary search algorithm
maamir farooq
 
PPTX
Searching & Sorting Algorithms
Rahul Jamwal
 
PDF
Non-Uniform Gap Distribution Library Sort
IJCSIS Research Publications
 
Search Algprithms
Shivam Singh
 
Binary search
sana younas
 
Insertion and Redix Sort
Sukhvinder Singh
 
Bca data structures linked list mrs.sowmya jyothi
Sowmya Jyothi
 
Binary search algorithm
maamir farooq
 
Searching & Sorting Algorithms
Rahul Jamwal
 
Non-Uniform Gap Distribution Library Sort
IJCSIS Research Publications
 

What's hot (16)

PPTX
data structures and algorithms Unit 3
infanciaj
 
PPT
Searching Sorting
guest2cb109
 
PPTX
Binary search python
MaryamAnwar10
 
PPT
Linked list
Harry Potter
 
PPTX
Searching
Ashim Lamichhane
 
PPT
L10 sorting-searching
mondalakash2012
 
PPT
List data structure
Mahmoud Abuwarda
 
PPT
Sorting algorithms
CHANDAN KUMAR
 
PPTX
poornima.coseq
ramyaraigupta
 
PPT
Binary Search
kunj desai
 
PPT
Ch 1 intriductions
irshad17
 
PPTX
Searching linear &amp; binary search
nikunjandy
 
PDF
linear search and binary search
Zia Ush Shamszaman
 
PPT
Searching in c language
CHANDAN KUMAR
 
PPTX
Rahat &amp; juhith
Rj Juhith
 
PPTX
Data structure unit I part B
SSN College of Engineering, Kalavakkam
 
data structures and algorithms Unit 3
infanciaj
 
Searching Sorting
guest2cb109
 
Binary search python
MaryamAnwar10
 
Linked list
Harry Potter
 
Searching
Ashim Lamichhane
 
L10 sorting-searching
mondalakash2012
 
List data structure
Mahmoud Abuwarda
 
Sorting algorithms
CHANDAN KUMAR
 
poornima.coseq
ramyaraigupta
 
Binary Search
kunj desai
 
Ch 1 intriductions
irshad17
 
Searching linear &amp; binary search
nikunjandy
 
linear search and binary search
Zia Ush Shamszaman
 
Searching in c language
CHANDAN KUMAR
 
Rahat &amp; juhith
Rj Juhith
 
Data structure unit I part B
SSN College of Engineering, Kalavakkam
 
Ad

Similar to Ch05 Black Jack (20)

PPTX
LLL3 Searching & Sorting algorithms.pptx
plabonrahmanme
 
PPTX
Searching_Sorting.pptx
21BD1A058RSahithi
 
PPTX
Chapter 2. data structure and algorithm
SolomonEndalu
 
PPTX
DFC30233_CHAPTER 6 (1).pptxxxxxxxxxxxxxxxxxxxxxxxx
rajinevitable05
 
PPTX
Searching,sorting
LavanyaJ28
 
PPTX
2.Problem Solving Techniques and Data Structures.pptx
Ganesh Bhosale
 
PPTX
searching in data structure.pptx
chouguleamruta24
 
PPTX
All Searching and Sorting Techniques in Data Structures
sonalishinge2015
 
ODP
Data operatons & searching and sorting algorithms
Anushdika Jeganathan
 
PPSX
Data Structure and Algorithm Chapter 2.ppsx Chapter 2.ppsx
SolomonEndalu
 
PPTX
Searching, Sorting and Hashing Techniques
Selvaraj Seerangan
 
PPTX
sorting-160810203705.pptx
VarchasvaTiwari2
 
PDF
advanced searching and sorting.pdf
haramaya university
 
PDF
Sorting algorithm
Balaji Nangare
 
PPTX
Algorithm & data structures lec4&5
Abdul Khan
 
PDF
Sorting and Searching Techniques
Prof Ansari
 
PPTX
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
PPTX
Data Structures_ Sorting & Searching
ThenmozhiK5
 
PPTX
Data Structures Unit 2 FINAL presentation.pptx
dilipd20
 
PPTX
Different Searching and Sorting Methods.pptx
Minakshee Patil
 
LLL3 Searching & Sorting algorithms.pptx
plabonrahmanme
 
Searching_Sorting.pptx
21BD1A058RSahithi
 
Chapter 2. data structure and algorithm
SolomonEndalu
 
DFC30233_CHAPTER 6 (1).pptxxxxxxxxxxxxxxxxxxxxxxxx
rajinevitable05
 
Searching,sorting
LavanyaJ28
 
2.Problem Solving Techniques and Data Structures.pptx
Ganesh Bhosale
 
searching in data structure.pptx
chouguleamruta24
 
All Searching and Sorting Techniques in Data Structures
sonalishinge2015
 
Data operatons & searching and sorting algorithms
Anushdika Jeganathan
 
Data Structure and Algorithm Chapter 2.ppsx Chapter 2.ppsx
SolomonEndalu
 
Searching, Sorting and Hashing Techniques
Selvaraj Seerangan
 
sorting-160810203705.pptx
VarchasvaTiwari2
 
advanced searching and sorting.pdf
haramaya university
 
Sorting algorithm
Balaji Nangare
 
Algorithm & data structures lec4&5
Abdul Khan
 
Sorting and Searching Techniques
Prof Ansari
 
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
Data Structures_ Sorting & Searching
ThenmozhiK5
 
Data Structures Unit 2 FINAL presentation.pptx
dilipd20
 
Different Searching and Sorting Methods.pptx
Minakshee Patil
 
Ad

More from leminhvuong (20)

PPTX
Proxy
leminhvuong
 
PPT
Lession2 Xinetd
leminhvuong
 
PPT
Module 7 Sql Injection
leminhvuong
 
PPT
Iptables
leminhvuong
 
PPT
Lession1 Linux Preview
leminhvuong
 
PPT
Http
leminhvuong
 
PPT
Dns
leminhvuong
 
PPT
Net Admin Intro
leminhvuong
 
PPT
Lession4 Dhcp
leminhvuong
 
PPT
Lession3 Routing
leminhvuong
 
PPT
Module 1 Introduction
leminhvuong
 
PPT
Wire Less
leminhvuong
 
PPT
Net Security Intro
leminhvuong
 
PPT
Module 10 Physical Security
leminhvuong
 
PPT
Module 9 Dos
leminhvuong
 
PPT
Module 8 System Hacking
leminhvuong
 
PPT
Module 6 Session Hijacking
leminhvuong
 
PPT
Module 5 Sniffers
leminhvuong
 
PPT
Module 4 Enumeration
leminhvuong
 
PPT
Module 3 Scanning
leminhvuong
 
Lession2 Xinetd
leminhvuong
 
Module 7 Sql Injection
leminhvuong
 
Iptables
leminhvuong
 
Lession1 Linux Preview
leminhvuong
 
Net Admin Intro
leminhvuong
 
Lession4 Dhcp
leminhvuong
 
Lession3 Routing
leminhvuong
 
Module 1 Introduction
leminhvuong
 
Wire Less
leminhvuong
 
Net Security Intro
leminhvuong
 
Module 10 Physical Security
leminhvuong
 
Module 9 Dos
leminhvuong
 
Module 8 System Hacking
leminhvuong
 
Module 6 Session Hijacking
leminhvuong
 
Module 5 Sniffers
leminhvuong
 
Module 4 Enumeration
leminhvuong
 
Module 3 Scanning
leminhvuong
 

Ch05 Black Jack

  • 1. Chapter 11 Sorting and Searching
  • 2. Chapter Objectives Examine the linear search and binary search algorithms Examine several (vài) sorting algorithms, including: selection sort insertion sort bubble sort quick sort merge sort Discuss the complexity (phức tạp) of these algorithms
  • 3. Searching Searching is the process of finding a target element among a group of items (the search pool ), or determining that it isn't there This requires repetitively comparing the target to candidates in the search pool An efficient sort performs no more comparisons than it has to The size of the search pool is a factor
  • 4. The Comparable Interface We want to define the algorithms such that they can search any set of objects Therefore we will search objects that implement the Comparable interface It contains one method, compareTo , which is designed to return an integer that specifies the relationship between two objects: obj1.compareTo(obj2) This call returns a number less than, equal to, or greater than 0 if obj1 is less than, equal to, or greater than obj2 , respectively
  • 5. The Comparable Interface All of the methods presented in this chapter are part of the SortingandSearching class This class makes use of the generic type T For this class, we make the further distinction (khác biệt) that T extends Comparable public class SortingandSearching<T extends Comparable>
  • 6. Linear Search A linear search simply examines each item in the search pool, one at a time, until either the target is found or until the pool is exhausted This approach does not assume the items in the search pool are in any particular order We just need to be able to examine each element in turn (in a linear fashion) It's fairly easy to understand, but not very efficient
  • 7. FIGURE 11.1 A linear search
  • 9.  
  • 10. Binary Search If the search pool is sorted, then we can be more efficient than a linear search A binary search eliminates large parts of the search pool with each comparison Instead of starting the search at one end, we begin in the middle If the target isn't found, we know that if it is in the pool at all, it is in one half or the other We can then jump to the middle of that half, and continue similarly
  • 11. FIGURE 11.2 A binary search
  • 12. Binary Search For example, find the number 29 in the following sorted list of numbers: 8 15 22 29 36 54 55 61 70 73 88 Compare the target to the middle value 54 We now know that if 29 is in the list, it is in the front half of the list With one comparison, we've eliminated half of the data Then compare to 22, eliminating another quarter of the data, etc.
  • 13. Binary Search A binary search algorithm is often implemented recursively Each recursive call searches a smaller portion of the search pool The base case of the recursion is running out of viable candidates to search, which means the target is not in the search pool At any point there may be two &quot;middle&quot; values, in which case either could be used
  • 15. Comparing Search Algorithms On average, a linear search would examine n/2 elements before finding the target Therefore, a linear search is O(n) The worst case for a binary search is (log 2 n) comparisons A binary search is a logarithmic algorithm It has a time complexity of O(log 2 n) But keep in mind that the search pool must be sorted For large n, a binary search is much faster
  • 16. Sorting Sorting is the process of arranging a group of items into a defined order based on particular criteria Many sorting algorithms have been designed Sequential sorts require approximately n 2 comparisons to sort n elements Logarithmic sorts typically require nlog 2 n comparisons to sort n elements
  • 17. Sorting Let's define a generic sorting problem that any of our sorting algorithms could help solve As with searching, we must be able to compare one element to another
  • 19. Listing 11.1 (cont.)
  • 21. Listing 11.2 (cont.)
  • 22. Selection Sort Selection sort orders a list of values by repetitively putting a particular value into its final position More specifically: find the smallest value in the list switch it with the value in the first position find the next smallest value in the list switch it with the value in the second position repeat until all values are in their proper places
  • 23. FIGURE 11.3 Illustration of selection sort processing
  • 25. Insertion Sort Insertion sort orders a list of values by repetitively inserting a particular value into a sorted subset of the list More specifically: consider the first item to be a sorted sublist of length 1 insert the second item into the sorted sublist, shifting the first item if needed insert the third item into the sorted sublist, shifting the other items as needed repeat until all values have been inserted into their proper positions
  • 26. FIGURE 11.4 Illustration of insertion sort processing
  • 28. Bubble Sort Bubble sort orders a list of values by repetitively comparing neighboring elements and swapping their positions if necessary More specifically: scan the list, exchanging adjacent elements if they are not in relative order; this bubbles the highest value to the top scan the list again, bubbling up the second highest value repeat until all elements have been placed in their proper order
  • 30. Comparing Sorts We've seen three sorts so far: selection sort insertion sort bubble sort They all use nested loops and perform approximately n 2 comparisons They are relatively inefficient Now we will turn our attention to more efficient sorts
  • 31. Quick Sort Quick sort orders a list of values by partitioning the list around one element, then sorting each partition More specifically: choose one element in the list to be the partition element organize the elements so that all elements less than the partition element are to the left and all greater are to the right apply the quick sort algorithm (recursively) to both partitions
  • 32. Quick Sort The choice of the partition element is arbitrary For efficiency, it would be nice if the partition element divided the list roughly in half The algorithm will work in any case, however We will divide the work into two methods: quickSort – performs the recursive algorithm findPartition – rearranges the elements into two partitions
  • 37. Merge Sort Merge sort orders a list of values by recursively dividing 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
  • 38. FIGURE 11.5 The decomposition of merge sort
  • 39. FIGURE 11.6 The merge portion of the merge sort algorithm
  • 42. Efficiency of quickSort and mergeSort Both quickSort and mergeSort use a recursive structure that takes log 2 n processing steps to decompose the original list into lists of length one At each step, both algorithms either compare or merge all n elements Thus both algorithms are O(nlogn)