![]() Length of sorted sub-array is 1 and unsorted sub-array is 3. In each pass, it finds the minimum element of the unsorted sub-array and swaps it with the leftmost unsorted element moving the sorted sub-array one element to the right.Īt the start, the smallest element of the array is selected (which is 2) and swapped with the element at 0 th index (which is 9): 2Īt the end of the first pass, the smallest element is in its correct position. Selection Sort divides the array into two parts - sorted sub-array and unsorted sub-array. Question 5Įxplain the technique of Selection Sort with an example. ![]() With this, the third and final pass ends and the elements of the array are in sorted order. Next, 5 is compared with 3 and as 5 is greater than 3 the elements are swapped: 2Īt the end of first pass, the second highest element of the array is in its correct position.Ģ is compared with 3 and as 2 is less then 3 no swapping takes place. Next, 9 is compared with 3 and as 9 is greater than 3 the elements are swapped: 5Īt the end of first pass, the highest element of the array is at the last position.ĥ is compared with 2 and as 5 is greater than 2 the elements are swapped: 2 Next, 9 is compared with 2 and as 9 is greater than 2 the elements are swapped: 5 Question 4Įxplain the technique of Bubble Sort with an example.īubble Sort is a sorting algorithm that works by repeatedly iterating through the array, comparing each pair of adjoining elements and swapping them if they are in wrong order.įor example, consider the following unsorted array: 9įirst 9 is compared with 5 and as 9 is greater than 5 the elements are swapped: 5 It will keep doing this in a linear progression and when it reaches the element at index 3, it finds a match so it will give us this index 3 which means that 7 is present at index 3 of array arr. Linear search will first check if 1 is equal to 7, then it will move on to the next element which is 8. We want to check if 7 is present in the array or not. As soon as the search value is found, the algorithm quits and returns the position (index) of the target value in the array.įor example, consider the following array: In linear search, we start at the first element of the array and sequentially check each element of the list for the search value until a match is found or all the elements have been searched. ![]() How does the linear search find an element in the array? Explain your answer with a suitable example. Performs more number of swaps to sort the array Performs lesser number of swaps to sort the same array relative to Bubble Sort Selection Sort selects the smallest element from unsorted sub-array and swaps it with the leftmost unsorted element.īubble Sort compares adjacent elements and swaps them if they are in wrong order. Linear search works on sorted and unsorted arraysīinary search works on only sorted arrays (ascending or descending)Įach element of the array is checked against the target value until the element is found or end of the array is reachedĪrray is successively divided into 2 halves and the target element is searched either in the first half or in the second half Linear search and Binary search are examples of search techniques. Searching means to search for a term or value in an array.īubble sort and Selection sort are examples of sorting techniques. Sorting means to arrange the elements of the array in ascending or descending order. Int a is an array of int data type that can hold 10 integer values whereas char a is an array of char data type that can hold 10 characters. it stores values in the memory reserved for the array elements. Array Initialisation assigns values to the array elements i.e. Write a program to initialise the given data in an array and find the minimum and maximum values along with the sum of the given elements.Īrray declaration tells the compiler about the size and data type of the array so that the compiler can reserve the required memory for the array. ![]() search, the algorithm uses the middle value of the array for the search operation.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |