Searching Sorting
Algorithm for Linear Search:
Step 1:Read the search element from the user
Step 2:Compare, the search element with the first element in the list.
Step 3:If both are matching, then Print:Given element found!!! and terminate the function
Step 4:If both are not matching, then compare search element with the next element in the list.
Step 5:Repeat steps 3 and 4 until the search element is compared with the last element in the list.
Step 6:If the last element in the list is also doesn match, then Print:Element not found!!! and terminate the function.
Algorithm for Binary Search:
Step 1:Read the search element from the user
Step 2:Find the middle element in the sorted list
Step 3:Compare, the search element with the middle element in the sorted list.
Step 4:If both are matching, then
Print:Given element found!!! and terminate the function
Step 5:If both are not matching, then check whether the search element is smaller or larger than middle element.
Step 6:If the search element is smaller than middle element, then repeat steps 2, 3, 4 and 5 for the left sublist of the middle element.
Step 7:If the search element is larger than middle element, then repeat steps 2, 3, 4 and 5 for the right sublist of the middle element.
Step 8:Repeat the same process until we find the search element in the list or until sublist contains only one element.
Step 9:If that element also doesn't match with the search element, then Print: Element not found in the list!!! \nand terminate the function.
Algorithm for Insertion Sort:
Step 1:Assume that first element in the list is in sorted portion of the list and remaining all elements are in unsorted portion.
Step 2:Consider first element from the unsorted list and insert that element into the sorted list in order specified.
Step 3:Repeat the above process until all the elements from the unsorted list are moved into the sorted list.
Algorithm for Selection Sort:
Step 1:Select the first element of the list(i.e., Element at first position in the list).
Step 2:Compare the selected element with all other elements in the list.
Step 3:For every comparison,if any element is smaller than selected element( for Ascending order),then these two are swapped.
Step 4:Repeat the same procedure with next position in the list till the entire list is sorted.
Algorithm for Shell Sort:
Step 1:Initialize the value of h
Step 2:Divide the list into smaller sub -list of equal interval h
Step 3:Sort these sub - lists using insertion sort
Step 4:Repeat until complete list is sorted.
Algorithm for Bubble Sort:
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
Algorithm for Linear Search:
Step 1:Read the search element from the user
Step 2:Compare, the search element with the first element in the list.
Step 3:If both are matching, then Print:Given element found!!! and terminate the function
Step 4:If both are not matching, then compare search element with the next element in the list.
Step 5:Repeat steps 3 and 4 until the search element is compared with the last element in the list.
Step 6:If the last element in the list is also doesn match, then Print:Element not found!!! and terminate the function.
Algorithm for Binary Search:
Step 1:Read the search element from the user
Step 2:Find the middle element in the sorted list
Step 3:Compare, the search element with the middle element in the sorted list.
Step 4:If both are matching, then
Print:Given element found!!! and terminate the function
Step 5:If both are not matching, then check whether the search element is smaller or larger than middle element.
Step 6:If the search element is smaller than middle element, then repeat steps 2, 3, 4 and 5 for the left sublist of the middle element.
Step 7:If the search element is larger than middle element, then repeat steps 2, 3, 4 and 5 for the right sublist of the middle element.
Step 8:Repeat the same process until we find the search element in the list or until sublist contains only one element.
Step 9:If that element also doesn't match with the search element, then Print: Element not found in the list!!! \nand terminate the function.
Algorithm for Insertion Sort:
Step 1:Assume that first element in the list is in sorted portion of the list and remaining all elements are in unsorted portion.
Step 2:Consider first element from the unsorted list and insert that element into the sorted list in order specified.
Step 3:Repeat the above process until all the elements from the unsorted list are moved into the sorted list.
Algorithm for Selection Sort:
Step 1:Select the first element of the list(i.e., Element at first position in the list).
Step 2:Compare the selected element with all other elements in the list.
Step 3:For every comparison,if any element is smaller than selected element( for Ascending order),then these two are swapped.
Step 4:Repeat the same procedure with next position in the list till the entire list is sorted.
Algorithm for Shell Sort:
Step 1:Initialize the value of h
Step 2:Divide the list into smaller sub -list of equal interval h
Step 3:Sort these sub - lists using insertion sort
Step 4:Repeat until complete list is sorted.
Algorithm for Bubble Sort:
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
Comments
Post a Comment