CHAPTER 12. SEARCHING, SORTING AND THE MANIPULATION OF ELEMENTS IN AN ARRAY.
-
Upload
noelia-newberry -
Category
Documents
-
view
235 -
download
2
Transcript of CHAPTER 12. SEARCHING, SORTING AND THE MANIPULATION OF ELEMENTS IN AN ARRAY.
![Page 1: CHAPTER 12. SEARCHING, SORTING AND THE MANIPULATION OF ELEMENTS IN AN ARRAY.](https://reader036.fdocuments.in/reader036/viewer/2022062405/551a9ae5550346e0158b560f/html5/thumbnails/1.jpg)
CHAPTER 12.
SEARCHING, SORTING AND THE MANIPULATION OF ELEMENTS IN AN
ARRAY.
![Page 2: CHAPTER 12. SEARCHING, SORTING AND THE MANIPULATION OF ELEMENTS IN AN ARRAY.](https://reader036.fdocuments.in/reader036/viewer/2022062405/551a9ae5550346e0158b560f/html5/thumbnails/2.jpg)
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;}
![Page 3: CHAPTER 12. SEARCHING, SORTING AND THE MANIPULATION OF ELEMENTS IN AN ARRAY.](https://reader036.fdocuments.in/reader036/viewer/2022062405/551a9ae5550346e0158b560f/html5/thumbnails/3.jpg)
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;}
![Page 4: CHAPTER 12. SEARCHING, SORTING AND THE MANIPULATION OF ELEMENTS IN AN ARRAY.](https://reader036.fdocuments.in/reader036/viewer/2022062405/551a9ae5550346e0158b560f/html5/thumbnails/4.jpg)
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.
![Page 5: CHAPTER 12. SEARCHING, SORTING AND THE MANIPULATION OF ELEMENTS IN AN ARRAY.](https://reader036.fdocuments.in/reader036/viewer/2022062405/551a9ae5550346e0158b560f/html5/thumbnails/5.jpg)
int search ( Object [] a, Object searchValue) {for (int i = 0 ; i< a.length; i++)
if (a[i].equals(searchValue))return i
return -1;}
![Page 6: CHAPTER 12. SEARCHING, SORTING AND THE MANIPULATION OF ELEMENTS IN AN ARRAY.](https://reader036.fdocuments.in/reader036/viewer/2022062405/551a9ae5550346e0158b560f/html5/thumbnails/6.jpg)
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);
![Page 7: CHAPTER 12. SEARCHING, SORTING AND THE MANIPULATION OF ELEMENTS IN AN ARRAY.](https://reader036.fdocuments.in/reader036/viewer/2022062405/551a9ae5550346e0158b560f/html5/thumbnails/7.jpg)
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.
![Page 8: CHAPTER 12. SEARCHING, SORTING AND THE MANIPULATION OF ELEMENTS IN AN ARRAY.](https://reader036.fdocuments.in/reader036/viewer/2022062405/551a9ae5550346e0158b560f/html5/thumbnails/8.jpg)
117
15 117
13
39
5
![Page 9: CHAPTER 12. SEARCHING, SORTING AND THE MANIPULATION OF ELEMENTS IN AN ARRAY.](https://reader036.fdocuments.in/reader036/viewer/2022062405/551a9ae5550346e0158b560f/html5/thumbnails/9.jpg)
IMPLEMENTATION OF THE CODEint 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;
}
![Page 10: CHAPTER 12. SEARCHING, SORTING AND THE MANIPULATION OF ELEMENTS IN AN ARRAY.](https://reader036.fdocuments.in/reader036/viewer/2022062405/551a9ae5550346e0158b560f/html5/thumbnails/10.jpg)
CompareTo method.Usage of CompareTo VALUE 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(obj2 A positive integer if obj1 is greater than obj2
String obj1 = “Mary”;String obj2 = “Suzanne”;String obj3 = “Bob”;
System.out.println(obj1.compareTo(obj2); //returns -6System.out.println(obj1.compareTo(obj3); //returns 11
![Page 11: CHAPTER 12. SEARCHING, SORTING AND THE MANIPULATION OF ELEMENTS IN AN ARRAY.](https://reader036.fdocuments.in/reader036/viewer/2022062405/551a9ae5550346e0158b560f/html5/thumbnails/11.jpg)
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.
4
5
7
6
2
2
4
5
6
7
![Page 12: CHAPTER 12. SEARCHING, SORTING AND THE MANIPULATION OF ELEMENTS IN AN ARRAY.](https://reader036.fdocuments.in/reader036/viewer/2022062405/551a9ae5550346e0158b560f/html5/thumbnails/12.jpg)
Sort Idea
The basic idea of a selection sort is as follows.
For each index position iFind 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.
![Page 13: CHAPTER 12. SEARCHING, SORTING AND THE MANIPULATION OF ELEMENTS IN AN ARRAY.](https://reader036.fdocuments.in/reader036/viewer/2022062405/551a9ae5550346e0158b560f/html5/thumbnails/13.jpg)
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
![Page 14: CHAPTER 12. SEARCHING, SORTING AND THE MANIPULATION OF ELEMENTS IN AN ARRAY.](https://reader036.fdocuments.in/reader036/viewer/2022062405/551a9ae5550346e0158b560f/html5/thumbnails/14.jpg)
THE SORTING GAMEUNSORTED 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
4 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2
5 5 5 3 3 3 3 3 3
1 4 4 4 4 4 4 4 4
7 7 7 7 7 5 5 5 5
6 6 6 6 6 6 6 6 6
8 8 8 8 8 8 8 7 7
9 9 9 9 9 9 9 9 8
3 3 3 5 5 7 7 8 9
![Page 15: CHAPTER 12. SEARCHING, SORTING AND THE MANIPULATION OF ELEMENTS IN AN ARRAY.](https://reader036.fdocuments.in/reader036/viewer/2022062405/551a9ae5550346e0158b560f/html5/thumbnails/15.jpg)
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);}
}
![Page 16: CHAPTER 12. SEARCHING, SORTING AND THE MANIPULATION OF ELEMENTS IN AN ARRAY.](https://reader036.fdocuments.in/reader036/viewer/2022062405/551a9ae5550346e0158b560f/html5/thumbnails/16.jpg)
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;}
![Page 17: CHAPTER 12. SEARCHING, SORTING AND THE MANIPULATION OF ELEMENTS IN AN ARRAY.](https://reader036.fdocuments.in/reader036/viewer/2022062405/551a9ae5550346e0158b560f/html5/thumbnails/17.jpg)
Swap method
Void swap (int[] a, int x, int y){int temp = a[x];a[x] = a[y];a[y]= temp;
}
![Page 18: CHAPTER 12. SEARCHING, SORTING AND THE MANIPULATION OF ELEMENTS IN AN ARRAY.](https://reader036.fdocuments.in/reader036/viewer/2022062405/551a9ae5550346e0158b560f/html5/thumbnails/18.jpg)
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.
![Page 19: CHAPTER 12. SEARCHING, SORTING AND THE MANIPULATION OF ELEMENTS IN AN ARRAY.](https://reader036.fdocuments.in/reader036/viewer/2022062405/551a9ae5550346e0158b560f/html5/thumbnails/19.jpg)
BUBBLE SORTvoid 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 ;
}}
![Page 20: CHAPTER 12. SEARCHING, SORTING AND THE MANIPULATION OF ELEMENTS IN AN ARRAY.](https://reader036.fdocuments.in/reader036/viewer/2022062405/551a9ae5550346e0158b560f/html5/thumbnails/20.jpg)
INSERTION SORT
Watch this youtube video to understand how insertion sort works.
http://www.youtube.com/watch?v=Fr0SmtN0IJM
![Page 21: CHAPTER 12. SEARCHING, SORTING AND THE MANIPULATION OF ELEMENTS IN AN ARRAY.](https://reader036.fdocuments.in/reader036/viewer/2022062405/551a9ae5550346e0158b560f/html5/thumbnails/21.jpg)
Insertion Sort Algorithmvoid 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;
}}
![Page 22: CHAPTER 12. SEARCHING, SORTING AND THE MANIPULATION OF ELEMENTS IN AN ARRAY.](https://reader036.fdocuments.in/reader036/viewer/2022062405/551a9ae5550346e0158b560f/html5/thumbnails/22.jpg)
After understanding the three techniques of sorting see this video
• http://www.youtube.com/watch?v=k4RRi_ntQc8