Wednesday, 22 April 2015

Quick sort algorithm

Quick Sort:-

Steps for doing quick sort -
  1. Select an element as pivot from the list.
  2. Divide the list into two parts such that all the elements on the left side of the pivot are smaller than pivot and the elements on the right side are greater than pivot.
  3. Store the pivot at its correct position in between the two lists.
  4. One element has reached at its correct position.
  5. Repeat steps 1-3 for each of the sub-lists created.
  6. Stop the process untill one element is left in each of the sub-list.
Algorithm :-

Quick_Sort(low, high)
1. If(low > high)
          Return
2. Set pivot = arr[low]
3. Set i = low +1
4. Set j = high
5. Repeat step 6 until i > high or arr[i] >pivot than
6. Increment i by 1
7. Repeat step 8 until j < low or arr[j] < pivot than
8. Decrement j by 1
9. If i < j
          swap arr[i] with arr[j]
10. If i <= j
          go to step 5.
11. If low < j
          swap arr[low] with arr[j]
12. Quick_Sort (low, j-1)
13. Quick_Sort (j+1, high)

Example:-

1.         28  55  46  38  16  89  83  30
             |
         pivot   
               -------------------------> Greater value     
2.         28  55  46  38  16  89  83  30
                         <------------------- Smaller value

3.         28  55  46  38  16  89  83  30
                   ||                ||
                        SWAP

4.         28  16  46  38  55  89  83  30

       smaller <-------
                               |                         
5.         28  16  46  38  55  89  83  30 
                          |
                          ----------> greater value

6.        28  16  46  38  55  89  83  30
                   |     |
           No change as smaller value is already on the left

7. Divide the list. 
           28  16               46  38  55  89  83  30 
           List A                         List B

8. Swap 16 with 28 in A
          16   28        // truncate pivot from A
           16              // only one element left no sorting req.

9. Similarly, repeat the process for list B.

10.     16                  46  38  55  89  83  30

11.     16  28            46  38 30            89  83  55    
                                  |          |                List C
                                   SWAP

12.     16  28           30  38  46            89  83  55

13. Repeat the process until all the lists don't get sorted.
14. After that merge them. Sorted list will be -

                   16  28  30  38  46  55  83  89  





Sunday, 19 April 2015

Shell sort algorithm

Shell Sort Algorithm:-

Shell sort algorithm is an improved version of Insertion sort algorithm. In an average case when list is already partially sorted insertion sort is not an efficient way to solve the problem. Thus, we use shell sort algorithm.
        In shell sort algorithm, we divide the list in no. of lists according to the length of the list and then apply insertion sort on each sub-list. And then merge all the sub-lists to form one.

Example:-
       0   1   2   3  4   5   6    7     8   9  10
     70 30 40 10 80 20 90 110 75 60 45
We can divide it into 3 parts with 2 having 4 elements and one having 3 elements.
       0       3      6     9                 1       4     7        10                 2     5     8
    70 10 90 60         30 80 110 45          40 20 75

Apply insertion sort on each sub-list.
     0       3      6      9                1     4        7     10                  2      5     8
    10 60 70 90         30 45 80 110          20 40 75 
Merge them.
     0   1   2   3  4   5   6    7     8   9  10
    10 30 20 60 45 40 70 80 75 90 110

Divide it into two parts.
     0   2   4    6   8   10                1  3   5   7   9  
    10 20 45 70 75 110              30 60 40 80 90
Sort them using insertion sort.
      0   2    4   6   8   10                 1   3  5    7   9
     10 20 45 70 75 110               30 40 60 80 90
Merge them.
      0    1   2    3  4  5   6   7   8  9     10
     10 30 20 40 45 60 70 80 75 90 110 
Apply insertion sort to the list.
        10 20 30 40 45 60 70 75 80 90 110                

The list is sorted now.