major or egual problem

Jul 24, 2018 at 4:49pm
hi guys, can't get why in the last function("QUICKSORT", that is a way to order a vector using the previous function "partitionsort") if in the condition for the "if" i use (a<b) it works but (a!=b) it dooesn't. That is strange cuz should be pretty the same condition when the program run the step.So why this happen?

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
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;const int ms=50;int i,n;

void printintvector(int a[]){cout<<"{";for(i=0;i<n;i++){cout<<a[i]<<",";}cout<<"}"<<endl<<endl;}

void randomintvector(int v[],int a,int b){cout<<"dammi min e max e ti "
"daro "<<n<<" numeri casuali compresi tra quei min e max: \n\n";cout<<"min: ";cin>>a;
cout<<"max: ";cin>>b;for(int i=0;i<n;i++){v[i]=rand()%(b-a+1)+a;};for(int i=0;i<n;i++)
{cout<<v[i]<<" ";}cout<<endl;}


int partitionsort(int v[],int a, int b){int t,i,j,g,f;a=a-1;b=b-1;s=a;
for(i=a+1;i<=b;i++){if(v[a]>=v[i]){s++;};};//tiene i conto dei passi da fare per il perno
t=v[a];v[a]=v[s];v[s]=t;           //sposta il perno
for(i=a;i<s;i++){for(j=b;j>s;j--) //sposta gli elementi nella parte guista del vettore
{if(v[i]>v[s]&&v[j]<=v[s]){t=v[i];v[i]=v[j];v[j]=t;}}};
return s+1;} //e ritorna l'indice del perno

void quicksort(int v[], int a, int b){if(a!=b){int x;x=partitionsort(v,a,b);
quicksort(v,a,x-1);quicksort(v,x+1,b);};}


int main(){

int v1[ms],v2[ms],a,b,p;
srand(time(0));
do{cout<<"dimensione del vettore(max="<<ms<<"): ";cin>>n;}while(n>ms);

randomintvector(v1,a,b);quicksort(v1,1,n);printintvector(v1);

return 0;}
Last edited on Jul 24, 2018 at 4:50pm
Jul 24, 2018 at 5:03pm
I'm not going to bother trying to figure out what that says. Fix your formatting. Your code is unreadable as it is.
Jul 24, 2018 at 5:55pm
what happens in quicksort and partitionsort when a>b? Is it possible to get a>b in your code?

if you can figure out those 2 things, you will see why it does not work.
so the first thing to do is leave it with your change (a!=b) and run it and print out a and b when b>a. See if that is possible. then figure out what it did if it is possible.

Last edited on Jul 24, 2018 at 5:56pm
Jul 24, 2018 at 6:29pm
1
2
void quicksort(int v[], int a, int b){if(a!=b){int x;x=partitionsort(v,a,b);
quicksort(v,a,x-1);quicksort(v,x+1,b);};}

These bounds don't even look right, not to mention partitionsort() function seems sketchy.

1
2
3
4
5
int main(){
// ...
quicksort(v1,1,n);
//...
return 0;}

This looks like it's sorting from index 1 to an index past the end of the array.

Please show the algorithm you were trying to follow, specifically which implementation. Because if you're just making it up as you go along, I'd recommend a known working one like https://en.wikipedia.org/wiki/Quicksort#Hoare_partition_scheme
Jul 24, 2018 at 6:58pm
do you really code like this?
Topic archived. No new replies allowed.