Sorting and Searching in Java (1).pdf

Bubble sort let us consider the list of elements

  • No School
  • AA 1
  • 29

This preview shows page 20 - 29 out of 29 pages.

Bubble Sort
Image of page 20

Subscribe to view the full document.

Let us consider the list of elements above. To sort these elements using bubble sort we first compare the first two elements and sort them but in this case 14 and 33 are already sorted. Next we compare 33 and 27, since 27 is less than 33 we swap the two elements as below Now we compare 33 and 35, in this case these two elements are sorted hence we retain the order and move on to compare the last two which are 35 and 10. Since 10 is less than 35 the two elements are swapped hence the structure in the next slide.
Image of page 21
This is how the array will look like in the first iteration. The above steps are repeated until the array is sorted hence the following structures show how the array elements would be after each iteration.
Image of page 22

Subscribe to view the full document.

Bubble Sort Algorithm begin BubbleSort(list) for all elements of list if list[i] > list[i+1] swap(list[i], list[i+1]) end if end for return list end BubbleSort
Image of page 23
public class BubbleSort { private static void bubbleSort(int[] intArray) { int n = intArray.length; int temp = 0; for(int i=0; i < n; i++){ for(int j=1; j < (n-i); j++){ if(intArray[j-1] > intArray[j]){ temp = intArray[j-1]; intArray[j-1] = intArray[j]; intArray[j] = temp; } } } } public static void main(String[] args) { int []x = {56,3,57,89,34,67,8,89,100}; bubbleSort(x); for (int i=0; i<x.length; i++){ System.out.print(x[i]+" ");} } }
Image of page 24

Subscribe to view the full document.

Merge Sort Merge sort is a sorting technique based on divide and conquer technique. With worst- case time complexity being Ο(n log n), it is one of the most respected algorithms. Merge sort first divides the array into equal halves and then combines them in a sorted manner.
Image of page 25
To show how merge sort works consider the list below: We know that merge sort first divides the whole array iteratively into equal halves unless the atomic values are achieved. We see here that an array of 8 items is divided into two arrays of size 4. These two halves can further be divided into sub groups as shown below We further divide these until atomic values are reached The above structure shows that the atomic values have been reached and we cannot further divide the list any further.
Image of page 26

Subscribe to view the full document.

Now, we combine them in exactly same manner they were broken down. Please note the color codes given to these lists. We first compare the element for each list and then combine them into another list in sorted manner. In next iteration of combining phase, we compare lists of two data values, and merge them into a list of found data values placing all in sorted order. After final merging, the list should look like the following structure:
Image of page 27
Image of page 28

Subscribe to view the full document.

END
Image of page 29

{[ 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