ebooksgratis.com

See also ebooksgratis.com: no banners, no cookies, totally FREE.

CLASSICISTRANIERI HOME PAGE - YOUTUBE CHANNEL
Privacy Policy Cookie Policy Terms and Conditions
Strand sort - Wikipedia, the free encyclopedia

Strand sort

From Wikipedia, the free encyclopedia

Strand sort is a sorting algorithm. It works by repeatedly pulling sorted sublists out of the list to be sorted and merging them with a result array. Each iteration through the unsorted list pulls out a series of elements which were already sorted, and merges those series together.

The name of the algorithm comes from the "strands" of sorted data within the unsorted list which are removed one at a time. It is a comparison sort due to its use of comparisons when removing strands and when merging them into the sorted array.

The strand sort algorithm is O(n log n) in the average case. In the best case (a list which is already sorted) the algorithm is linear, or O(n). In the worst case (a list which is sorted in reverse order) the algorithm is O(n2).

Strand sort is most useful for data which is stored in a linked list, due to the frequent insertions and removals of data. Using another data structure, such as an array, would greatly increase the running time and complexity of the algorithm due to lengthy insertions and deletions. Strand sort is also useful for data which already has large amounts of sorted data, because such data can be removed in a single strand.

[edit] Example

Unsorted List Sublist Sorted List
3 1 5 4 2
1 4 2 3 5
1 4 2 3 5
2 1 4 3 5
2 1 3 4 5
2 1 3 4 5
1 2 3 4 5
  1. Parse the Unsorted List once, taking out any ascending (sorted) numbers.
  2. The (sorted) Sublist is, for the first iteration, pushed onto the empty sorted list.
  3. Parse the Unsorted List again, again taking out relatively sorted numbers.
  4. Since the Sorted List is now populated, merge the Sublist into the Sorted List.
  5. Repeat steps 3-4 until both the Unsorted List and Sublist are empty.

[edit] Algorithm

A simple way to express strand sort in pseudocode is as follows:

procedure strandSort( A : list of sortable items ) defined as:
  while length( A ) > 0
    clear sublist
    sublist[ 0 ] := A[ 0 ]
    remove A[ 0 ]
    for each i in 0 to length( A ) do:
      if A[ i ] > sublist[ last ] then
        append A[ i ] to sublist
        remove A[ i ]
      end if
    end for
    merge sublist into results
  end while
  return results
end procedure

Here is an example of this sort in Java 1.5:

public static <E extends Comparable<? super E>> List<E> sort(Collection<E> coll) 
        {  
                List<E> results = new ArrayList<E>();
                while (!coll.isEmpty())
                {
                        ArrayList<E> sublist = new ArrayList<E>();
                        Iterator<E> i = coll.iterator();
                        sublist.add(i.next());
                        i.remove();
                        while (i.hasNext())
                        {
                                E val = i.next();
                                if (val.compareTo(sublist.get(sublist.size()-1)) >= 0)
                                {
                                        sublist.add(val);
                                        i.remove();
                                }
                        }
                        if (!results.isEmpty())
                        {
                                ListIterator<E> li = results.listIterator();
                                E current = li.next();
                                while (!sublist.isEmpty())
                                {
                                        if (sublist.get(0).compareTo(current) < 0)
                                        {
                                                li.previous();
                                                li.add(sublist.remove(0));
                                        }
                                        else if (li.hasNext())
                                        {
                                                current = li.next();
                                        }
                                        else
                                                break;
                                }
                        }
                        else results.addAll(sublist);
                        sublist.clear();
                }
                return results;
        }

[edit] References

Languages


aa - ab - af - ak - als - am - an - ang - ar - arc - as - ast - av - ay - az - ba - bar - bat_smg - bcl - be - be_x_old - bg - bh - bi - bm - bn - bo - bpy - br - bs - bug - bxr - ca - cbk_zam - cdo - ce - ceb - ch - cho - chr - chy - co - cr - crh - cs - csb - cu - cv - cy - da - de - diq - dsb - dv - dz - ee - el - eml - en - eo - es - et - eu - ext - fa - ff - fi - fiu_vro - fj - fo - fr - frp - fur - fy - ga - gan - gd - gl - glk - gn - got - gu - gv - ha - hak - haw - he - hi - hif - ho - hr - hsb - ht - hu - hy - hz - ia - id - ie - ig - ii - ik - ilo - io - is - it - iu - ja - jbo - jv - ka - kaa - kab - kg - ki - kj - kk - kl - km - kn - ko - kr - ks - ksh - ku - kv - kw - ky - la - lad - lb - lbe - lg - li - lij - lmo - ln - lo - lt - lv - map_bms - mdf - mg - mh - mi - mk - ml - mn - mo - mr - mt - mus - my - myv - mzn - na - nah - nap - nds - nds_nl - ne - new - ng - nl - nn - no - nov - nrm - nv - ny - oc - om - or - os - pa - pag - pam - pap - pdc - pi - pih - pl - pms - ps - pt - qu - quality - rm - rmy - rn - ro - roa_rup - roa_tara - ru - rw - sa - sah - sc - scn - sco - sd - se - sg - sh - si - simple - sk - sl - sm - sn - so - sr - srn - ss - st - stq - su - sv - sw - szl - ta - te - tet - tg - th - ti - tk - tl - tlh - tn - to - tpi - tr - ts - tt - tum - tw - ty - udm - ug - uk - ur - uz - ve - vec - vi - vls - vo - wa - war - wo - wuu - xal - xh - yi - yo - za - zea - zh - zh_classical - zh_min_nan - zh_yue - zu -