230 likes | 353 Views
CSE 1341 Sorting & Searching. Searching & Sorting. Searching data involves determining whether a value (referred to as the search key ) is present in the data and, if so, finding its location. Sorting places data in ascending or descending order, based on one or more sort keys.
E N D
CSE 1341 Sorting & Searching
Searching & Sorting • Searching data involves determining whether a value (referred to as the search key) is present in the data and, if so, finding its location. • Sorting places data in ascending or descending order, based on one or more sort keys.
Linear Search • The linear search algorithm searches each element in an array sequentially. • If the search key does not match an element in the array, the algorithm tests each element, and when the end of the array is reached, informs the user that the search key is not present. • If the search key is in the array, the algorithm tests each element until it finds one that matches the search key and returns the index of that element. • If there are duplicate values in the array, linear search returns the index of the first element in the array that matches the search key.
Arraysstatic method toStringreturna a String representation of an array. • 5
Searching Efficiency • What is the maximum number of comparisons that is required when doing a linear search? • What if an array contained one million elements? What’s the most efficient way to look up a word in the dictionary?
Binary Search • The binary search algorithm is more efficient than linear search, but it requires that the array be sorted. • The first iteration tests the middle element in the array. If this matches the search key, the algorithm ends. • If the search key is less than the middle element, the algorithm continues with only the first half of the array. • If the search key is greater than the middle element, the algorithm continues with only the second half. • Each iteration tests the middle value of the remaining portion of the array. • If the search key does not match the element, the algorithm eliminates half of the remaining elements. • The algorithm ends by either finding an element that matches the search key or reducing the sub-array to zero size.
Arraysstatic method sort sorts the elements of an array in ascending order.
How Efficient is Binary Search? • Maximum comparisons with 10 elements… How many times can you divide a number by 2? 1,000 elements would require max of 10 comparisons 210 = 1024 OR log2(1,000) ≈ 10 1,000,000 elements would require max of 20 comparisons 220 = 1,048,576 OR log2(1,000,000) ≈ 20
Sorting Algorithms • Sorting data (i.e., placing the data into some particular order, such as ascending or descending) is one of the most important computing applications. • An important item to understand about sorting is that the end result—the sorted array—will be the same no matter which algorithm you use to sort the array. • The choice of algorithm affects only the run time and memory use of the program.
Selection Sort Find the smallest value and swap places with the first element Repeat for 2-n, 3-n, 4-n,….n-n
public static void selection_srt(int array[], int n){ for(int x=0; x<n; x++) { int index_of_min = x; for(int y=x; y<n; y++) { if(array[index_of_min]>array[y]) index_of_min = y; } //end inner for loop int temp = array[x]; array[x] = array[index_of_min]; array[index_of_min] = temp; } //end out er for loop } //end method selection_srt }//end class SelectionSort public class SelectionSort{ public static void main(String a[]) { int i; int array[] = {12,9,4,99,120,1,3,10}; System.out.println(" Selection Sort \n"); System.out.println("Values Before the sort:\n"); for(i = 0; i < array.length; i++) System.out.print( array[i]+" "); System.out.println(); selection_srt(array, array.length); System.out.print("Values after the sort:\n"); for(i = 0; i <array.length; i++) System.out.print(array[i]+" "); System.out.println(); } //end main
Bubble Sort Repeat n-1 times
public static void bubble_srt( int a[], int n ) { int i, j,t=0; for(i = 0; i < n; i++) { for(j = 1; j < (n-i); j++) { if(a[j-1] > a[j]) { t = a[j-1]; a[j-1]=a[j]; a[j]=t; } //end if } //end inner for loop } //end outer for loop } //end method bubble_srt }//end class BubbleSort public class BubbleSort { public static void main(String a[]){ int i; int array[] = {12,9,4,99,120,1,3,10}; System.out.println("Values Before the sort:\n"); for(i = 0; i < array.length; i++) System.out.print( array[i]+" "); System.out.println(); bubble_srt(array, array.length); System.out.print("Values after the sort:\n"); for(i = 0; i <array.length; i++) System.out.print(array[i]+" "); System.out.println(); }