Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!news.glorb.com!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Wed, 15 Feb 2012 00:41:12 -0600 Message-ID: <4F3B5380.F0B@mindspring.com> Date: Wed, 15 Feb 2012 01:41:04 -0500 From: pete Reply-To: pfiland@mindspring.com Organization: PF X-Mailer: Mozilla 3.04Gold (WinNT; I) MIME-Version: 1.0 Newsgroups: comp.programming Subject: Re: What algorithm is this? (Variant of Selection sort?) References: <1bbf76cc-241c-4936-bf52-3d48e1180c41@kn4g2000pbc.googlegroups.com> <4F3A7F10.6F65@mindspring.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 229 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.154.216.252 X-Trace: sv3-MahbaTCXe4kn9cdxprZA/lMFyGeC8xbLAH7L/JTFZOXy4jj/hSyY7KB+ZFE4vMv2fe42vYcjWX0LmwK!aveGLJuYh3T30zZC8iDKV96a+aGcejlaCP0Xp5tOhS8GpS3gTIJhiHwcYonIHHiGpVt4UjmrZvKI!RsyzpgPLcQ== X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 6933 Xref: x330-a1.tempe.blueboxinc.net comp.programming:1321 R. Rajesh Jeba Anbiah wrote: > > On Feb 14, 8:34 pm, pete wrote: > > R. Rajesh Jeba Anbiah wrote: > > > On Feb 13, 12:47 pm, Zeljko Vrba wrote: > > > > On 2012-02-13, R. Rajesh Jeba Anbiah wrote: > > > > > > > > > $arr = array(10,9,8,7,6,5,4,3,2,1); > > > > > for($i=0, $len=count($arr); $i<$len-1; ++$i){ > > > > > for($j=$i+1, $len=count($arr); $j<$len; ++$j){ > > > > > if ($arr[$i] > $arr[$j]) { > > > > > $temp = $arr[$i]; > > > > > $arr[$i] = $arr[$j]; > > > > > $arr[$j] = $temp; > > > > > } > > > > > } > > > > > } > > > > > ?> > > > > > > > As I understand, selection sort is > > > > > about finding index first and > > > > > doing the swap. What about the above? Is it a variant or sort with > > > > > some name? TIA > > > > > > Looks like textbook implementation of Bubble sort. > > > > > Yes, found in textbook only. But, it's not a Bubble sort. Right? > > >http://en.wikipedia.org/wiki/Bubble_sort > > > > It looks a lot like bubble sort, > > but it is not bubble sort. > > > > Bubble sort works by swapping adjacent elements. > > The algorithm which you have shown, > > swaps elements which are not adjacent. > > > > I would just call your algorithm "simple selection sort", > > while realizing that there are variations > > on the way that it can be implemented. > > > > Selection sorts, sort from one end to the other. > > > > Knuth classifies heapsort > > as belonging to the selection sort family of algorithms. > > As heapsort is typically implementd for sorting arrays: > > the last element is sorted first, > > and then the next to last, > > and so on. > > Thanks for your valuable feedback. On a personal note I'm thinking > why it's not officially documented and named--even though it's > available in some textbooks as "bubble sort". That is definitely not bubble sort. This is bubble sort: $arr = array(10,9,8,7,6,5,4,3,2,1); for($i=0, $len=count($arr); $i<$len-1; ++$i){ for($j=$len-1, $len=count($arr); $j>$i; --$j){ if ($arr[$j-1] > $arr[$j]) { $temp = $arr[$j-1]; $arr[$j-1] = $arr[$j]; $arr[$j] = $temp; } } } Bubble sort is a stable sort, simple selection sort is not. A stable sort is one in which the original order of equal keys is preserved. For sorting integers, it makes no difference whether or not a sort is stable. But if say for example, you were sorting pointers to text strings by string length, then there would be a difference. If "one","two","three","four","five", "six","seven","eight","nine","ten" were to be sorted by string length according to your posted algorithm, then the output would be: one two six ten four nine five eight three seven Note that nine and five are out of their original order relative to each other, even though they are the same length. Also note that eight three and seven are out of their original order relative to each other, even though they are the same length. A stable sort by string length of the original order would yield instead: one two six ten four five nine three seven eight Note that same length words retain their original order relative to each other. /* BEGIN ss.c */ #include #include #define NMEMB(A) (sizeof (A) / sizeof *(A)) #define GT(A, B) (comp((A), (B)) > 0) int comp(const void *a, const void *b); int main(void) { char *arr[] = { "one","two","three","four","five", "six","seven","eight","nine","ten" }; unsigned i; unsigned j; unsigned len; char *temp; len = NMEMB(arr); for (i = 0; i < len -1; ++i) { for (j = i + 1; j < len; ++j) { if (GT(arr + i, arr + j)) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } puts("BEGIN ss.c output\n"); for (i = 0; i != len; ++i) { puts(arr[i]); } puts("\nEND ss.c output"); return 0; } int comp(const void *a, const void *b) { const size_t a_len = strlen(*(const char *const *)a); const size_t b_len = strlen(*(const char *const *)b); return b_len > a_len ? -1 : b_len != a_len; } /* END ss.c */ /* BEGIN bs.c */ #include #include #define NMEMB(A) (sizeof (A) / sizeof *(A)) #define GT(A, B) (comp((A), (B)) > 0) int comp(const void *a, const void *b); int main(void) { char *arr[] = { "one","two","three","four","five", "six","seven","eight","nine","ten" }; unsigned i; unsigned j; unsigned len; char *temp; len = NMEMB(arr); for (i = 0; i < len - 1; ++i) { for (j = len - 1; j > i; --j) { if (GT(arr + j - 1, arr + j)) { temp = arr[j - 1]; arr[j - 1] = arr[j]; arr[j] = temp; } } } puts("BEGIN bs.c output\n"); for (i = 0; i != len; ++i) { puts(arr[i]); } puts("\nEND bs.c output"); return 0; } int comp(const void *a, const void *b) { const size_t a_len = strlen(*(const char *const *)a); const size_t b_len = strlen(*(const char *const *)b); return b_len > a_len ? -1 : b_len != a_len; } /* END bs.c */ -- pete