-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSort.c
90 lines (86 loc) · 2.16 KB
/
QuickSort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define N 30
#define M 1000
int A[N], opc, i, j, aux, p_max, x;
int particao(left,right){
int i,j,aux;
double x;
x = A[right];
i = left-1;
for(j=left;j<=right-1;j++){
if(A[j]<=x){
//troca(A[i],A[j]);
i++;
aux=A[i];
A[i]=A[j];
A[j]=aux;
}
}
//troca(A[i+1],A[right]);
aux=A[i+1];
A[i+1]=A[right];
A[right]=aux;
return (i+1);
}
void quicksort(left, right){
int q;
if (left<right){
q = particao(left,right);
quicksort (left, q-1);
quicksort (q+1, right);
}
}
int main(){
while(opc != 4){
printf("******MENU*******\n\n1 - Vetor Ordenado\n2 - Vetor Aleatorio\n3 - Vetor Ordem Inversa\n4 - Sair \n\nOpcao: ");
scanf("%d", &opc);
switch(opc)
{
case 1:{
printf("\nVETOR ORIGINAL\n");
for(i=0; i<N; i++){
A[i] = i;
printf("%d \n", A[i]);
}
quicksort(0, N-1);
printf("\nVETOR ORDENADO\n");
for(i=0; i<N; i++){
printf("%d \n", A[i]);
}
break;
}
case 2:{
printf("\nVETOR ORIGINAL\n");
for(i=0; i<N; i++){
A[i] = rand() % M;
printf("%d \n", A[i]);
}
quicksort(0, N-1);
printf("\nVETOR ORDENADO\n");
for(i=0; i<N; i++){
printf("%d \n", A[i]);
}
break;
}
case 3:{
x = N;
printf("\nVETOR ORIGINAL\n");
for(i=0; i<N; i++){
A[i] = x;
x--;
printf("%d \n", A[i]);
}
quicksort(0, N-1);
printf("\nVETOR ORDENADO\n");
for(i=0; i<N; i++)
{
printf("%d \n", A[i]);
}
break;
}
}
getch();
}
}