Sorting An Array

Hi Guys,

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.




Last edited on
thinkin somethin like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>
using namespace 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
}


untested but no errors
Last edited on
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!)
Can you elaborate a little on what exactly is going on with your code? I am having trouble interpreting it completely.
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
Last edited on
@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...

Does that make sense?
So you're making this more complicated than it has to be. Basically, you're doing:

Make the index references stay the same when I sort it.


But what you need to do is:

Make the other list that I have sort so that their indexes and the original indexes still match.


The second option is much easier. All you have to do is, whenever you make a change to the first list, make an identical change to the second list.
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?
Why not? Doesn't it make sense?
Yea that makes sense. I am having trouble implementing this now. How do I get the position vector to move the same way as the sorted vector?

I had an idea but it isn't working...
Last edited on
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.
Yea the initial idea I had was correct, I was just trying to sort it at the wrong spot in the code. Thanks for the help.
Topic archived. No new replies allowed.