chapter 12. searching, sorting and the manipulation of elements in an array
TRANSCRIPT
CHAPTER 12.
SEARCHING, SORTING AND THE MANIPULATION OF ELEMENTS IN AN
ARRAY.
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;}
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;}
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.
int search ( Object [] a, Object searchValue) {for (int i = 0 ; i< a.length; i++)
if (a[i].equals(searchValue))return i
return -1;}
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);
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.
117
15 117
13
39
5
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;
}
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
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
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.
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
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
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);}
}
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;}
Swap method
Void swap (int[] a, int x, int y){int temp = a[x];a[x] = a[y];a[y]= temp;
}
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.
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 ;
}}
INSERTION SORT
Watch this youtube video to understand how insertion sort works.
http://www.youtube.com/watch?v=Fr0SmtN0IJM
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;
}}
After understanding the three techniques of sorting see this video
• http://www.youtube.com/watch?v=k4RRi_ntQc8