sorting (array) c++ who user can enter numbers from output.
#include <iostream>
using namespace std;
void enternumber (int *array, int N);
void bubblesort (int *array, int N, int Order);
void selectionsort (int *array, int N, int Order);
void insertionsort (int *array, int N, int Order);
void quicksort (int *array, int left, int right, int N, int Order);
int split (int lower, int upper, int *array, int Order);
void displayarray (int *array, int N);//fungsi prototype
void main ()
{
int n;
int * array;
int order, sorting=0;
cout<<"select (1) for ascending, (2) for descending order";
cin>>order;
while (order !=1&&order !=2)
{
cout<<"input error"<<endl;
cout<<"selct (1) for ascending, (2) for descending order";
cin>>order;
}
cout<<"Enter list of size";
cin>>n;
array=new int[n];
enternumber (array,n);
cout<<"select sorting tekhnik"<<endl;
cout<<" 1) Bubble sort"<<endl;
cout<<"2) selection sort"<<endl;
cout<<"3) Insertion sort"<<endl;
cout<<"4) Quick sort"<<endl;
cin>>sorting;
while ((sorting!=1)&&(sorting!=2)&&(sorting!=3)&&(sorting!=4))
{
cout<<"Input error"<<endl;
cout<<"select sorting tekhnik:"<<endl;
cout<<" 1) Bubble sort"<<endl;
cout<<"2) selection sort"<<endl;
cout<<"3) Insertion sort"<<endl;
cout<<"4) Quick sort"<<endl;
cin>>sorting;
}
cout<<"your numbers are :";
displayarray (array,n);
cout<<"your choice of sorting is :"<<sorting<<endl;
if (sorting==1)
bubblesort(array,n,order);
else if (sorting==2)
selectionsort(array,n,order);
else if (sorting==3)
insertionsort(array,n,order);
else
{
quicksort(array,0,n-1,n,order);
cout<<"sorted list for quick sort :";
displayarray(array,n);
}
void enternumber (int *array, int N)
{
int i;
for (i=0;i<N;i++)
{
cout<<"enter number : ";
cin>>array[i];
}
}
void bubblesort(int *array,int N, int Order)
{
int i,j,temp=0;
for(i-0;i<N-1;i++)
for (j=1;j<N;j++)
{
if ((array[j]<array[j-1]&&(Order==1))
{
temp=array[j];
array [j]=array[j-1];
array [j-1]=temp;
}
else if((array[j]array[j-1]&&(Order==2)
{
temp=array[j];
array[j]=array[j-1];
array[j-1]=temp;
}
}
cout<<"sorted list for bubble sort:";
displayarray(array,N);
}
void selectionsort(int *array, int N, int Order)
{
int i,j,min,temp=0;
for(i-0;i<N-1;i++)
{
min=i;
for(j=i+1;j<N;j++)
{
if((array[j]<array[j-1]&&(Order==1))
min j;
else if((array[j]>array[j-1]&& (Order==2))
min=j;

}
temp=array[min];
array[min]=array[i];
array[i]=temp;
}
cout<<"sorted for selection sort : ";
displayarray (array,N);
}
void insertionsort(int *array,int N, int Order)
{
int i,j,temp=0;
for(i=1;i<N;i++)
{
temp=array[i];
j=i-1;
if(Order==1)
{
while (array[j]>temp)
{
array[j+1]=array[i];
j--;
}

}
else if (Order==2)
{
while((array[j]<temp)&&(j>=0))
{
array [j+1}=array[j];
j--;
}
}
array[j+1]=temp;
}
cout<<"sorted list for insertion sort:";
displayarray(array,N);
}
void quicksort(int *array, int left, int right, int N, int Order)
{
if (right>left)
{
int pivot=split(left, right,array,Order);
quicksort(array,left,pivot-1,N,Order);
quicksort(array,pivot+1;right,N,Order);
}
}
int split(int left, int right,int *array, int Order)
{
int pivot=array[left];
if(Order==1)
{
while(true)
{
while (array[left]<pivot)
left++;

while(array[right]>pivot)
right--;
if(left<right)
swap(array[left],array[right]};
else
return right;
}

}
else if (Order==2)
{
while(true)
{
while (array[left]<pivot)
left++;
while(array[right]>pivot)
right--;
if(left<right)
swap(array[left],array[right]};
else
return right;
}
}
}

void displayarray(int *array, int N)
{
int i;
for (i-0;i<N;i++)
cout<<array[i]<<" ";
cout<<"\n";
}
return 0;
}

Please help

----------
thanks for helping

Recommended Answers

All 2 Replies

How is it not working? Does it compile? Does it crash? Does it produce the wrong result? What errors are produced?

In just the bubble sort I see several typos.

Don't have your sort functions call the display function - do that back in main.

Please, please use some horizontal and vertical spacing and indenting

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.