sorting (array) c++ help please

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_elemen 10000

please, i really - really need help to do it i am not understand how they could be in the main, function
If you wrote each sort function you could then have the user enter a choice and use a switch statement in main so you use the appropriate sort function. I'm not sure I totally understand your question. Don't copy this code it is not correct just to illustrate.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{



switch( choice)
{
     case 1: selectionSort(A);
     break;
     case 2: insertionSort(A);
     break;
     case 3: .......;


    default: cout << "Invalid choice " << endl;
}



return 0;
} 

Like I said this is really a rough I idea I didn't write it in visual studio so this code is more then likely errors. But it does serve to illustrate the idea of how you can have the different type of sort functions in main. You can have a little user prompt message before the switch. I think this is what you are asking. If not sorry.
thanks i have code like this to answer my task like i ask you,
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;
}
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;
}














































Hi putri, can you use code tag first for writing your code? It makes easier for everyone to read it :)
And i just read some of your initial codes, i think that it would be better if you use do...while instead repeating the same command. Like:

1
2
3
do {
      std::cout << "select (1) ascending or (2) for descending order";
} while (order < 1 || order > 2);


and so for the other codes, i saw it can be more efficient

Hope helps :)
www.cplusplus.com/forum/beginner/88778/
Topic archived. No new replies allowed.