I have an array of unknown size. I need to sort the vector (can be smallest to largest or largest to smallest) BUT I need to keep the index of the array the same as it was before or I need to find a way to keep the beginning index attached to the specific array value. It is probably easier to see in an example so here is an example:
array = .2, .3, .1, .4
where: i[1]= .2, i[2] =.3, i[3]= .1 i[4]=.4
I need to sort this array into something like:
sorted array = .1, .2, .3, .4
while I keep the index value of 3 associated with .1, 1 associated with .2, 2 associated with .3 and 4 associated with .4.
The only way I can see to sort the array, however, is by changing the index values. If I do this though, I don't have a way to use the index value in the future when I need to.
#include<iostream>
usingnamespace std;
int i;double aarrycpy[];
double aarry[];
double in;
main()
{
for(in>=0;i++) // i is the index it increases everytime there a number input
{cout<<"Numbers for array \n Enter neg number to sort";
cin>>in;
aarry[i]=in;
aarrycpy[i]=aarry[i];} //this keeps original array index same bc only the cpy is changed later
sort(aarrycpy,i); // this calls sort function
}
double sort(double aarrycpy[],int i) // sort function
{ double temp;
int x;
int swaps;
while(swaps>0){ // loops till no sorts are made
for(x=0;x<i;x++)// sry leftovers from a different post
// checks if arraycpy[x]<arraycpy[x+1]
if (aarrycpy[x] < aarrycpy[x+1])// this determines which direction to go
swaps=0; //counts if a sort was made or not
temp=aarrycpy[x+1];// this section swaps arrycpy[x] an arrycpy[x+1] if above is true
aarrycpy[x+1]=aarrycpy[x];
aarrycpy[x]=temp;
swaps++; // counts a swap
}
So you're trying to change the index of the items in the array...without changing the index? I think you're confusing yourself overly...what exactly is the overall purpose of this particular algorithm that needs the sorted arraylist?
(p.s. Arrays have their place, but they're largely overused by people that don't know how to use lists, deques and vectors...I even used to be one of those people!)
elaborated. only thing else to add is that through the whole code i wrote the original numbers an index is completely unchanged only the cpy is changed. thats basically what you wanted
and the sort makes the biggest go to aarrycpy[0] smallest gets higher [number]. to make it go the other direction change the if statemnt to aarrycpy[x] > aarrycpy[x+1]
the swap part should still work with above statement
@ciphermagi: I have an input file of the form m, p1, p2, p3, p4 ... pm.
I am actually using the vector class to pull in the values of the input file into a vector<double> P.
I am then sorting the values p1...pm into ascending order. The only way I know how to do this is by comparing the values of P and then adjusting the index in order to put them in order.
The actual point of the program, however, is to create a discrete random variable histogram where the position of the variable is important. The input actually specifies the probability and the position is the value. As an example:
Input: 4, .2, .1, .4, .3
This is saying I have 4 probabilities. I have a 20% chance of getting value 1, I have 10% chance of getting value 2, I have a 40% chance of getting value 3 and finally a 30% chance of getting value 4.
When I sort this however, I get P_sorted = 4 .1 .2 .3 .4 but I still need to have 10% chance of getting 2, 20% of 1, so on and so forth...
But how do I do that? What is the difference between lists and using a vector? Are you suggesting I make 2 vectors, one being the vector to sort and one that simply counts 1,2,3...m. Then when I sort the first vector just apply those same moves to the counting vector?
Well, you'll have to make your own sort function, but...once you have the sort function (see the above code by markyrocks), every time you perform any change on one vector, perform the exact same change on it's companion.