 Hello, welcome to the lecture on sorting operation on array in Java. In this lecture, we will be discussing about how to perform sorting operation on array in Java and methods available to perform sorting operation in array class in Java library. The learning outcome of this video lecture is that the student will be able to write a Java program to perform sorting operation array using built-in sort method in array class. Here we are discussing sorting operation. Sorting is an operation performed on list of elements in which elements are arranged in order sequence, that is ascending or descending order. There are various sorting algorithm exist. Here we are discussing bubble sorting. Basic logic of bubble sorting is that the array is scanned from left to right in which each pair of successive values are compared and for ascending order sorting, if left element is smaller than right element, then do not swap these elements. Otherwise, if left element is greater than right element, then swap the element. This will be repeated for n time for n iteration where n is the total number of elements in array. Here is the sample code to demonstrate implementation of bubble sorting algorithm. First initializing integer array A with the values 4, 85, 7, 1, 0, 36, minus 5 and 48. Then loop I is to repeat procedure of arrangement of elements n times and then loop J to iterate array from left to right that is from index 0 to array length. Here is the condition to check left element is greater than right element. If the condition is true, then we are swapping the value of left element that is A of J with the right element that is A of J plus 1 as no action. Therefore, S block is not mentioned here. Last we are displaying the sorted list by displaying contents of array A. This is the input and output of sorting program. Here array of numbers sorted in ascending order. We have discussed how bubble sort works. Now arrays class in java.util package containing sort method which sort the specify array into ascending numerical order. The sorting algorithm usually is a dual pure quick sort which offers O of n login performance on many data sets that cause other quick source to degrade to quadratic performance. This is typically faster than traditional one pure quick sort implementation. Now the method is sort method is here it is a public static method. It is accessible using class name itself. Return type is void and one parameter is passed to this method is array to be sorted that is a parameter which is passed to this method. In this declaration array passed is of type integer. Here is a simple java program to demonstrate use of sort method and binary search method from arrays class. First array of integer that is array A is declared and initialize with sorted values 22, 35, 15, 10, 20. On line 8 sort method is called from arrays class to which integer array A is passed as parameter. This sort method called arrange elements from array A in ascending order. In line number 10 this sorted array is passed to binary search method from arrays class along with search key which returns search result. In this case 22 will be located at index 3 after sorting array A in ascending order. So here is the output of this program that is 22, 22nd found at index 3. There are different forms of sort method few of them we will discuss now. One form of sort method is shown here which is different than the previous form. The use of this method is to sort the specified range of array into ascending order. Whatever the range is passed to this method that range of elements only will be sorted here. The range to be sorted extends from the index from index which is including for the inclusive for the sorting up to the index that is 2 index in which the 2 index will be excluded from the sorting range. The method is public static method it is accessible using class name itself. Return type is wide and 3 parameters are passed to this method in which first parameter is integer array. Second parameter is the index of the first element of the range to be sorted. Third parameter is the index of the last element of the range to be sorted. This index element will be excluded from sorting list. Now you can pause the video for some time and answer the below question what will be the output of the following java code. Now here in this program integer array is initialized with elements 13, 7, 6, 45, 21, 9, 200. On next line of code array sort method is called in which so array passed as first parameter and second parameter is index 1 and third parameter is index 5. It means sort the array from index 1 till index 4. So here 5 will be excluded from the sorting range. Next line of code printing array elements. Therefore array after sorting is 13, 6, 7, 21, 45, 9, 2 and 100. Here complete array is not sorted. Instead only part of array elements are sorted as per the range specified in the sort method. Here few more forms of sort method specified. The first method is having character array as parameter. Second method is having float array as parameter. The third method is having double array as parameter. The fourth method is having short type array as parameter. And the fifth method is having long type array as parameter. Here we will discuss one more example. What will be the output of the following java code? Here character array is declared initialized with some values then sort method is called by passing the three parameters array. Then second is the start index from index and the third parameter is the up to index. And finally the array contents are displayed. So what will be the output of this program? Now here the sort method to the sort method character array is passed and the range given for sorting is index 1 till index 6 excluding the index 6 means it will be sorting the list from index 1 till index 5. So accordingly this it will sort from index 0 means index 1 is from character d 2, 3, 4, 5, 6 means up to the character c. It will exclude the index 6. So this it will sort the list that is there from the index from the character d h a g c. Only this particular part of this array will be sorted. So therefore output will be b then a c d g and h in the remaining part that is from index 6 f will be as it is and e will be as it is. So therefore after sorting this array character array the output will be b a c d g h f e. So we will go with one more question here. What will be the output of the following java code in which we are using the sort and binary search method from the arrays class. So in this code also we are passing the character array to the sort method but here range is not specified for the sorting. And then for binary search method we are passing this sorted array and the key value which we want to search in this particular array. So what will be the output of this particular program. So here character array a will be sorted first. So after sorting the array will be containing a c a b c d e f g h. So this is in this sorted order after sorting we are searching the position for g. So that is why output is at index 6. So after sorting a b c d e f g g will be at index position 6. So we found g character at index 6. So binary search will return the index position where that key you are going to find. Thank you for watching this video.