Search
 
SCRIPT & CODE EXAMPLE
 

C

quick sort c

#include<stdio.h>
#define MAX 100

int insert_array(int a[]) {
  int n, i;
  printf("Quanti elementi?: ");
  scanf("%d", &n);

  for (i=0; i<n; i++) {
  	 printf("elemento %d: ", i);
  	    scanf("%d", &a[i]);
  }
  return(n);
}

void stampa_array(int a[], int n) {
  int i;
  for (i=0; i<n; i++) {
    printf("%d ", a[i]);
  }
  printf("
");
  return;
}

void quicksort(int a[MAX],int primo,int ultimo){
   int i, j, pivot, temp;
/*pivot -- inizialmente il pivot è il primo elemento
primo e ultimo sono le due variabili che servono per scorrere l'array
*/
   if(primo<ultimo){
      pivot=primo;
      i=primo;
      j=ultimo;     
      
      while(i<j){
         while(a[i]<=a[pivot]&&i<ultimo)
            i++;
         while(a[j]>a[pivot])
            j--;
         if(i<j){   
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
         }
      }

      temp=a[pivot];
      a[pivot]=a[j];
      a[j]=temp;
      quicksort(a,primo,j-1);
      quicksort(a,j+1,ultimo);
   }
}

int main(){
   int n, a[MAX],i;
   n = insert_array(a);
   printf("Array iniziale: "); 
   stampa_array(a,n);    
   quicksort(a,0,n-1);
   printf("Array ordinato con quick-sort: ");
   stampa_array(a,n);
   return 0;
}
Comment

quicksort in c

int cmpfunc (const void * a, const void * b) {
   return ( *(int*)a - *(int*)b );
}
Comment

PREVIOUS NEXT
Code Example
C :: adjacency matrix representation maker 
C :: clear screen in c 
C :: Bitwise Operators in C language 
C :: gcd and lcd in c 
C :: read file c 
C :: how to change background color in c programming 
C :: mongo script to find collection size in database 
C :: replace a substring with another substring in c 
C :: passing file as argument in c 
C :: KneesDev 
C :: how to transform a char to ascii code in c 
C :: leggere stringhe con spazio in mezzo c 
C :: getchar c 
C :: how to print % in c 
C :: command args c 
C :: scan c 
C :: how to make two arrays equal in c 
C :: what is the last character of a string in c 
C :: what is %d in C 
C :: ansi c read write bmp 
C :: c to assembly converter 
C :: how tier lists work 
C :: create a gtk window 
C :: %s and string pointer 
C :: Print Characters 
C :: Handling exceptions during datetime conversion 
C :: pdo crud 
C :: timespec c 
C :: manasa loves maths solution IN C 
C :: why return 0 is written at the code end? 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =