ORDER BY PART-1

ORDER BY IN SQL
The order by statement in SQL is used to sort the stored data in either ascending or descending order.
§       We have used sorting for the books on the parameters as no. of pages and no. of copies available of the book. For sorting Quick sort is used.
§       Quick sort picks an element as pivot and partitions the given array around the picked pivot.   






  • Partition Algorithm

There can be many ways to do partition, following pseudo code adopts the method given in CLRS book. The logic is simple, we start from the leftmost element and keep track of index of smaller (or equal to) elements as i. While traversing, if we find a smaller element, we swap current element with arr[i]. Otherwise we ignore current element.
Pseudo code for Partition
partition (arr[], low, high)
{
    // pivot (Element to be placed at right position)
    pivot = arr[high]; 

    i = (low - 1)  // Index of smaller element

    for (j = low; j <= high- 1; j++)
    {
        // If current element is smaller than the pivot
        if (arr[j] < pivot)
        {
            i++;    // increment index of smaller element
            swap arr[i] and arr[j]
        }
    }
    swap arr[i + 1] and arr[high])
    return (i + 1)
}



Comments

Post a Comment

Popular posts from this blog

SEARCHING

ORDER BY PART-2

GROUP BY