Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[OT] Insertion Sort
View unanswered posts
View posts from last 24 hours
View posts from last 7 days

 
Reply to topic    Gentoo Forums Forum Index Forum italiano (Italian) Forum di discussione italiano
View previous topic :: View next topic  
Author Message
starise
Apprentice
Apprentice


Joined: 09 Oct 2004
Posts: 211
Location: Napoli

PostPosted: Wed Jan 12, 2005 12:39 am    Post subject: [OT] Insertion Sort Reply with quote

Scusate per l'OT, ma ho urgenza di risolvere un problema.
Sapreste farmi un esempio di algoritmo che richiama una funzione void del tipo:

Code:


void insertion_sort(int a[], int n)
{
     int i, j, app;
     for (i=1; i<n; i++)         
     {
     app = a[i];
     j = i-1;
     while (j>=0 && a[j]>app) {
     a[j+1] = a[j];
     j--;
     }
a[j+1] = app;

  return;
}

Scusate ancora e grazie! :)
_________________
Notebook: Dell XPS M1330, Core 2 Duo 2GHz, 2 GB DDR2, HD 200GB 7200rpm, GeForce 8400M - powered by Gentoo
Back to top
View user's profile Send private message
koma
Advocate
Advocate


Joined: 06 Jun 2003
Posts: 2702
Location: Italy

PostPosted: Wed Jan 12, 2005 1:12 am    Post subject: Reply with quote

non so aiutarti però ho un suggerimento:
il forum è un ottimo strumento per trovare aiuto ma spesso per evitare di dover metterci ore ad attendere risposta perchè le si deficita di informazioni aggiuntive si può sfruttare l'IM (istant messaging) io come vedi ho aggiunto al profilo ICQ mail e quant'altro è un ottimo mezzo per poter superare le frontiere del post ^_^ solo un consiglio scusami se mi sono intromesso

my 2 cents
Koma
_________________
http://www.youtube.com/watch?v=_yoJI-Tl94g GLORY GLORY HYPNOTOAD
Back to top
View user's profile Send private message
starise
Apprentice
Apprentice


Joined: 09 Oct 2004
Posts: 211
Location: Napoli

PostPosted: Wed Jan 12, 2005 2:23 am    Post subject: Reply with quote

koma wrote:
non so aiutarti però ho un suggerimento:
si può sfruttare l'IM (istant messaging) io come vedi ho aggiunto al profilo ICQ mail e quant'altro è un ottimo mezzo per poter superare le frontiere del post ^_^ solo un consiglio scusami se mi sono intromesso

Grazie del consiglio! solo che non so a chi rivolgermi!

cmq. ho provato a fare un programma, ma non mi funziona!!!
diavolo! cos'ho sbagliato!? questo maledetto programma mi serve per domani! :(

Vi posto quello che ho fatto:

Code:

#include <stdio.h>
#include <malloc.h>

void insertion_sort(int a[], int n);
main()
{

int    *a;
int    i;     
int    n;

     printf("Inserisci la quantità di numeri da esaminare\n");
     scanf("%d", &n);
     
     a=(int*)malloc(n*sizeof(int));
     
     for (i=1; i<n; i++)
{

printf("inserire un numero\n");
scanf("%d", &a[i]);

}
insertion_sort(a,n);
     
printf("\nArray Ordinato:");

   for (i=0;i<n;i++)

   {

      printf("%d",a[i]);

   }

   free (a);

}

void insertion_sort(int a[], int n) {
 
  int i, j, app;

  for (i=1; i<n; i++) {
    app = a[i];
    j = i-1;
    while (j>=0 && a[j]>app) {
      a[j+1] = a[j];
      j--;
    }
    a[j+1] = app;
  }
 
  return;
 
}

_________________
Notebook: Dell XPS M1330, Core 2 Duo 2GHz, 2 GB DDR2, HD 200GB 7200rpm, GeForce 8400M - powered by Gentoo
Back to top
View user's profile Send private message
fedeliallalinea
Administrator
Administrator


Joined: 08 Mar 2003
Posts: 31675
Location: here

PostPosted: Wed Jan 12, 2005 7:31 am    Post subject: Reply with quote

Una funzione che non ritorna nulla non ha bisogno del return

Code:
#include <stdio.h>

void insertion_sort(int a[], int n) {
  int i, j, app;

  for (i=1; i<n; i++) {
    app = a[i];
    j = i-1;
    while (j>=0 && a[j]>app) {
      a[j+1] = a[j];
      j--;
    }
    a[j+1] = app;
  }
}

int main() {
  int a[8] = {1,5,7,2,8,3,5,6};
  int i;

  /* Stampo l'array non ordinato */
  for(i=0;i<8;i++)
    printf("%d ",a[i]);
  printf("\n");

  insertion_sort(a,8);
 
  /* Stampo l'array ordinato */
  for(i=0;i<8;i++)
    printf("%d ",a[i]);
  printf("\n");
}

_________________
Questions are guaranteed in life; Answers aren't.

"Those who would give up essential liberty to purchase a little temporary safety,
deserve neither liberty nor safety."
- Ben Franklin
https://www.news.admin.ch/it/nsb?id=103968
Back to top
View user's profile Send private message
Mistobaan
Tux's lil' helper
Tux's lil' helper


