Put an element in a sorted vector

Given a sorted vector V and an element a, put the element a into the vector keeping the vector sorted.
Example:
INPUT
[3 24 88]
55
OUTPUT
[3 24 55 88]

EDIT: the way I wrote the code means that I expect that it is given a sorted vector of elements(so a vector that starts from the little one to the bigger one).

The code I wrote is 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
#include <iostream>

using namespace std;

int main () {
	//Initialization:
	const int N = 100;
	int vec[N];
	int number = 0, numbvec = 0, n = 0, temp = 0;
	
	//Input:
	cout<<"How many elements do you want to put in the vector?\n";
	cin>>n;
	for (int i = 0; i<n; i++) {
		cin>>numbvec;
		vec[i] = numbvec;
	}
	cout<<"Insert another element.\n";
        cin>>number;
	
	//Procedure:
	for (int j = 0; j<n; j++) {
		if (number < vec[j]) {
			temp = vec[j];
			vec[j] = number;
			number = temp;
		}
	}
	
	//Output:
	for (int k = 0; k<n+1; k++) {
		cout<<vec[k]<<" ";
	}
	
	//Termination:
	return 0;
}


The problem is in the second for loop. Because I don't know how to put the last element of the vector in the next position. Any idea how to do that?
Last edited on
Given a sorted vector V
I do not see any vectors in your code.

put the element a into the vector keeping the vector sorted.
This is better be done in two stages: (1) find point of insertion, (2) insert element at tha point.
If you have sorted vector it is easy:
1
2
3
4
std::vector<int> foo {11, 23, 46, 70};
int value = 33;
auto it = std::upper_bound(foo.cbegin(), foo.cend(), value); //1
foo.insert(it, value); //2 
http://coliru.stacked-crooked.com/
I do not see any vectors in your code.

That's because I wanted the program to ask the vector so I wrote:
1
2
3
4
5
6
7
//Input:
	cout<<"How many elements do you want to put in the vector?\n";
	cin>>n;
	for (int i = 0; i<n; i++) {
		cin>>numbvec;
		vec[i] = numbvec;
	}

And then the element that must be put in.
1
2
cout<<"Insert another element.\n";
        cin>>number;

I know, I should have initialized it and then give the output.

What does the std::upper_bound do? I can't find it in the reference of <vector>.
That's because I wanted the program to ask the vector so
Well, why didn't you store it inside vector then?
http://coliru.stacked-crooked.com/a/6b6020eb94137552

What does the std::upper_bound do? I can't find it in the reference of <vector>.
it returns iterator to first element larger than value. Basicly an insertion point for insert after
http://en.cppreference.com/w/cpp/algorithm/upper_bound
I stored it into vector. This is my vector:
int vec[N];

Oh! It was in <algorithm>. That's why I couldn't find it.

Anyway, is there no other way to do it without using <vector> and <algorithm>? My teacher doesn't use those library, he uses arrays like I did.
I stored it into vector. This is my vector:
int vec[N];
This is an array, not a vector. You can easily use all those functions with arrays instead of vectors (although you will have to manage size manually).

So, you need first to find insertion point, index of elemnt where new value should be.
Then you will need to shift all values from the end to your point one element to the right:
1
2
3
4
int n = /*...*/;//Amount of elements in array
int p = /*...*/; //insertion point
for(int i = n; i > p; --i)
    array[i] = array[i-1];
Then you need to insert element to the place it should occupy: array[p] = value;
To find the position I have to find the first number that is bigger than the one I want to put in. It should be like this:
1
2
3
4
5
for (int i = 1; i<n; i++) {
		if(a < vec[i]) {
			pos=i;
		}
	}

At this point I should use:
1
2
for(int i = n; i > pos; --i)
    vec[i] = vec[i-1];


In what part of the code should I write vec[pos] = a?

EDIT: Nevermind! I had to put it after the shift. Thanks a lot!
Last edited on
Topic archived. No new replies allowed.