Skip to main content
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


Comments

Popular posts from this blog

Tree and Graph Tree and graph can be classified under the non linear data structure. Here I have shared few algorithms of tree and graph. Graph algorithms Algorithm for DFS(Depth First Search) graph traversal: Step 1:Define a Stack of size total number of vertices in the graph. Step 2:Select any vertex as starting point for traversal.Visit that vertex and push it on to the Stack. Step 3:Visit any one of the adjacent vertex of the vertex which is at top of the stack which is not visited and push it on to the stack. Step 5:When there is no new vertex to be visit then use back tracking and pop one vertex from the stack. Step 6:Repeat steps 3, 4 and 5 until stack becomes Empty. Step 7:When stack becomes Empty, then produce final spanning tree by removing unused edges from the graph. Algorithm for BFS(Breadth First Search) graph traversal: Step 1:Define a Queue of size total number of vertices in the graph. Step 2:Select any vertex as starting point for traversal.Visit ...
Linear Data Structure Algorithm: Part II Linked List is another variation of Linear Data Structure. Though it is categorized in Linear Data Structure, it is not contiguous in nature. Rather it stores nodes in different locations at memory and links the nodes to each other forming a list, Therefore derives the name Linked List. Single Linked List Algorithm to create a node in Single Linked List: Allocate memory for ptr (pointer to new node) If ptr = NULL     print: Overflow: Memory not allocated End If     Read Data to be inserted in node Set nptr->info = data Set nptr->next = NULL     return nptr End Algorithm to Count Node in Linked List: Set count to 0 Set temp to First While temp != NULL     Set count = count + 1     Set temp = temp->next End While return count End Algorithm to Reverse a given Linked List: reverse(First)    Set temp = First  ...
Linear Data Structure Algorithm - Part I Hello all you young hearts! "You are never given a wish without also being given a power to make it come true. You may have to work for it however." -Richard David Bach Here is my blog that gives you information about various algorithm of Data Structure. In this blog I have shared information about Linear Data Structure. Linear Data Structure includes Stack, Queue and Linked List. This blog basically focuses on Stack and Queue. Stack Algorithms Algorithm for Push operation: If(top>size-1)     Print Stack is full Else     Top=top+1     a[top]=item     Print a[top] is added End If  Algorithm for Pop operation: If(top is less than 0)     Print Stack is empty Else     a[top]=item     Top=top-1     Print a[top] is deleted End If Algorithm for Peek operation If(top is less than 0)  ...