Download presentation

Presentation is loading. Please wait.

Published byNoelia Newberry Modified over 4 years ago

1
CHAPTER 12. SEARCHING, SORTING AND THE MANIPULATION OF ELEMENTS IN AN ARRAY.

2
SEARCHING Linear Search. Int search ( int [] a, int searchValue) { for (int i = 0 ; i< a.length; i++) if (a[i] == searchValue) return i return -1; }

3
Searching an Array of Objects int search ( int [] a, String searchValue) { for (int i = 0 ; i< a.length; i++) if (a[i].equals(searchValue)) return i return -1; }

4
Generalized method. This method can be generalized to work with objects. Just substitute Object for String and the method will still work for Strings but also for Student objects.

5
int search ( Object [] a, Object searchValue) { for (int i = 0 ; i< a.length; i++) if (a[i].equals(searchValue)) return i return -1; }

6
Now the main method could use it. Assuming that the Student class has an appropiate equals method. String [] stringArray = { “Hi”, “there”, “Martin”}; Student [] studentArray = new Student [5]; Student stu = new Student(“Student 1”); for (int i = 0 ; i < studentArray.length; i++) studentArray[i] = new Student (“Student” + (i+1)); int StringPos = search(stringArray, “Martin”); Int StudentPos = search(studentArray, stu);

7
Binary Search Linear Search is done for a few hundreds of elements. When there is an array of elements in an ascending order there is a much better way to do searching. When people search for a number in the phone book they estimate the result in non linear way. Binary search is much faster method for very large arrays.

8
1 17 15 11 7 13 3 9 5

9
IMPLEMENTATION OF THE CODE int search(int [] a, int searchValue){ int left = 0; int right = a.length – 1; while (left <= right){ int midpoint = (left + right/2); if (a[midpoint] == searchValue) return midpoint; else if ( a[midpoint] < searchValue) left = midpoint + 1; else right = midpoint – 1 } return -1; }

10
CompareTo method. Usage of CompareToVALUE RETURNED obj1.compareTo(obj2)0 if obj1 is equal to obj2 Obj1.compareTo(obj2)A negative integer if obj1 is less than obj2 Obj1.compareTo(obj2A positive integer if obj1 is greater than obj2 String obj1 = “Mary”; String obj2 = “Suzanne”; String obj3 = “Bob”; System.out.println(obj1.compareTo(obj2); //returns -6 System.out.println(obj1.compareTo(obj3); //returns 11

11
Sorting We have seen that making algorithms to search arrays in a linear way and using binary search are efficient if the information is organized in an ascending order. But what if the data is not organized.

12
Sort Idea The basic idea of a selection sort is as follows. For each index position i Find the smallest data value in the array from positions i. Through length -1, where length is the number of data values stored. Exchange the smallest value with the value at position i.

13
Note the following before writing the code If the array is of length n, we need n-1 We must be able to find the smalles number We need to exchange appropriate array items

14
THE SORTING GAME UNSOR TED ARRAY AFTER 1ST PASS AFTER 2ND PASS AFTER 3RD PASS AFTER 4TH PASS AFTER 5TH PASS AFTER 6TH PASS AFTER 7TH PASS AFTER 8TH PASS 411111111 222222222 555333333 144444444 777775555 666666666 888888877 999999998 333557789

15
HERE IS THE CODE… void selectionSort(int [], a){ for (int i=0; i < a.length – 1; i++){ int minIndex = findMinimum(a,i); if (int minIndex != i) swap(a,i,minIndex); }

16
findMinimum method int findMinimum (int [] a, int first){ minIndex = first; for (int i = first + 1; i<a.length; i++) if (a[i] < a[minIndex]) minIndex = i; return minIndex; }

17
Swap method Void swap (int[] a, int x, int y){ int temp = a[x]; a[x] = a[y]; a[y]= temp; }

18
BUBBLE SORT The bubble sort algorithm involves a nested loop structure. The outer loop controls the number of (successively smaller) passses through the array. The inner loop controls the pairs of adjacent items being compared. If we ever make a complete pass through the inner loop without having to make an interchange, we can declare the array sorted and avoid all future passes through teh array.

19
BUBBLE SORT void bubleSort (int [] a){ int k = 0; boolean exchangeMade = true; while ( ((k< a.length -1) && exchangeMade){ exchangeMade = false; k++; for (int j= 0 ; j < a.length – k; j++) if (a[j] > a[j + 1]) swap(a,j,j+1); exchangeMade = true ; }

20
INSERTION SORT Watch this youtube video to understand how insertion sort works. http://www.youtube.com/watch?v=Fr0SmtN0IJ M

21
Insertion Sort Algorithm void insertionSort(int[] a){ int itemToInsert; boolean stillLooking; for (int k = 1; k <a.length; k++){ itemToInsert = a[k]; j = k-1; while ((j>=0) && stillLooking){ if (itemToInsert < a[j]){ a[j+1]=a[j]; J--; } else stillLooking = false; } a[j+1] = itemToInsert; }

22
After understanding the three techniques of sorting see this video http://www.youtube.com/watch?v=k4RRi_nt Qc8 http://www.youtube.com/watch?v=k4RRi_nt Qc8

Similar presentations

OK

CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.

CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google