Thursday 28 January 2016

DATA STRUCTURE Sorting Notes

Introduction to Sorting

Sorting is nothing but storage of data in sorted order, it can be in ascending or descending order. The term Sorting comes into picture with the term Searching. There are so many things in our real life that we need to search, like a particular record in database, roll numbers in merit list, a particular telephone number, any particular page in a book etc.
Sorting arranges data in a sequence which makes searching easier. Every record which is going to be sorted will contain one key. Based on the key the record will be sorted. For example, suppose we have a record of students, every such record will have the following data:
  • Roll No.
  • Name
  • Age
  • Class
Here Student roll no. can be taken as key for sorting the records in ascending or descending order. Now suppose we have to search a Student with roll no. 15, we don't need to search the complete record we will simply search between the Students with roll no. 10 to 20.

Sorting Efficiency

There are many techniques for sorting. Implementation of particular sorting technique depends upon situation. Sorting techniques mainly depends on two parameters. First parameter is the execution time of program, which means time taken for execution of program. Second is the space, which means space taken by the program.

Types of Sorting Techniques

There are many types of Sorting techniques, differentiated by their efficiency and space requirements. Following are some sorting techniques which we will be covering in next sections.
  1. Bubble Sort
  2. Insertion Sort
  3. Selection Sort
  4. Quick Sort
  5. Merge Sort
  6. Heap Sort

    Bubble Sorting

    Bubble Sort is an algorithm which is used to sort N elements that are given in a memory for eg: an Array with N number of elements. Bubble Sort compares all the element one by one and sort them based on their values.
    It is called Bubble sort, because with each iteration the smaller element in the list bubbles up towards the first place, just like a water bubble rises up to the water surface.
    Sorting takes place by stepping through all the data items one-by-one in pairs and comparing adjacent data items and swapping each pair that is out of order.
    Bubble Sort for Data Structures

    Complexity Analysis of Bubble Sorting

    In Bubble Sort, n-1 comparisons will be done in 1st pass, n-2 in 2nd pass, n-3 in 3rd pass and so on. So the total number of comparisons will be
    (n-1)+(n-2)+(n-3)+.....+3+2+1
    Sum = n(n-1)/2
    i.e O(n2)
    
    Hence the complexity of Bubble Sort is O(n2).
    The main advantage of Bubble Sort is the simplicity of the algorithm.Space complexity for Bubble Sort is O(1), because only single additional memory space is required for temp variable
    Best-case Time Complexity will be O(n), it is when the list is already sorted.

    Insertion Sorting

    It is a simple Sorting algorithm which sorts the array by shifting elements one by one. Following are some of the important characteristics of Insertion Sort.
    1. It has one of the simplest implementation
    2. It is efficient for smaller data sets, but very inefficient for larger lists.
    3. Insertion Sort is adaptive, that means it reduces its total number of steps if given a partially sorted list, hence it increases its efficiency.
    4. It is better than Selection Sort and Bubble Sort algorithms.
    5. Its space complexity is less, like Bubble Sorting, inerstion sort also requires a single additional memory space.
    6. It is Stable, as it does not change the relative order of elements with equal keys
    7. Example for Stable sort

    How Insertion Sorting Works

    Insertion SOrting in Data Structures.

    Complexity Analysis of Insertion Sorting

    Worst Case Time Complexity : O(n2)
    Best Case Time Complexity : O(n)
    Average Time Complexity : O(n2)
    Space Complexity : O(1)

    Selection Sorting

    Selection sorting is conceptually the most simplest sorting algorithm. This algorithm first finds the smallest element in the array and exchanges it with the element in the first position, then find the second smallest element and exchange it with the element in the second position, and continues in this way until the entire array is sorted.

    How Selection Sorting Works

    Selection Sorting in Data Structures
    In the first pass, the smallest element found is 1, so it is placed at the first position, then leaving first element, smallest element is searched from the rest of the elements, 3 is the smallest, so it is then placed at the second position. Then we leave 1 nad 3, from the rest of the elements, we search for the smallest and put it at third position and keep doing this, until array is sorted.

    Complexity Analysis of Selection Sorting

    Worst Case Time Complexity : O(n2)
    Best Case Time Complexity : O(n2)
    Average Time Complexity : O(n2)
    Space Complexity : O(1)

    Quick Sort Algorithm

    Quick Sort, as the name suggests, sorts any list very quickly. Quick sort is not stable search, but it is very fast and requires very less aditional space. It is based on the rule of Divide and Conquer(also called partition-exchange sort). This algorithm divides the list into three main parts :
    1. Elements less than the Pivot element
    2. Pivot element
    3. Elements greater than the pivot element
    In the list of elements, mentioned in below example, we have taken 25 as pivot. So after the first pass, the list will be changed like this.
    6 8 17 14 25 63 37 52
    Hnece after the first pass, pivot will be set at its position, with all the elements smaller to it on its left and all the elements larger than it on the right. Now 6 8 17 14 and 63 37 52 are considered as two separate lists, and same logic is applied on them, and we keep doing this until the complete list is sorted.

    How Quick Sorting Works

    Quick Sorting in Data Structures



    Complexity Analysis of Quick Sort

    Worst Case Time Complexity : O(n2)
    Best Case Time Complexity : O(n log n)
    Average Time Complexity : O(n log n)
    Space Complexity : O(n log n)
    • Space required by quick sort is very less, only O(n log n) additional space is required.
    • Quick sort is not a stable sorting technique, so it might change the occurence of two similar elements in the list while sorting.

      Merge Sort Algorithm

      Merge Sort follows the rule of Divide and Conquer. But it doesn't divides the list into two halves. In merge sort the unsorted list is divided into N sublists, each having one element, because a list of one element is considered sorted. Then, it repeatedly merge these sublists, to produce new sorted sublists, and at lasts one sorted list is produced.
      Merge Sort is quite fast, and has a time complexity of O(n log n). It is also a stable sort, which means the "equal" elements are ordered in the same order in the sorted list.

      How Merge Sort Works

      Merge Sorting in Data Structures

      Like we can see in the above example, merge sort first breaks the unsorted list into sorted sublists, and then keep merging these sublists, to finlly get the complete sorted list.

      Complexity Analysis of Merge Sort

      Worst Case Time Complexity : O(n log n)
      Best Case Time Complexity : O(n log n)
      Average Time Complexity : O(n log n)
      Space Complexity : O(n)
    • Time complexity of Merge Sort is O(n Log n) in all 3 cases (worst, average and best) as merge sort always divides the array in two halves and take linear time to merge two halves.
    • It requires equal amount of additional space as the unsorted list. Hence its not at all recommended for searching large unsorted lists.
    • It is the best Sorting technique for sorting Linked Lists.

      Heap Sort Algorithm

      Heap Sort is one of the best sorting methods being in-place and with no quadratic worst-case scenarios. Heap sort algorithm is divided into two basic parts :
    • Creating a Heap of the unsorted list.
    • Then a sorted array is created by repeatedly removing the largest/smallest element from the heap, and inserting it into the array. The heap is reconstructed after each removal.

    What is a Heap ?

    Heap is a special tree-based data structure, that satisfies the following special heap properties :
    1. Shape Property : Heap data structure is always a Complete Binary Tree, which means all levels of the tree are fully filled.
    2. difference between complete and incomplete binary tree
    3. Heap Property : All nodes are either [greater than or equal to] or [less than or equal to] each of its children. If the parent nodes are greater than their children, heap is called a Max-Heap, and if the parent nodes are smalled than their child nodes, heap is called Min-Heap.
    4. Min-Heap and Max-heap

    How Heap Sort Works

    Initially on receiving an unsorted list, the first step in heap sort is to create a Heap data structure(Max-Heap or Min-Heap). Once heap is built, the first element of the Heap is either largest or smallest(depending upon Max-Heap or Min-Heap), so we put the first element of the heap in our array. Then we again make heap using the remaining elements, to again pick the first element of the heap and put it into the array. We keep on doing the same repeatedly untill we have the complete sorted list in our array.
    In the below algorithm, initially heapsort() function is called, which calls buildheap() to build heap, which inturn uses satisfyheap() to build the heap.

    Complexity Analysis of Heap Sort

    Worst Case Time Complexity : O(n log n)
    Best Case Time Complexity : O(n log n)
    Average Time Complexity : O(n log n)
    Space Complexity : O(n)
    • Heap sort is not a Stable sort, and requires a constant space for sorting a list.
    • Heap Sort is very fast and is widely used for sorting.


0 comments:

Post a Comment