You are using a version without Ads of this website. Please, consider donating:

### sorting (array) c++ programming

hello...i am newbie, and i cant do my task,
to Create a program to sort the data by using the method:
- Selection sort
- Insertion Sort
- Bubble Sort
- Merge Sort
- Quick Sort

where the Methods are made ​​in the method / function in the main program and can be called by method (Array A);
example :
selectionSort(A);
insertionSort(A);
bubbleSort(A);
mergeSort(A,0,n-1);
quickSort(A,0,n-1);

that includes a numbers element [Array]is the user from out the program with max_element = 10000

please, i really - really need help to do it i am not understand how they could be in the main function
I think that this exercise is asking you to write a main() which calls each of those 5 sort functions.

 ``12345678910`` ``````int main() { // get Array filled selectionSort(A); insertionSort(A); bubbleSort(A); mergeSort(A,0,n-1); quickSort(A,0,n-1); // display sorted results }``````

The example is showing how the functions must be called. This gives you the prototypes for the 5 sort functions to be defined.

Last edited on
but i dont know why my code can't to run, i also can to implementation merge sort, ... can you help me

#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;
}
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;
}

Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: