Skip to content

Instantly share code, notes, and snippets.

@dlion
Created September 11, 2014 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlion/9d59ae9e47f1c832590d to your computer and use it in GitHub Desktop.
Save dlion/9d59ae9e47f1c832590d to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void stampaVettore(int *v, int n)
{
int i;
for(i=0; i < n; i++)
printf("%d ",v[i]);
printf("\n\n");
}
void quicksort(int *x, int first, int last)
{
int pivot,j,temp,i;
if(first<last)
{
pivot=first;
i=first;
j=last;
while(i<j)
{
while(x[i]<=x[pivot]&&i<last) i++;
while(x[j]>x[pivot]) j--;
if(i < j)
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
}
int main()
{
int n;
int i;
int *vettore;
printf("Grandezza vettore: ");
scanf("%d", &n);
srand(time(NULL));
vettore = (int*)malloc(n*sizeof(int));
for(i=0; i < n; i++)
vettore[i] = rand() % 200;
printf("-- Stampa vettore prima --\n");
stampaVettore(vettore, n);
quicksort(vettore, 0, n-1);
printf("-- Stampa vettore dopo --\n");
stampaVettore(vettore, n);
printf("Massimo 1: %d\nMassimo 2: %d\n",vettore[n-1], vettore[n-2]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment