Sorting and Searching in Java (1).pdf

# Now we compare the value stored at location 4 with

• No School
• AA 1
• 29

This preview shows page 8 - 16 out of 29 pages.

Now we compare the value stored at location 4, with the value being searched for i.e. 31. We find that value at location 4 is 27, which is not a match. Because value is greater than 27 and we have a sorted array so we also know that target value must be in upper portion of the array.

Subscribe to view the full document.

We change our low to mid + 1 and find the new mid value again. low = mid + 1, mid = low + (high - low) / 2 Our new mid is 7 now. We compare the value stored at location 7 with our target value 31. The value stored at location 7 is not a match, rather it is less that what we are looking for. So the value must be in lower part from this location. So we calculate the mid again. This time it is 5. We compare the value stored at location 5 with our target value. We find that it is a match.
We conclude that the target value 31 is stored at location 5. Binary search halves the searchable items and thus reduces the count of comparisons to be made to very less numbers.

Subscribe to view the full document.

public static int binarySearch(int[] data,int size, int key) { int low = 0; int high = size - 1; while(high >= low) { int middle = (low + high) / 2; if(data[middle] == key) { return key; } if(data[middle] < key) { low = middle + 1; } if(data[middle] > key) { high = middle - 1; } } return -1; }
Sorting Sorting is ordering a list of objects. We can distinguish two types of sorting. If the number of objects is small enough to fit into the main memory, sorting is called internal sorting . If the number of objects is so large that some of them reside on external storage during the sort, it is called external sorting . There are various types of sorting methods used and these include insertion sort, bubble sort, bucket sort hip sort, merge sort and selection sort

Subscribe to view the full document.

Insertion Sort This is a in-place comparison based sorting algorithm. Here, a sub-list is maintained which is always sorted. For example, the lower part of an array is maintained to be sorted. A element which is to be inserted in this sorted sub-list, has to find its appropriate place and insert it there. Hence the name insertion sort . The array is searched sequentially and unsorted items are moved and inserted into sorted sub-list (in the same array). This algorithm is not suitable for large data sets as its average and worst case complexity are of Ο(n 2 ) where n are no. of items.
Insertion Sort Algorithm Step 1 − If it is the first element, it is already sorted. return 1; Step 2 − Pick next element Step 3 − Compare with all elements in the sorted sub-list Step 4 − Shift all the elements in the sorted sub - list that is greater than the value to be sorted Step 5 − Insert the value Step 6 − Repeat until list is sorted

Subscribe to view the full document.

Consider the list of unsorted numbers below 7 34 6 23 9 28 78 32 60 Using insertion sort describe how you would sort the above numbers
• Fall '19

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern