Tuesday 6 October 2015

Quick S



Sorting Algorithms - QUICKSORT

Example

if( left + 10 <= right)
    {
 int i = left, j = right - 1;
 for ( ; ; )
   {
     while (a[++i] < pivot  ) {}   // move the left finger
     while (pivot  < a[--j] ) {}   // move the right finger

     if (i < j) swap (a[i],a[j]);  // swap 
            else  break;    // break if fingers have crossed
          }
       swap (a[i], a[right-1]);    // restore the pivot
       quicksort ( a, left, i-1);   // call quicksort for the left part
       quicksort (a, i+1, right);   // call quicksort for the left part
     }
else  insertionsort (a, left, right);
Let's see how this code will work on an example, disregarding the first if statement - the check for the number of elements.
Source array to be sorted: 10, 3, 25, 6, 8, 17, 1, 2, 18, 5 I. Compute the first partitioning:
Source array: 10, 3, 25, 6, 8, 17, 1, 2, 18, 5
  1. Choosing the pivot:
  2. The median-of-three: [10, 8, 5] : the median is 8
  3. Order the three elements and hide the pivot :
  4. 5, 3, 25, 6, 8, 17, 1, 2, 18, 10
    5, 3, 25, 6, 18, 17, 1, 2, 8, 10
  5. Start rearranging:
  6. Set two indices: i and j
    i = left (i.e. i = 0)
    j = right -1 (i.e. j = 8)
    
    
    5, 3, 25, 6, 18, 17, 1, 2, 8, 10
    ^                          ^
    i                          j
    
    At each run of the for loop we swap two elements
    3.1. First run: while (a[++i] < pivot ){}
    ++i means: first increment i, then perform the comparison.
    Move i to the right skipping elements that are less than the pivot
    We know that the very first element is less than the pivot, so we do not compare it
    
    
    5, 3, 25, 6, 18, 17, 1, 2, 8, 10
          ^                    ^
          i                    j
    
    i stops at 25, as 25 is greater than the pivot ( i = 2)
    while (pivot < a[--j] ) {}
    Initially j is 'right'-1 = 8. This is the pivot.
    We do not process the pivot, that is why we write --j - first decrement, and then compare.
    
    5, 3, 25, 6, 18, 17, 1, 2, 8, 10
          ^                 ^
          i                 j
    
    j stops at 2 , the first element from right to left that is smaller than 8. (j = 7)
    if (i < j) swap (a[i], a[j]);
    else break;
    i = 2 is less than j=7 and we swap 25 and 2:
    
    5, 3, 2, 6, 18, 17, 1, 25, 8, 10
          ^                 ^
          i                 j
    
    3.2. Second run of the for loop:
    
    5, 3, 2, 6, 18, 17, 1, 25, 8, 10
          ^                 ^
          i                 j
    
    We start with i = 2 , pointing to 2, and j = 7, pointing to 25
    while (a[++i] < pivot) {}
    6 is less then the pivot (8), we skip it, and i stops at 18 ( i = 4)
    
    5, 3, 2, 6, 18, 17, 1, 25, 8, 10
                 ^         ^
                 i         j
    
    while (pivot < a[--j]) {}
    1 is less than the pivot, j stops there. (j = 6)
    
    5, 3, 2, 6, 18, 17, 1, 25, 8, 10
                 ^      ^
                 i      j
    
    if (i < j) swap (a[i], a[j]);
    else break;
    i = 4 is less than j = 6 and we swap 18 and 1:
    
    5, 3, 2, 6, 1, 17, 18, 25, 8, 10
                ^       ^
                i       j
    
    3.3. Third run of the for loop:
    We start with i = 4 , pointing to 1, and j = 6, pointing to 18.
    
    5, 3, 2, 6, 1, 17, 18, 25, 8, 10
                ^       ^
                i       j
    
    while (a[++i] < pivot {}
    17 is greater than the pivot and i stops there (i = 5)
    while (pivot < a[--j] < {}
    1 is less then the pivot and j stops there ( j = 4)
    
    5, 3, 2, 6, 1, 17, 18, 25, 8, 10
                ^   ^
                j   i
    
    if (i < j) swap (a[i], a[j]);
    else break;
    Now we break, because i and j have crossed
  7. Bring back the pivot:
  8. The pivot is swapped with a[i]
    5, 3, 2, 6, 1, 8, 18, 25, 17, 10
    This is the first partitioning:
    [ 5, 3, 2, 6, 1 ] [8] [18, 25, 17, 10]
II. Recursively we process the left part of the array: 5, 3, 2, 6, 1
  1. Choosing the pivot: the median of three: 5, 2, 1:
  2. The median is 2
  3. Order the three elements and hide the pivot:
  4. 1, 3, 6, 2, 5
  5. Start the for loop.
    1. i will stop at 3 ( i = 1) - the first element greater than the pivot:
    2. j will stop at 1 (j = 0)- the first element from right to left smaller than the pivot.
    3. No swap is done, as i and j have crossed:
  6. Bring back the pivot: swap the pivot with a[1]
  7. 1, 2, 6, 3, 5
    The partitioning now is:
    [ 1 ] [ 2 ] [ 6, 3, 5 ]
III. Do the same with the left array and the right array.
The left array consists only of one element, so we stop.
In practice, we stop partitioning when the elements are small in number, e.g. n < 10,
and we sort with insertion sort. Here we stop at 1, for the sake of the example.
Next comes the right array: [ 6, 3, 5]
It is only of three elements, and the median of three choice will put them in order:
3, 5, 6
Now we have a sorted part: 1, 2, 3, 5, 6
IV. In a similar way we process the right part in the first partitioning: 18, 25, 17, 10.
After it has been sorted, we will obtain:
[1, 2, 3, 5, 6] [8] [10, 17, 18, 25] and this is the whole array sorted.

Quick SOrt

Quick Sort: Partition in place
Most implementations of quick sort make use of the fact that you can partition in place by keeping two pointers: one moving in from the left and a second moving in from the right. They are moved towards the centre until the left pointer finds an element greater than the pivot and the right one finds an element less than the pivot. These two elements are then swapped. The pointers are then moved inward again until they "cross over". The pivot is then swapped into the slot to which the right pointer points and the partition is complete.

int partition( void *a, int low, int high )
  {
  int left, right;
  void *pivot_item;
  pivot_item = a[low];
  pivot = left = low;
  right = high;
  while ( left < right ) {
    /* Move left while item < pivot */
    while( a[left] <= pivot_item ) left++;
    /* Move right while item > pivot */
    while( a[right] > pivot_item ) right--;
    if ( left < right ) SWAP(a,left,right);
    }
  /* right is final position for the pivot */
  a[low] = a[right];
  a[right] = pivot_item;
  return right;
  }
Note that this above code does not check that left does not exceed the array bound. You need to add this check, before performing the swaps - both the one in the loop and the final one outside the loop. partition ensures that all items less than the pivot precede it and returns the position of the pivot. This meets our condition for dividing the problem: all the items in the lower half are known to be less than the pivot and all items in the upper half are known to be greater than it.
Note that we have used our ItemCmp function in the partition function. This assumes that there is an external declaration for ItemCmp and that in any one program, we only want to sort one type of object. Generally this will not be acceptable, so the formal specification for quicksort in the Unix and ANSI C libraries includes a function compar which is supplied to qsort when it is called. Passing the function, compar, which defines the ordering of objects when qsort is called avoids this problem in the same way that we passed an ItemCmp function to ConsCollection

Analysis

The partition routine examines every item in the array at most once, so it is clearly O(n). Usually, the partition routine will divide the problem into two roughly equal sized partitions. We know that we can divide n items in half log2n times. This makes quicksort a O(nlogn) algorithm - equivalent to heapsort.
However, we have made an unjustified assumption - see if you can identify it before you continue.http://imraan-prrec.blogspot.in/2015/10/quick.html

Quick

7.3 Quick Sort (cont)

Quick Sort - The Facts!

Quick Sort is generally the best known sorting algorithm, but it has a serious limitation, which must be understood before using it in certain applications. What happens if we apply the qsort routine on the previous page to an already sorted array? This is certainly a case where we'd expect the performance to be quite good!
However, the first attempt to partition the problem into two problems will return an empty lower partition - the first element is the smallest. Thus the first partition call simply chops off one element and calls qsort for a partition with n-1 items! This happens a further n-2 times! Each partition call still requires O(n) operations - and we have generated O(n) such calls.
In the worst case, quicksort is an O(n2) algorithm!

Can we do anything about this?

A number of variations to the simple quicksort will generally produce better results: rather than choose the first item as the pivot, some other strategies work better.

Median-of-3 Pivot

For example, the median-of-3 pivot approach selects three candidate pivots and uses the median one. If the three pivots are chosen from the first, middle and last positions, then it is easy to see that for the already sorted array, this will produce an optimum result: each partition will be exactly half (±one element) of the problem and we will need exactly ceiling(logn) recursive calls.

Random pivot

Some qsort's will simply use a randomly chosen pivot. This also works fine for sorted arrays - on average the pivot will produce two equal sized partitions and there will be O(logn) of them.
However, whatever strategy we use for choosing the pivot,
it is possible to propose a pathological case
in which the problem is not divided equally at any partition stage. Thus quicksort must always be treated as potentially O(n2).

Why bother with quicksort then?

Heap sort is always O(nlog n): why not just use it?

Heap

Algorithms for Sorting - Heapsort


  1. Heapsort
  2. Based on Priority Queues
    Sorts in O(NlogN) time by performing N times deleteMin operations.
    Why O(NlogN)?
      • Each deleteMin operation takes logN running time.
      • N times performing deleteMin → NlogN running time
    Basic algorithm (not very efficient - extra memory required):
    1. Store N elements in a binary heap tree.
    2. Perform deleteMin operation N times, storing each element deleted from
      the heap into another array.
    3. Copy back the array.
    Improvements: Use the same array to store the deleted elements
    instead of using another array
    How can we do this?
    After each deletion we percolate down the hole that is formed after the deletion,
    and as a result we get a vacant position in the array - the last cell.
    There we store the deleted element.
    When all the elements are deleted and stored in the same array following the above method,
    the elements will be there in reversed order.
    What is the remedy for this?

    Store the elements in the binary heap tree in reverse order of priority -
    then at the end the elements in the array will be in correct order.
    Complexity of heap sort: O(NlogN) is the rough estimate.
    Here is an example of heapsort.




Learning Goals
  • Be able to explain how heapsort works and its complexity.

Exam-like questions
  1. Explain the complexity of heapsort
  2. Given an array e.g. 17, 23, 10, 1, 7, 16, 9, 20, sort it on paper using heapsort
    Write down explicitly each step.

Heap

An Example of Heapsort:
Given an array of 6 elements: 15, 19, 10, 7, 17, 16, sort it in ascending order using heap sort.
Steps:

  1. Consider the values of the elements as priorities and build the heap tree.
  2. Start deleteMin operations, storing each deleted element at the end of the heap array.
After performing step 2, the order of the elements will be opposite to the order in the heap tree.
Hence, if we want the elements to be sorted in ascending order, we need to build the heap tree
in descending order - the greatest element will have the highest priority.
Note that we use only one array , treating its parts differently:

  1. when building the heap tree, part of the array will be considered as the heap,
    and the rest part - the original array.
  2. when sorting, part of the array will be the heap, and the rest part - the sorted array.
This will be indicated by colors: white for the original array, blue for the heap and red for the sorted array
Here is the array: 15, 19, 10, 7, 17, 6
  1. Building the heap tree
  2. The array represented as a tree, complete but not ordered: 
    Start with the rightmost node at height 1 - the node at position 3 = Size/2.
    It has one greater child and has to be percolated down:
     
    After processing array[3] the situation is:
     
    Next comes array[2]. Its children are smaller, so no percolation is needed. 
    The last node to be processed is array[1]. Its left child is the greater of the children.
    The item at array[1] has to be percolated down to the left, swapped with array[2].
     
    As a result the situation is:
     
    The children of array[2] are greater, and item 15 has to be moved down further, swapped with array[5].
     
    Now the tree is ordered, and the binary heap is built.
  1. Sorting - performing deleteMax operations:
  2. 1. Delete the top element 19. 1.1. Store 19 in a temporary place. A hole is created at the top
     
    1.2. Swap 19 with the last element of the heap.

    As 10 will be adjusted in the heap, its cell will no longer be a part of the heap.
    Instead it becomes a cell from the sorted array
     
    1.3. Percolate down the hole
     
    1.4. Percolate once more (10 is less that 15, so it cannot be inserted in the previous hole)
     
    Now 10 can be inserted in the hole
     
    2. DeleteMax the top element 172.1. Store 17 in a temporary place. A hole is created at the top
    2.2. Swap 17 with the last element of the heap.
    As 10 will be adjusted in the heap, its cell will no longer be a part of the heap.
    Instead it becomes a cell from the sorted array
     
    2.3. The element 10 is less than the children of the hole, and we percolate the hole down:
     
    2.4. Insert 10 in the hole
     
    3. DeleteMax 163.1. Store 16 in a temporary place. A hole is created at the top
     
    3.2. Swap 16 with the last element of the heap.

    As 7 will be adjusted in the heap, its cell will no longer be a part of the heap.
    Instead it becomes a cell from the sorted array
     
    3.3. Percolate the hole down (7 cannot be inserted there - it is less than the children of the hole)
     
    3.4. Insert 7 in the hole
     
    4. DeleteMax the top element 154.1. Store 15 in a temporary location. A hole is created.
     
    4.2. Swap 15 with the last element of the heap.
    As 10 will be adjusted in the heap, its cell will no longer be a part of the heap.
    Instead it becomes a position from the sorted array
     
    4.3. Store 10 in the hole (10 is greater than the children of the hole)
     
    5. DeleteMax the top element 10.5.1. Remove 10 from the heap and store it into a temporary location.
     
    5.2. Swap 10 with the last element of the heap.
    As 7 will be adjusted in the heap, its cell will no longer be a part of the heap. Instead it becomes a cell from the sorted array  
    5.3. Store 7 in the hole (as the only remaining element in the heap
     
    7 is the last element from the heap, so now the array is sorted

Monday 21 September 2015

My message for students community:

Obstacles, hurdle & hindrances,
are just for testing thy endurance.
Life's challenges are sure to be dreary
But never be filled with dismay.

It's not wise ,to show cowardice
keep your clam till they vanish
Bare all the gloom and disappointments
And fortitude face other torment.

Be intrepid and invisible
and have forbearance
Thou will very soon subdue
All these hurdle coming thy avenue.

My emphasis is towards:
  • Adoption of latest techniques, methodologies and systems for effective orientation of the students not only to the present but also to future needs.
  • Practical applications.
  • Promote Entrepreneurial, Self supporting and Man-making Education.

Monday 7 September 2015


How TCP-IP Works Video Really nice video

 Download

MC

Mobile Computing Notes for Units V

See the assignment questions and write any 2

https://www.mediafire.com/?vec7mcqbpe26sakDownload


PPT Unit V by Mohammed Imran

MC

Mobile Computing JOCHEN SCHILLER

https://www.mediafire.com/?b9itoik6m07sizvClick to Download                        
  Directory: SCM
  Dev tools: Adobe PDF
  File size: 18.9 MB
  Update: 2015-09-07
  Downloads: 0