|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Sink Sort: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Sink Sort |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- Main idea in this method is to compare two adjacent elements and put them in proper order if they are not.
- Do this by scanning the element from first to last.
- After first scan, largest element is placed at last position. This is like a largest object sinking to bottom first.
- After second, scan second largest is placed at second last position.
- After n passes all elements are placed in their correct positions, hence sorted.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
An Applet Demonstration Of Sink Sort |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- The algorithmic description of the Sink sort is given below:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Algorithm Sink-Sort (a[n]) |
|
|
|
|
|
|
|
|
Step 1 : |
|
for i = 0 to n-2 do |
|
|
|
|
|
|
|
for j = 0 to n-i do |
|
|
|
|
|
Step 2 : |
|
|
|
|
|
|
|
Step 3 : |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (a[j] > a[j+1]) then |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Step 4 : |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
swap(a[j],a[j+1]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- The algorithm can be modified, such that, after each pass next smallest element reach to the top position.This is like a bubble coming to top. Hence called Bubble-Sort.
|
|
|
Prev |
|
|