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.
Image of page 8

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.
Image of page 9
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.
Image of page 10

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; }
Image of page 11
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
Image of page 12

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.
Image of page 13
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
Image of page 14

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
Image of page 15
Image of page 16

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern