.

Sunday, August 2, 2015

Quick Sort : using traditional partition concept.

Quicksort or partition-exchange sort, is a fast sorting algorithm, which is using divide and
 conquer algorithm. Quicksort first divides a large list into two smaller sub-lists:
 the low elements and the high elements. Quicksort can then recursively sort the sub-lists.

pivot -In the very early versions of quicksort, the leftmost element of the partition would often be chosen
 as the pivot element. Unfortunately, this causes worst-case behavior on already sorted arrays, which
 is a rather common use-case. The problem was easily solved by choosing either a random index for the pivot or choosing the middle index of the partition.

Average case performance‎: ‎O(n log n)
Best case performance‎      :  O(n log n)
Worst case performance‎    :  ‎O(n2)


Here i am taking middle element as pivot.

Steps to implement Quick sort:

1) Choose an element, called pivot, from the list. Generally pivot can be the middle index element.
2) Reorder the list so that all elements with values less than the pivot come before the pivot, while all elements with values   greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final   position. This is called the partition operation.
3) Recursively apply the above steps to the sub-list of elements with smaller values and separately the sub-list of elements with greater values.


__author__ = 'deepika'

__author__ = 'deepika'

def quicksort(alist ,left ,right):
    if left >= right:
        return 

    partition = partitn(alist,left,right)
    if left < partition - 1:
        quicksort(alist, left, partition - 1)

    if partition < right:
        quicksort(alist, partition, right)
def partitn(alist, left, right):
    i=left
    j=right
    mid=(left+right)/2 

    pivot=alist[mid]
    while i <= j:
        while alist[i] < pivot:
            i=i+1 

        while alist[j] > pivot:
            j=j-1 

        if i <= j:
            temp = alist[i]
            alist[i] = alist[j]
            alist[j] = temp
            i = i + 1 

            j = j - 1 

   return i
alist = [9, 4, 3, 1, 6, 8, 2, 0]
left = 0

 right = len(alist)-1 

quicksort(alist,left,right)
print("----O-u-t-p-u-t-----")
print(alist)




----O-u-t-p-u-t-----
[0, 1, 2, 3, 4, 6, 8, 9]


1) Choose an element, called pivot, from the list. Generally pivot can be the middle index element.
2) Reorder the list so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation.
3) Recursively apply the above steps to the sub-list of elements with smaller values and separately the sub-list of elements with greater values.
Quick Sort
- See more at: http://www.java2novice.com/java-sorting-algorithms/quick-sort/#sthash.F34FiGUQ.dpuf
Steps to implement Quick sort:

1) Choose an element, called pivot, from the list. Generally pivot can be the middle index element.
2) Reorder the list so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation.
3) Recursively apply the above steps to the sub-list of elements with smaller values and separately the sub-list of elements with greater values.
Quick Sort
- See more at: http://www.java2novice.com/java-sorting-algorithms/quick-sort/#sthash.F34FiGUQ.dpuf
Quicksort or partition-exchange sort, is a fast sorting algorithm, which is using divide and conquer algorithm. Quicksort first divides a large list into two smaller sub-lists: the low elements and the high elements. Quicksort can then recursively sort the sub-lists. - See more at: http://www.java2novice.com/java-sorting-algorithms/quick-sort/#sthash.F34FiGUQ.dpuf
Quicksort or partition-exchange sort, is a fast sorting algorithm, which is using divide and conquer algorithm. Quicksort first divides a large list into two smaller sub-lists: the low elements and the high elements. Quicksort can then recursively sort the sub-lists. - See more at: http://www.java2novice.com/java-sorting-algorithms/quick-sort/#sthash.F34FiGUQ.dpuf
Quicksort or partition-exchange sort, is a fast sorting algorithm, which is using divide and conquer algorithm. Quicksort first divides a large list into two smaller sub-lists: the low elements and the high elements. Quicksort can then recursively sort the sub-lists. - See more at: http://www.java2novice.com/java-sorting-algorithms/quick-sort/#sthash.F34FiGUQ.dpuf
Quicksort or partition-exchange sort, is a fast sorting algorithm, which is using divide and conquer algorithm. Quicksort first divides a large list into two smaller sub-lists: the low elements and the high elements. Quicksort can then recursively sort the sub-lists. - See more at: http://www.java2novice.com/java-sorting-algorithms/quick-sort/#sthash.F34FiGUQ.dpuf
Quicksort or partition-exchange sort, is a fast sorting algorithm, which is using divide and conquer algorithm. Quicksort first divides a large list into two smaller sub-lists: the low elements and the high elements. Quicksort can then recursively sort the sub-lists. - See more at: http://www.java2novice.com/java-sorting-algorithms/quick-sort/#sthash.F34FiGUQ.dpuf

5 comments :