Quicksort incorrect results - c++

I have this quicksort code which I want to troubleshoot. There is this code that I have changed in the partition. Have tried selected the pivot element as the median of the array. But still it does not give me the correct output. Here I have different four types of arrays that I have created dynamically and trying to sort them. There are quicksort applied four times.
int counter=0, comparison=0;
void QUICKSORT(int *,int,int);
int PARTITION(int * ,int,int);
int values(float l);
int main(){
int i,n,q;
cout<<"Enter number of elements:";
cin>>n;
int* a = new int[ n ];
for(int p=1;p<=n;p++)
{
q = rand() % (n*100) ;
a[p]=q;
}
cout<<"\nThe elements od the original array are:\n\n";
for(i=1;i<=n;i++)
cout<<a[i]<<" ";
clock_t tStart = clock();
QUICKSORT(a,1,n);
cout<<"\nThe elements of the sorted array are:\n\n";
for(i=1;i<=n;i++)
cout<<a[i]<<" ";
}
// function for the generation of random poisson values. This function is called as many times
// as the array size decided by the user
int values(float l)
{
for (int i=0;i<1000;i++)
{
float L=exp(-l);
float k=0;
float p=1;
//begin while
do{
k=k+1;
double u = rand() / (double)RAND_MAX;
p=p*u;
} while (p>L);
return k-1;
}
}
//}
void QUICKSORT(int *a,int p,int r){
int q;
if(p<r){
q=PARTITION(a,p,r);
QUICKSORT(a,p,q-1);
QUICKSORT(a,q+1,r);
}
}
int PARTITION(int *a,int p,int r){
counter++;
// cout<<" first a " <<a[p]<<endl;
// cout<<" second a : "<<a[r-1]<<endl;
int x=(a[r]+a[p])/2;
//int x=a[r];
int i = p-1,temp,j;
for(j=p;j<=r-1;j++){
if(a[j]<=x){
i=i+1;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[i+1];
a[i+1]=a[r];
a[r]=temp;
return(i+1);
}

At a glance..
QUICKSORT(a,p,q-1);
QUICKSORT(a,q+1,r);
That looks like it should be either:
QUICKSORT(a,p,q);
QUICKSORT(a,q+1,r);
or:
QUICKSORT(a,p,q-1);
QUICKSORT(a,q,r);
But also that partition method looks very questionable. Even if it successfully finds the median I doubt it's worth the effort. The point of quicksort is to sort with as few comparisons as possible, so looping through each entire subarray to find the median kind of defeats that purpose, and is overkill. I would suggest picking three items: first, middle, last, and just using the median of those.

Related

Trying to save different values in 2 arrays using one FOR loop but the first array is been attributed the value of the second input

I am testing the ability to use functions to input values in 2 arrays and also to perform addition of the values. I wish to input the different values in the 2 arrays using a function inputArray(A1, A2, size) using a For loop.
I also used a function sumArray(A1, A2, size) to perform the addition of the 2 values in the arrays.
But the issue is with the input function as when i am running the program to input different values in the 2 arrays, the first array is also been attributed the value of the second array.
void inputArray(int Array1[], int Array2[], int n)
{
for(int i=0; i<n; i++)
{
cout<<"Array 1:"<<endl;
cin>>Array1[i];
cout<<" Array 2: "<<endl;
cin>>Array2[i];
cout<<endl;
//test
cout<<"\n\t A1[0]= "<<Array1[0]<<endl;
cout<<"\n\t A2[0]="<<Array2[0]<<endl;
}
}
I tried to use 2 different functions to input the values and this worked.But then again when i used a FOR loop for the Addition function sum = sumArray(A1, A2, size), both arrays A1 and A2 were being attributed the value of the second array.
int sumArray(int Array1[], int Array2[], int n)
{
int sum= 0;
for(int i=0; i<n; i++)
{
sum+= ( Array1[i] + Array2[i] );
cout<<"The total sum so far for values till position "<<i<<" is "<<sum<<endl;
}
//to monitor if the program is performing well with the addition
//test
cout<<"\n\t A1[0]= "<<Array1[0]<<endl;
cout<<"\n\t A2[1]="<<Array2[2]<<endl;
return sum;
}
#include<iostream>
using namespace std;
void inputArray(int Array1[], int Array2[], int n)
{
for(int i=0; i<n; i++)
{
cout<<"Array 1:"<<endl;
cin>>Array1[i];
cout<<" Array 2: "<<endl;
cin>>Array2[i];
cout<<endl;
//test
cout<<"\n\t A1[0]= "<<Array1[0]<<endl;
cout<<"\n\t A2[0]="<<Array2[0]<<endl;
}
}
//To sum all the values in the 2 arrays//not asked in the question
int sumArray(int Array1[], int Array2[], int n)
{
int sum= 0;
for(int i=0; i<n; i++)
{
sum+= ( Array1[i] + Array2[i] );
cout<<"The total sum so far for values till position "<<i<<" is "<<sum<<endl;
}
//to monitor if the program is performing well with the addition
//test
cout<<"\n\t A1[0]= "<<Array1[0]<<endl;
cout<<"\n\t A2[1]="<<Array2[2]<<endl;
return sum;
}
int main()
{
int size;
int A1[size];
int A2[size];
size= 3;
cout<<"Input "<<size<<" values in the first and second array: "<<endl;
inputArray( A1, A2, size); //do not write square bracket when calling a function
int sum = sumArray(A1, A2, size);
cout<<"The sum of the total values is : "<<sum<<endl;
//test
cout<<"\n\t A1[0]= "<<A1[0]<<endl;
cout<<"\n\t A2[1]="<<A2[2]<<endl;
return 0;
}
Here you use a non-initialised variable for the size of the two arrays.
int size;
int A1[size];
int A2[size];
This is in C++ wrong in more than one way.
You need to define C-like arrays with a constant size.
Safer would be to use C++ containers, e.g. std::vector for anything without predictable size.
Even in C, where VLAs are possible, creating them like you did and LATER reading in a new value for size will not change anything, especially not the size of the arrays.
Also, but that is already inside undefined behaviour and purely speculative,
if the compiler understands that as A1 and A2of size 0, then the start of both arrays is the same and writing to one writes also to the other - AND be totally forbidden because it will access beyond any arrays size.
To demonstrate that, try
int A1[5];
int A2[5];

Sorting by selection in an array c++

so my program should be able to take several numbers from the user until the last number is "999". Then, it should be able to print out an array having all the numbers entered sorted. I have to use sorting by selection (by comparing values in each index).
I tried by best and i don't seem to know what is going wrong
this is my code: (sorry for the lack of tidiness and comments, I'm in a hurry)
#include <iostream>
using namespace std;
int main() {
int n;
int i;
int sentinel =999;
int A[250];
cout <<"Please enter the list of nbs ending with 999"<< endl;
cin>>n;
i=0;
int nblist;
while(n!=sentinel)
{
A[i]=n;
cin>>n;
i++;
}
nblist = i-1;
int minindex;
i =0;
int min;
int k;
k=0;
min=A[k];
for(k=0;k<nblist;k++)
{cout<<k<<endl; int i =0;
while(i<nblist){
cout<<i<<endl;
if (A[i]< min)
{
min= A[i];
if (A[i]==min)
{minindex=j;}
i++;
//cout<< "the array containing min is "<< minindex << endl;
}
cout<<"The nb of array is "<<i<<"its filled with"<< A[i]<<endl;
}int temp;
temp= A[k];
A[k]=A[minindex];
A[minindex]=temp;
//cout<<"The nb of array is "<<k<<"its filled with"<< A[k]<<endl;
//cout<<"The nb of array is "<<minindex<<"its filled with"<< A[minindex]<<endl;
int counter=0;
while(counter<nblist)
{ cout<<"The nb of array is "<<counter<<"its filled with"<< A[counter]<<endl;
counter++;
}
}
return 0;
}

QuickSort not sorting array

I've been trying to make a qsort algorithm, but so far I've failed miserably. Keep in mind I'm kind of a newbie when it comes to programming, so yeah. After I build and run, and input my array, it returns the same exact array, instead of sorting it. Here's the code in question:
#include <iostream>
using namespace std;
int v[11], i, n, st, dr;
void qsort (int v[11], int st, int dr)
{
int i=st, j=dr;
int aux;
int pivot = v[(st+dr)/2];
while(i<=j)
while(v[i]<pivot)
{
i++;
if(i<=j)
{
aux=v[i];
v[i]=v[j];
v[j]=aux;
i++;
j--;
}
}
if(st<j)
qsort(v,st,j);
if(i<dr)
qsort(v,i,dr);
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>v[i];
st=v[1];
dr=v[n];
qsort(v, st, dr);
cout<<"vectorul sortat este"<<' ';
for(i=1;i<=n;i++)
cout<<v[i]<<' ';
return 0;
}
Thanks in advance!
st and dr should be the initial and final indices where you want to sort, not the values (also, keep in mind that in C++ a vector on n elements has indices from 0 to n-1, so fix also your for loops), so you have to change
st=v[1];
dr=v[n];
to
st=0
dr=n-1
or simply:
qsort(v, 0, n-1);

Sorting an array from lowest to greatest

So i'm in my first programming class and am having some trouble with an assignment. The assignment was to make a code that asks the user for six integers and six letters and to store them in arrays. Then print a bar graph with the data sorted from lowest to highest. That is the part i am having trouble with. I named it the Arrange function, and to me everything looks right. I was just hoping someone could tell me where i went wrong in swapping the lowest integer. Any other comments on the code would be much appreciated! Thanks (I also cant use sort functions from a library, or pointers)
#include <iostream>
using namespace std;
void Arrange(int num[],int order[],char let[],char letOrder[],int cap);
void Swap(int order[],char let[],int a,int b);
void BuildGraph(int number[],char letter[],int max);
int getMax(int number[]);
int main(){
int Number[6];
char Letter[6];
int Order[6];
char LetterOrder[6];
int max;
cout<<"Please Enter 6 Integers: "; //gets 6 integers
for(int i=0;i<6;i++){
cout<<"Ineteger # "<<i+1<<" = ";
cin>>Number[i];
cout<<endl;
}
cout<<"Please Enter 6 Characters: "; //gets 6 characters
for(int i=0;i<6;i++){
cout<<"Enter a character here: ";
cin>>Letter[i];
cout<<endl;
}
max= getMax(Number);
BuildGraph(Number,Letter,max);
Arrange(Number,Order,Letter,LetterOrder,max);
BuildGraph(Order,LetterOrder,max);
return 0;
}
int getMax(int number[]){
int max=number[0];
for(int i=0;i<6;i++){
if(max<number[i]) max=number[i];
}
return max;
}
void Arrange(int num[],int order[],char let[],char letOrder[],int cap){
int i,n;
order=num;
letOrder=let;
for(i=0;i<5;i++){
for(n=0;n<6;n++){
if(order[i]>order[n]){
Swap(order,letOrder,i,n);
}
}
}
cout<<order[4];
}
void Swap(int order[],char let[],int a,int b){
int temp1=order[b];
order[b]=order[a];
order[a]=temp1;
int temp2=let[b];
let[b]=let[a];
let [a]=temp2;
}
void BuildGraph(int number[],char letter[],int max){
cout<<"Bar Chart"<<endl;
for(int row=max;row>=1;row--){
for(int col=0;col<6;col++){
if(row<=number[col]) cout<<letter[col]<<" ";
else cout<<" ";
}
cout<<endl;
}
}
It would probably be easier to visualize and implement if you have separate functions for arrange and swap.
void arrangeNum(int num[])
void arrangeChar(char char[])
void swapInt(int x, int y)
void swapChar(char x, char y)
Then the arrange functions will just be bubble sorts. But you also lose scope when calling swap so they don't actually do anything. It would be easier to just swap them within the Arrange functions.

calculator in c++ adding function

So my program makes a random (x,y) arrays, what i want to do is to make x a real number and y imaginary number and add them:
my main program
#include "complx.h"
int main(){
const int n = 2;
Complex a[n];
getData(a,n);
printArray(a,n);
isort(a,n);
printArray(a,n);
complex_sum(a,n);
return 0;
}
it also prints and arranges the arrays, but what I am interested in is how to add the arrays, when for example I would use 4 arrays (x,y)(x,y)(x,y)(x,y).
this is how i get a random numbers
void getData(Complex a[],int n){
int i;
srand(time(0)); //If comment this out, get same sequence with each run.
for(i=0; i<n; i++){
a[i].x = rand()%3; //3 and 10 are just for testing isort
a[i].y = rand()%10;
}
return;
}
and here is how i'm trying to add it:
void complex_sum(Complex a[], int n){
for (int i=0; i<n; i++)
cout<<"("<<a[i].x+a[i].x<<")";
cout<<endl;
return;
I am stuck on how to add (x,y)(x,y)= x+yi
thanks
I am not completely inline with the way you are attempting; but to sum n number of complex numbers stored in an array and print it on screen you can try the following:
void complex_sum(Complex a[], int n){
int real=0,img=0;
for (int i=0; i<n; i++){
real+=a[i].x;
img+=a[i].y;
}
cout<<"("<<real << "+" << img << "i)";
}