Joined: 07 May 2004
Posts: 76

PostPosted: Wed Jan 12, 2005 8:57 am    Post subject: Reply with quote

Quando vedo questo tipo di domande, penso subito a compiti per casa.... bisogna sbatterci la testa su questi listati, altrimenti non si sviluppa l'intuito per l'errore...

Posso darti un consiglio:

Quando devi compilare usa
Code:

gcc -Wall -ansi ...


aiuta molto.. :D

spero di essere stato utile
_________________
God is real until declared integer
Back to top
View user's profile Send private message
babalinux
Tux's lil' helper
Tux's lil' helper


Joined: 20 Aug 2003
Posts: 117

PostPosted: Wed Jan 12, 2005 12:08 pm    Post subject: Reply with quote

starise wrote:


cmq. ho provato a fare un programma, ma non mi funziona!!!
diavolo! cos'ho sbagliato!? questo maledetto programma mi serve per domani! :(

Vi posto quello che ho fatto:

Code:

#include <stdio.h>
#include <malloc.h>   /*  1. meglio includere stdlib.h */

void insertion_sort(int a[], int n);

main()  /* 2. secondo lo standard, main() dovrebbe essere di tipo int */
{

int    *a;
int    i;     
int    n;

     printf("Inserisci la quantità di numeri da esaminare\n");
     scanf("%d", &n);
     
     a=(int*)malloc(n*sizeof(int));
     
     /* 3. perche' inizi la scansione da 1? l'array in C e' indicizzato a partire da 0 */
     for (i=1; i<n; i++)
{

printf("inserire un numero\n");
scanf("%d", &a[i]);

}
insertion_sort(a,n);
     
printf("\nArray Ordinato:");

   for (i=0;i<n;i++)

   {

      printf("%d",a[i]);

   }

   free (a);

}

void insertion_sort(int a[], int n) {
 
  int i, j, app;

  for (i=1; i<n; i++) {
    app = a[i];
    j = i-1;
    while (j>=0 && a[j]>app) {
      a[j+1] = a[j];
      j--;
    }
    a[j+1] = app;
  }
 
  return;
 
}


Questa la versione riveduta e corretta:

Code:
#include <stdio.h>
#include <stdlib.h>

void insertion_sort(int a[], int n);

int main()
{

   int    *a;
   int    i, n, t=0;

    printf("Inserisci la quantità di numeri da esaminare\n");
    scanf("%d", &n);
     
   /*   creazione ed inizializzazione array */
    a=(int*)malloc(n*sizeof(int));
   for (i=0;i<n;i++)   
     a[i] = 0;

   /*   DEBUG */
   printf("\nArray:\n");
   for (i=0;i<n;i++)   
     printf("%d\n",a[i]);
     
   /*   input (iniziavi da 1) */
    for (i=0; i<n; i++)   {
      printf("inserire un numero\n");
      scanf("%d", &t);
      a[i] = t;
   }

   /*   DEBUG */
   printf("\nArray: \n");
   for (i=0;i<n;i++)   
     printf("%d\n",a[i]);

   insertion_sort(a,n);
     
   printf("\nArray Ordinato: \n");
   for (i=0;i<n;i++)   
     printf("%d\n",a[i]);

   /* deallocazione */
   free (a);

   return(0);
}

void insertion_sort(int a[], int n) {
 
  int i, j, app;

  for (i=1; i<n; i++) {
    app = a[i];
    j = i-1;
    while (j>=0 && a[j]>app) {
      a[j+1] = a[j];
      j--;
    }
    a[j+1] = app;
  }
}


P.S.: Ti consiglio di annidare meglio il codice.

cheers,
baba
_________________
"...Funny things you learn from your mama, like the way to throw your head back when your swallowing pills..."
Back to top
View user's profile Send private message
fedeliallalinea
Administrator
Administrator


Joined: 08 Mar 2003
Posts: 31675
Location: here

PostPosted: Wed Jan 12, 2005 12:39 pm    Post subject: Reply with quote

Poi consiglio questo tool
Code:
*  dev-util/efence
      Latest version available: 2.2.2
      Latest version installed: [ Not Installed ]
      Size of downloaded files: 27 kB
      Homepage:    http://perens.com/FreeSoftware/
      Description: old crusty malloc() debugger for Linux and Unix
      License:     GPL-2

per quando si programma con molti malloc, calloc e compagnia bella
_________________
Questions are guaranteed in life; Answers aren't.

"Those who would give up essential liberty to purchase a little temporary safety,
deserve neither liberty nor safety."
- Ben Franklin
https://www.news.admin.ch/it/nsb?id=103968
Back to top
View user's profile Send private message
randomaze
Bodhisattva
Bodhisattva


Joined: 21 Oct 2003
Posts: 9985

PostPosted: Wed Jan 12, 2005 1:22 pm    Post subject: Reply with quote

fedeliallalinea wrote:
per quando si programma con molti malloc, calloc e compagnia bella


Io tifo per valgrind :-D
_________________
Ciao da me!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Forum italiano (Italian) Forum di discussione italiano All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum