Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #112001

Re: Arrays sorting!!

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: Arrays sorting!!
Date 2017-06-12 12:03 +0100
Organization A noiseless patient Spider
Message-ID <87tw3lcvs1.fsf@bsb.me.uk> (permalink)
References <ohl2ii$b85$2@gioia.aioe.org>

Show all headers | View raw


Christopher <info@invalid.invalid> writes:

> Can anybody tell me why is this array giving me corruption?

OK, that's been done.  But let me comment o the algorithm...

> /* Program begins */
> #include <stdio.h>
> #include <time.h>
> #include <stdlib.h>
>
> void sort (int a[], int n);
>
> int main (void)
> {
>
> 	int i, j, k, nums[ 60 ];
>
> 	srand( ( int ) time( NULL ) ) ;
>
> 	for ( i = 0; i < 60; i++ )
> 		nums[ i ] = i;
>
> 	for ( i = 0 ; i < 60 ; i++ )
> 	{
> 		j = ( rand() % 60 ) + 1 ;
> 		k = nums[ i ] ;
> 		nums[ i ] = nums[ j ] ;
> 		nums[ j ] = k ;
> 	}

Do a search for Fisher-Yates shuffle.  It's only a small variation on
yours but, in its modern form, allows you to stop shuffling when you
have the six elements you need.

Another option is to use Floyd's choose n from m algorithm:

  void choose(int choose_n, int from_m, int *chosen)
  {
       for (int i = 0, j = 0; j < choose_n; i++)
            if (true_with_probability(choose_n - j, from_m - i))
                 chosen[j++] = i;
  }

The function

  bool true_with_probability(int n, int m);

returns 1 with probability n/m.

<snip>
-- 
Ben.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Arrays sorting!! Christopher <info@invalid.invalid> - 2017-06-12 04:55 +0100
  Re: Arrays sorting!! luser droog <luser.droog@gmail.com> - 2017-06-11 21:30 -0700
    Re: Arrays sorting!! bartc <bc@freeuk.com> - 2017-06-12 13:32 +0100
      Re: Arrays sorting!! bartc <bc@freeuk.com> - 2017-06-12 13:48 +0100
        Re: Arrays sorting!! supercat@casperkitty.com - 2017-06-12 07:36 -0700
          Re: Arrays sorting!! David Brown <david.brown@hesbynett.no> - 2017-06-12 21:22 +0200
            Re: Arrays sorting!! supercat@casperkitty.com - 2017-06-12 14:00 -0700
              Re: Arrays sorting!! David Brown <david.brown@hesbynett.no> - 2017-06-12 23:39 +0200
                Re: Arrays sorting!! "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-12 18:47 -0400
  Re: Arrays sorting!! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-12 12:03 +0100
    Re: Arrays sorting!! "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2017-06-12 20:23 +0800
      Re: Arrays sorting!! mark.bluemel@gmail.com - 2017-06-12 07:32 -0700
        Re: Arrays sorting!! "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2017-06-12 23:06 +0800
      Re: Arrays sorting!! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-12 16:08 +0100
        Re: Arrays sorting!! "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2017-06-12 23:11 +0800
          Re: Arrays sorting!! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-12 16:46 +0100
            Re: Arrays sorting!! "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2017-06-13 17:49 +0800
    Re: Arrays sorting!! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-12 15:57 +0100
  Re: Arrays sorting!! Real Troll <Real.Troll@Trolls.com> - 2017-06-12 16:55 -0400

csiph-web