I was wondering, how do I put in an index number for n elements. The program provided asks you to enter the numbers and it puts them in descending order, but I need an index number to be put with the values you enter for n to be the same for that exact number you put in.
Here's the code I have:
#include <iostream>
using namespace std;
int Partition(int low,int high,int arr[]);
void Selection_sort(int low,int high,int arr[]);
int main()
{
int *a,n,low,high,i;
cout<<"/**************************Selection Sort Algorithm Implementation*****************/";
cout<<"Enter number of elements: ";
cin>>n;
a=new int[n];
cout<<"enter the elements: "<<endl;
for(i=0;i<n;i++)
{
cin >> a[i];
}
//for(i=0;i<n;i++)
//a[i]=rand()%100;
cout<<"Element: \tInitial Order of elements: \tIndex"<<endl;
for(i=0;i<n;i++)
cout<<i+1<<"\t"<<a[i]<<endl;
high=n-1;
low=0;
Selection_sort(low,high,a);
cout<<"Element: \tFinal Array After Sorting: \tIndex:"<<endl;
for(i=0;i<n;i++)
cout<<i+1<<"\t"<<a[i]<<endl;
system("pause");
return 0;
}
int Partition(int low,int high,int arr[])
{ int i,high_vac,low_vac,pivot/*,itr*/;
pivot=arr[low];
while(high>low)
{ high_vac=arr[high];
while(pivot>high_vac)
{
if(high<=low) break;
high--;
high_vac=arr[high];
}
arr[low]=high_vac;
low_vac=arr[low];
while(pivot<low_vac)
{
if(high<=low) break;
low++;
low_vac=arr[low];
}
arr[high]=low_vac;
}
arr[low]=pivot;
return low;
}
void Selection_sort(int low,int high,int arr[])
{
int Piv_index,i;
if(low<high)
{
Piv_index=Partition(low,high,arr);
Selection_sort(low,Piv_index-1,arr);
Selection_sort(Piv_index+1,high,arr);
}
}
Thanks for your help