Skip to main content

Posts

Showing posts with the label data structure.

Heap Sort In Data Structure

    ⇰  HEAP SORT :- Heap sort is also a type of sorting technique, which can apply in binary tree sorting. Heap sort technique is comparison based sorting technique based on Heap data structure. This technique is similar to the selection sort . But first of all we need to know about Heap data structure. A Binary Heap is a Complete Binary Tree where items are stored in a special order such that  root node is compared with its children and arranged  accordingly. The heap is of two types:- Min heap ( value of root node is less than or equal to child) , Max heap ( value of root node is greater or equal to child). The heap can be represented by binary tree or array.   ⇰  ALGORITHM HEAP SORT :- Step1 :- Write root in the list. Step2 :- Write last element at the root. Step3 :- Heapify the tree. Step4 :- Repeat all three steps.   ⇰  IMPLEMENTATION OF HEAP SORT :- Let us take a heap:-   We take element and put into list Share, Follow

Merge Sort In data Structure

    ⇰  MERGE SORT :- Merge sort is also a type of sorting technique. It is mainly based on divide-and-conquer algorithm as quick and merge sort, which breaks down a list into several sub-lists until each sublist consists of a single element and merging those sublists in a manner that results into a sorted list.  Merge sort is a sorting technique based on divide and conquer technique. Its  worst-case time complexity is Ο(n log n).    It divides input list in two sub-lists, calls itself for the two sub-lists and then merges the two sorted sub-lists. Suppose we have two sub-lists. listA is a list with n element and listB with m element. The operator that combines the element of listA and listB into single result_list with R=n+m elements is called merging.     If the elements of listA and listB are in sorted order then we may get a result_list in sorted order. If listA and listB are not in sorted order then we have to make them in sorted order and then both the list can be me

Quick Sort In Data Structure

    ⇰   QUICK SORT :- Quick sort is one of the type of sorting technique. It  follows the di vide and conquer algorithm.  The Quick sort   treats an array as a list of elements. When sorting process begins, it selects the List's middle element as the list pivote. The algorithm then divides the list into two sub-lists, one with the elements that are less then the list pivote and other list with elements greater then or equal to list pivote.    The algorithm then recursively invokes or calls itself with both the list. Each time when the sorting algorithm is invoked, it further divides the elements into smaller sub-lists.  This algorithm is quite efficient for large-sized data.  In quick Sort generally middle element of list is considered as pivote, but we can  take pivot in different ways as follows:-     i > picking first element as pivot.  ii > picking last element as pivot iii > Pick a random element as pivot. iv > Pick middle element as pivot (We are us

Bubble Sort in Data Structure

       ⇰  BUBBLE SORT :-  The bubble sorting technique is very simple sorting technique. However this technique is not efficient in comparision to other sorting techniques but for smaller list this technique works fine.  This sorting technique is comparison-based in which each pair of adjacent elements is compared and the elements are swapped if they are not in the desired order . Its worst case complexity is of Ο(n ^2 ) where n  is the number of items in the list.     Suppose we want to sort the elements of a list in ascending order, the bubble sort loops through the element in the list and compares the adjecent element and moves the smaller element to the the top of the list. EXAMPLE :- Let us take a unsorted list :-     4 3 5 2 1  ⇰    ALGORITHM OF BUBBLE SORT :- Variable used :- list= array of element, n= number of element in the list. I, J, temp = local variable. Step1 :- Initialize               I =1 Step2 :- Repeat through step5                  whi

Tree Traversal

        ⇰   TREE TRAVERSAL :- Tree Traversal refers to the process of visiting each node in a tree data structure exactly once.  You might for instance want to add all the values in the tree or find the largest one. For all these operations, you will need to visit each node of the tree atleast and exectly once. Linear data structures like arrays, stacks, queues and linked list have only one logical way to read the data. But a hierarchical data structure like a tree can be traversed in different ways.  Following are the generally used Techniques for traversing trees :-    i>Breadth First Traverse.  ii> Depth First Traverse. i > Breadth First Traverse :- Breadth First Traverse of a tree is technique of printing all the nodes of a tree level by level. It  is also called as   Level Order Traversal .   Here if we traverse in above tree by breadth first treverse technique the traversal will be on level wise as in the order :- 1 2 3 4 5 6.   ii > Depth

Selection Sort In Data Structure

   ⇰  SELECTION SORT :- Selection sort is one of the simple type of sorting algorithm. This sorting algorithm is based on In-place comparision of element in the array. In this technique the smallest element is selected from the unsorted array and swapped with the leftmost element. This process continues moving unsorted array boundary by one element to the right. The selection starts from first element and searches the entire list or array until it finds the minimum value in the first place, again selects the second element and searches for the second samllest element. This process continues untill the complete list becomes sort form.This algorithm is not suitable for large data as insertion sort. Its  worst case complexity is Ο(n 2 ), where n is the number of items. FOR EXAMPLE :- Let us take a unsorted list :-      4 3 5 2 1   ⇰  ALGORITHM OF SELECTION SORT :- Variable used:-  array= list of element, size= number of element,       I, J , temp = local vari