Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!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: Tue, 16 Aug 2011 18:22:18 -0500 Message-ID: <4E4AFBAA.6AD2@mindspring.com> Date: Tue, 16 Aug 2011 19:22:18 -0400 From: pete Reply-To: pfiland@mindspring.com Organization: PF X-Mailer: Mozilla 3.04Gold (WinNT; I) MIME-Version: 1.0 Newsgroups: comp.lang.c Subject: Re: selection-sort in C References: <4e4a4fc9$0$313$14726298@news.sunsite.dk> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 57 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.154.216.115 X-Trace: sv3-ZmtFFoXhPJB/yzgeByLyELXCu3LIwYVSqKVrIjlNzlJJv0OA8A26n+Rg/JUn45JxHdWlU+Fpxm7z5eb!mbNh1vEDaZVotZtyrRil5HV9PvlY+SHNjXvYq95VO7TXNx44F8sGctA0VvmZsz4zw9+yluju+j1y!OqFtWvw1s9E= 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: 2102 Xref: x330-a1.tempe.blueboxinc.net comp.lang.c:9495 arnuld wrote: > void selectionSort(char s[], int len) > { > int i, idx; > char tmp; > for(i = 0; i != len; ++i) That loop is too long. To sort an array of (N) elements, you only have to sort the first (N - 1) elements. void selectionSort(char s[], int len) { int i, idx; char tmp; for(i = 0; i != len - 1; ++i) { idx = findSmallest(s, i, len); tmp = s[i]; s[i] = s[idx]; s[idx] = tmp; } } > { > idx = findSmallest(s, i, len); > tmp = s[i]; > s[i] = s[idx]; > s[idx] = tmp; > } > } For an array of (N) elements you could decrease the number of swaps to less than (N), and you could also lighten the load on the findSmallest() function. void selectionSort(char s[], int len) { int i, idx; char tmp; for(i = 0; i != len - 1; ++i) { idx = findSmallest(s, i + 1, len); if (s[i] > s[idx]) { tmp = s[i]; s[i] = s[idx]; s[idx] = tmp; } } } -- pete